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 cache service that stored data in databases
 13:  *
 14:  * @author  Twin Huang <twinhuang@qq.com>
 15:  * @property Db $db A database service
 16:  */
 17: class DbCache extends BaseCache
 18: {
 19:     /**
 20:      * The cache table name
 21:      *
 22:      * @var string
 23:      */
 24:     protected $table = 'cache';
 25: 
 26:     /**
 27:      * Whether check if table is exists or not
 28:      *
 29:      * @var bool
 30:      */
 31:     protected $checkTable = true;
 32: 
 33:     /**
 34:      * The SQL to check if table exists
 35:      *
 36:      * @var array
 37:      */
 38:     protected $checkTableSqls = array(
 39:         'mysql'     => "SHOW TABLES LIKE '%s'",
 40:         'sqlite'    => "SELECT name FROM sqlite_master WHERE type='table' AND name='%s'",
 41:         'pgsql'     => "SELECT true FROM pg_tables WHERE tablename = '%s'"
 42:     );
 43: 
 44:     /**
 45:      * The SQL to create cache table
 46:      *
 47:      * @var array
 48:      */
 49:     protected $createTableSqls = array(
 50:         'mysql'  => "CREATE TABLE %s (id VARCHAR(255) NOT NULL, value LONGTEXT NOT NULL, expire DATETIME NOT NULL, lastModified DATETIME NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci ENGINE = InnoDB",
 51:         'sqlite' => "CREATE TABLE %s (id VARCHAR(255) NOT NULL, value CLOB NOT NULL, expire DATETIME NOT NULL, lastModified DATETIME NOT NULL, PRIMARY KEY(id))",
 52:         'pgsql'  => "CREATE TABLE %s (id VARCHAR(255) NOT NULL, value TEXT NOT NULL, expire TIMESTAMP(0) WITHOUT TIME ZONE NOT NULL, lastModified TIMESTAMP(0) WITHOUT TIME ZONE NOT NULL, PRIMARY KEY(id))",
 53:     );
 54: 
 55:     /**
 56:      * Constructor
 57:      *
 58:      * @param array $options
 59:      */
 60:     public function __construct(array $options = array())
 61:     {
 62:         parent::__construct($options);
 63:         $this->prepareTable();
 64:     }
 65: 
 66:     /**
 67:      * Check if table exists, if not exists, create table
 68:      */
 69:     public function prepareTable()
 70:     {
 71:         $driver = $this->db->getDriver();
 72:         $tableExistsSql = sprintf($this->checkTableSqls[$driver], $this->table);
 73:         if ($this->checkTable && !$this->db->fetchColumn($tableExistsSql)) {
 74:             $createTableSql = sprintf($this->createTableSqls[$driver], $this->table);
 75:             $this->db->executeUpdate($createTableSql);
 76:         }
 77:     }
 78: 
 79:     /**
 80:      * {@inheritdoc}
 81:      */
 82:     public function get($key, $expire = null, $fn = null)
 83:     {
 84:         if ($this->exists($key)) {
 85:             $result = $this->db->select($this->table, $this->namespace . $key);
 86:             $result = unserialize($result['value']);
 87:         } else {
 88:             $result = false;
 89:         }
 90:         return $this->processGetResult($key, $result, $expire, $fn);
 91:     }
 92: 
 93:     /**
 94:      * {@inheritdoc}
 95:      */
 96:     public function set($key, $value, $expire = 0)
 97:     {
 98:         $data = array(
 99:             'value' => serialize($value),
100:             'lastModified' => date('Y-m-d H:i:s'),
101:             'expire' => date('Y-m-d H:i:s', $expire ? time() + $expire : 2147483647)
102:         );
103:         $identifier = array(
104:             'id' => $this->namespace . $key
105:         );
106: 
107:         if ($this->exists($key)) {
108:             // In MySQL, the rowCount method return 0 when data is not modified,
109:             // so check errorCode to make sure it executed success
110:             $result = $this->db->update($this->table, $data, $identifier) || '0000' == $this->db->errorCode();
111:         } else {
112:             $result = $this->db->insert($this->table, $data + $identifier);
113:         }
114:         return (bool)$result;
115:     }
116: 
117:     /**
118:      * {@inheritdoc}
119:      */
120:     public function remove($key)
121:     {
122:         return (bool)$this->db->delete($this->table, array('id' => $this->namespace . $key));
123:     }
124: 
125:     /**
126:      * {@inheritdoc}
127:      */
128:     public function exists($key)
129:     {
130:         $result = $this->db->select($this->table, $this->namespace . $key);
131: 
132:         if (!$result) {
133:             return false;
134:         }
135:         if ($result['expire'] < date('Y-m-d H:i:s')) {
136:             $this->remove($key);
137:             return false;
138:         }
139: 
140:         return true;
141:     }
142: 
143:     /**
144:      * {@inheritdoc}
145:      */
146:     public function add($key, $value, $expire = 0)
147:     {
148:         if ($this->exists($key)) {
149:             return false;
150:         } else {
151:             return $this->set($key, $value, $expire);
152:         }
153:     }
154: 
155:     /**
156:      * {@inheritdoc}
157:      */
158:     public function replace($key, $value, $expire = 0)
159:     {
160:         if (!$this->exists($key)) {
161:             return false;
162:         } else {
163:             return $this->set($key, $value, $expire);
164:         }
165:     }
166: 
167:     /**
168:      * Note: This method is not an atomic operation
169:      *
170:      * {@inheritdoc}
171:      */
172:     public function incr($key, $offset = 1)
173:     {
174:         $value = $this->get($key) + $offset;
175:         return $this->set($key, $value) ? $value : false;
176:     }
177: 
178:     /**
179:      * {@inheritdoc}
180:      */
181:     public function clear()
182:     {
183:         return (bool)$this->db->executeUpdate("DELETE FROM {$this->table}");
184:     }
185: }
186: 
Wei Framework API documentation generated by ApiGen