3 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
4 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
5 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
6 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
7 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
8 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
9 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
10 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
11 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
12 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
13 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
15 * This software consists of voluntary contributions made by many individuals
16 * and is licensed under the MIT license. For more information, see
17 * <http://www.doctrine-project.org>.
20 namespace Doctrine\Common\Cache;
26 * SQLite3 cache provider.
29 * @author Jake Bell <jake@theunraveler.com>
31 class SQLite3Cache extends CacheProvider
34 * The ID field will store the cache key.
39 * The data field will store the serialized PHP value.
41 const DATA_FIELD = 'd';
44 * The expiration field will store a date value indicating when the
45 * cache entry should expire.
47 const EXPIRATION_FIELD = 'e';
62 * Calling the constructor will ensure that the database file and table
63 * exist and will create both if they don't.
65 * @param SQLite3 $sqlite
66 * @param string $table
68 public function __construct(SQLite3 $sqlite, $table)
70 $this->sqlite = $sqlite;
71 $this->table = (string) $table;
73 list($id, $data, $exp) = $this->getFields();
75 return $this->sqlite->exec(sprintf(
76 'CREATE TABLE IF NOT EXISTS %s(%s TEXT PRIMARY KEY NOT NULL, %s BLOB, %s INTEGER)',
87 protected function doFetch($id)
89 if ($item = $this->findById($id)) {
90 return unserialize($item[self::DATA_FIELD]);
99 protected function doContains($id)
101 return null !== $this->findById($id, false);
107 protected function doSave($id, $data, $lifeTime = 0)
109 $statement = $this->sqlite->prepare(sprintf(
110 'INSERT OR REPLACE INTO %s (%s) VALUES (:id, :data, :expire)',
112 implode(',', $this->getFields())
115 $statement->bindValue(':id', $id);
116 $statement->bindValue(':data', serialize($data), SQLITE3_BLOB);
117 $statement->bindValue(':expire', $lifeTime > 0 ? time() + $lifeTime : null);
119 return $statement->execute() instanceof SQLite3Result;
125 protected function doDelete($id)
127 list($idField) = $this->getFields();
129 $statement = $this->sqlite->prepare(sprintf(
130 'DELETE FROM %s WHERE %s = :id',
135 $statement->bindValue(':id', $id);
137 return $statement->execute() instanceof SQLite3Result;
143 protected function doFlush()
145 return $this->sqlite->exec(sprintf('DELETE FROM %s', $this->table));
151 protected function doGetStats()
157 * Find a single row by ID.
160 * @param bool $includeData
164 private function findById($id, $includeData = true)
166 list($idField) = $fields = $this->getFields();
169 $key = array_search(static::DATA_FIELD, $fields);
170 unset($fields[$key]);
173 $statement = $this->sqlite->prepare(sprintf(
174 'SELECT %s FROM %s WHERE %s = :id LIMIT 1',
175 implode(',', $fields),
180 $statement->bindValue(':id', $id, SQLITE3_TEXT);
182 $item = $statement->execute()->fetchArray(SQLITE3_ASSOC);
184 if ($item === false) {
188 if ($this->isExpired($item)) {
189 $this->doDelete($id);
198 * Gets an array of the fields in our table.
202 private function getFields()
204 return array(static::ID_FIELD, static::DATA_FIELD, static::EXPIRATION_FIELD);
208 * Check if the item is expired.
214 private function isExpired(array $item)
216 return isset($item[static::EXPIRATION_FIELD]) &&
217 $item[self::EXPIRATION_FIELD] !== null &&
218 $item[self::EXPIRATION_FIELD] < time();