Overview

Namespaces

  • None
  • Wei
    • Validator

Classes

  • Wei\Validator\All
  • Wei\Validator\AllOf
  • Wei\Validator\Alnum
  • Wei\Validator\Alpha
  • Wei\Validator\BaseValidator
  • Wei\Validator\Between
  • Wei\Validator\Blank
  • Wei\Validator\Callback
  • Wei\Validator\CharLength
  • Wei\Validator\Chinese
  • Wei\Validator\Color
  • Wei\Validator\Contains
  • Wei\Validator\CreditCard
  • Wei\Validator\Date
  • Wei\Validator\DateTime
  • Wei\Validator\Decimal
  • Wei\Validator\Digit
  • Wei\Validator\Dir
  • Wei\Validator\DivisibleBy
  • Wei\Validator\DoubleByte
  • Wei\Validator\Email
  • Wei\Validator\EndsWith
  • Wei\Validator\EqualTo
  • Wei\Validator\Exists
  • Wei\Validator\FieldExists
  • Wei\Validator\File
  • Wei\Validator\GreaterThan
  • Wei\Validator\GreaterThanOrEqual
  • Wei\Validator\IdCardCn
  • Wei\Validator\IdCardHk
  • Wei\Validator\IdCardMo
  • Wei\Validator\IdCardTw
  • Wei\Validator\IdenticalTo
  • Wei\Validator\Image
  • Wei\Validator\In
  • Wei\Validator\Ip
  • Wei\Validator\Length
  • Wei\Validator\LessThan
  • Wei\Validator\LessThanOrEqual
  • Wei\Validator\Lowercase
  • Wei\Validator\Luhn
  • Wei\Validator\MaxLength
  • Wei\Validator\MinLength
  • Wei\Validator\MobileCn
  • Wei\Validator\NaturalNumber
  • Wei\Validator\NoneOf
  • Wei\Validator\Null
  • Wei\Validator\Number
  • Wei\Validator\OneOf
  • Wei\Validator\Password
  • Wei\Validator\Phone
  • Wei\Validator\PhoneCn
  • Wei\Validator\PlateNumberCn
  • Wei\Validator\PositiveInteger
  • Wei\Validator\PostcodeCn
  • Wei\Validator\Present
  • Wei\Validator\QQ
  • Wei\Validator\RecordExists
  • Wei\Validator\Regex
  • Wei\Validator\Required
  • Wei\Validator\SomeOf
  • Wei\Validator\StartsWith
  • Wei\Validator\Time
  • Wei\Validator\Tld
  • Wei\Validator\Type
  • Wei\Validator\Uppercase
  • Wei\Validator\Url
  • Wei\Validator\Uuid
  • Overview
  • Namespace
  • Function
  1: <?php
  2: /**
  3:  * Wei Framework
  4:  *
  5:  * @copyright   Copyright (c) 2008-2015 Twin Huang
  6:  * @license     http://opensource.org/licenses/mit-license.php MIT License
  7:  */
  8: 
  9: namespace Wei;
 10: 
 11: /**
 12:  * A two-level cache service
 13:  *
 14:  * @author      Twin Huang <twinhuang@qq.com>
 15:  * @property    BaseCache $master The master(faster) cache service
 16:  * @property    BaseCache $slave The slave(slower) cache service
 17:  */
 18: class Bicache extends BaseCache
 19: {
 20:     /**
 21:      * @var array
 22:      */
 23:     protected $providers = array(
 24:         'master' => 'apc',
 25:         'slave' => 'fileCache',
 26:     );
 27: 
 28:     /**
 29:      * The seconds to update the slave cache
 30:      *
 31:      * @var int
 32:      */
 33:     protected $time = 5;
 34: 
 35:     /**
 36:      * {@inheritdoc}
 37:      */
 38:     public function get($key, $expire = null, $fn = null)
 39:     {
 40:         $key = $this->namespace . $key;
 41:         $result = $this->master->get($key);
 42:         if (false === $result) {
 43:             $result = $this->slave->get($key);
 44:         }
 45:         return $this->processGetResult($key, $result, $expire, $fn);
 46:     }
 47: 
 48:     /**
 49:      * {@inheritdoc}
 50:      */
 51:     public function set($key, $value, $expire = 0)
 52:     {
 53:         $key = $this->namespace . $key;
 54:         $result = $this->master->set($key, $value, $expire);
 55: 
 56:         if ($result && $this->needUpdate($key)) {
 57:             // $result is true, so return the slave cache result
 58:             return $this->slave->set($key, $value, $expire);
 59:         }
 60: 
 61:         // No need to update, returns the master cache result
 62:         return $result;
 63:     }
 64: 
 65:     /**
 66:      * {@inheritdoc}
 67:      */
 68:     public function remove($key)
 69:     {
 70:         $key = $this->namespace . $key;
 71:         $result1 = $this->master->remove($key);
 72:         $result2 = $this->slave->remove($key);
 73: 
 74:         return $result1 && $result2;
 75:     }
 76: 
 77:     /**
 78:      * {@inheritdoc}
 79:      */
 80:     public function exists($key)
 81:     {
 82:         $key = $this->namespace . $key;
 83:         return $this->master->exists($key) || $this->slave->exists($key);
 84:     }
 85: 
 86:     /**
 87:      * {@inheritdoc}
 88:      */
 89:     public function add($key, $value, $expire = 0)
 90:     {
 91:         $key = $this->namespace . $key;
 92:         $result = $this->master->add($key, $value, $expire);
 93: 
 94:         // The cache can be added only one time, when added success, set it to the slave cache
 95:         if ($result) {
 96:             // $result is true, so return the slave cache result only
 97:             return $this->slave->set($key, $value, $expire);
 98:         }
 99: 
100:         // false
101:         return $result;
102:     }
103: 
104:     /**
105:      * {@inheritdoc}
106:      */
107:     public function replace($key, $value, $expire = 0)
108:     {
109:         $key = $this->namespace . $key;
110:         $result = $this->master->replace($key, $value, $expire);
111: 
112:         // The cache can always be replaced when it's exists, so check for update
113:         if ($result && $this->needUpdate($key)) {
114:             return $this->slave->set($key, $value, $expire);
115:         }
116: 
117:         return $result;
118:     }
119: 
120:     /**
121:      * {@inheritdoc}
122:      */
123:     public function incr($key, $offset = 1)
124:     {
125:         $key = $this->namespace . $key;
126:         $result = $this->master->incr($key, $offset);
127: 
128:         if (false !== $result && $this->needUpdate($key)) {
129:             return $this->slave->set($key, $result) ? $result : false;
130:         }
131: 
132:         return $result;
133:     }
134: 
135:     /**
136:      * {@inheritdoc}
137:      */
138:     public function clear()
139:     {
140:         $result1 = $this->master->clear();
141:         $result2 = $this->slave->clear();
142:         return $result1 && $result2;
143:     }
144: 
145:     /**
146:      * Check if the key need to update to the slave cache
147:      *
148:      * @param string $key
149:      * @return bool
150:      */
151:     protected function needUpdate($key)
152:     {
153:         return $this->master->add('__' . $key, true, $this->time);
154:     }
155: }
156: 
Wei Framework API documentation generated by ApiGen