Namespaces

  • Latte
    • Loaders
    • Macros
    • Runtime
  • Nette
    • Application
      • Responses
      • Routers
      • UI
    • Bridges
      • ApplicationDI
      • ApplicationLatte
      • ApplicationTracy
      • CacheDI
      • CacheLatte
      • DatabaseDI
      • DatabaseTracy
      • DITracy
      • FormsDI
      • FormsLatte
      • Framework
      • HttpDI
      • HttpTracy
      • MailDI
      • ReflectionDI
      • SecurityDI
      • SecurityTracy
    • Caching
      • Storages
    • ComponentModel
    • Database
      • Conventions
      • Drivers
      • Table
    • DI
      • Config
        • Adapters
      • Extensions
    • Forms
      • Controls
      • Rendering
    • Http
    • Iterators
    • Loaders
    • Localization
    • Mail
    • Neon
    • PhpGenerator
    • Reflection
    • Security
    • Utils
  • none
  • Tracy
    • Bridges
      • Nette

Classes

  • DevNullStorage
  • FileStorage
  • MemcachedStorage
  • MemoryStorage
  • NewMemcachedStorage
  • PhpFileStorage
  • SQLiteJournal
  • SQLiteStorage

Interfaces

  • IJournal
  • Overview
  • Namespace
  • Class
  • Tree
  • Deprecated
  1: <?php
  2: 
  3: /**
  4:  * This file is part of the Nette Framework (https://nette.org)
  5:  * Copyright (c) 2004 David Grudl (https://davidgrudl.com)
  6:  */
  7: 
  8: namespace Nette\Caching\Storages;
  9: 
 10: use Nette;
 11: use Nette\Caching\Cache;
 12: 
 13: 
 14: /**
 15:  * SQLite storage.
 16:  */
 17: class SQLiteStorage implements Nette\Caching\IStorage, Nette\Caching\IBulkReader
 18: {
 19:     use Nette\SmartObject;
 20: 
 21:     /** @var \PDO */
 22:     private $pdo;
 23: 
 24: 
 25:     public function __construct($path)
 26:     {
 27:         $this->pdo = new \PDO('sqlite:' . $path);
 28:         $this->pdo->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
 29:         $this->pdo->exec('
 30:             PRAGMA foreign_keys = ON;
 31:             CREATE TABLE IF NOT EXISTS cache (
 32:                 key BLOB NOT NULL PRIMARY KEY,
 33:                 data BLOB NOT NULL,
 34:                 expire INTEGER,
 35:                 slide INTEGER
 36:             );
 37:             CREATE TABLE IF NOT EXISTS tags (
 38:                 key BLOB NOT NULL REFERENCES cache ON DELETE CASCADE,
 39:                 tag BLOB NOT NULL
 40:             );
 41:             CREATE INDEX IF NOT EXISTS cache_expire ON cache(expire);
 42:             CREATE INDEX IF NOT EXISTS tags_key ON tags(key);
 43:             CREATE INDEX IF NOT EXISTS tags_tag ON tags(tag);
 44:             PRAGMA synchronous = OFF;
 45:         ');
 46:     }
 47: 
 48: 
 49:     /**
 50:      * Read from cache.
 51:      * @param  string key
 52:      * @return mixed|NULL
 53:      */
 54:     public function read($key)
 55:     {
 56:         $stmt = $this->pdo->prepare('SELECT data, slide FROM cache WHERE key=? AND (expire IS NULL OR expire >= ?)');
 57:         $stmt->execute([$key, time()]);
 58:         if ($row = $stmt->fetch(\PDO::FETCH_ASSOC)) {
 59:             if ($row['slide'] !== NULL) {
 60:                 $this->pdo->prepare('UPDATE cache SET expire = ? + slide WHERE key=?')->execute([time(), $key]);
 61:             }
 62:             return unserialize($row['data']);
 63:         }
 64:     }
 65: 
 66: 
 67:     /**
 68:      * Reads from cache in bulk.
 69:      * @param  string key
 70:      * @return array key => value pairs, missing items are omitted
 71:      */
 72:     public function bulkRead(array $keys)
 73:     {
 74:         $stmt = $this->pdo->prepare('SELECT key, data, slide FROM cache WHERE key IN (?' . str_repeat(',?', count($keys) - 1) . ') AND (expire IS NULL OR expire >= ?)');
 75:         $stmt->execute(array_merge($keys, [time()]));
 76:         $result = [];
 77:         $updateSlide = [];
 78:         foreach ($stmt->fetchAll(\PDO::FETCH_ASSOC) as $row) {
 79:             if ($row['slide'] !== NULL) {
 80:                 $updateSlide[] = $row['key'];
 81:             }
 82:             $result[$row['key']] = unserialize($row['data']);
 83:         }
 84:         if (!empty($updateSlide)) {
 85:             $stmt = $this->pdo->prepare('UPDATE cache SET expire = ? + slide WHERE key IN(?' . str_repeat(',?', count($updateSlide) - 1) . ')');
 86:             $stmt->execute(array_merge([time()], $updateSlide));
 87:         }
 88:         return $result;
 89:     }
 90: 
 91: 
 92:     /**
 93:      * Prevents item reading and writing. Lock is released by write() or remove().
 94:      * @param  string key
 95:      * @return void
 96:      */
 97:     public function lock($key)
 98:     {
 99:     }
100: 
101: 
102:     /**
103:      * Writes item into the cache.
104:      * @param  string key
105:      * @param  mixed  data
106:      * @param  array  dependencies
107:      * @return void
108:      */
109:     public function write($key, $data, array $dependencies)
110:     {
111:         $expire = isset($dependencies[Cache::EXPIRATION]) ? $dependencies[Cache::EXPIRATION] + time() : NULL;
112:         $slide = isset($dependencies[Cache::SLIDING]) ? $dependencies[Cache::EXPIRATION] : NULL;
113: 
114:         $this->pdo->exec('BEGIN TRANSACTION');
115:         $this->pdo->prepare('REPLACE INTO cache (key, data, expire, slide) VALUES (?, ?, ?, ?)')
116:             ->execute([$key, serialize($data), $expire, $slide]);
117: 
118:         if (!empty($dependencies[Cache::TAGS])) {
119:             foreach ((array) $dependencies[Cache::TAGS] as $tag) {
120:                 $arr[] = $key;
121:                 $arr[] = $tag;
122:             }
123:             $this->pdo->prepare('INSERT INTO tags (key, tag) SELECT ?, ?' . str_repeat('UNION SELECT ?, ?', count($arr) / 2 - 1))
124:                 ->execute($arr);
125:         }
126:         $this->pdo->exec('COMMIT');
127:     }
128: 
129: 
130:     /**
131:      * Removes item from the cache.
132:      * @param  string key
133:      * @return void
134:      */
135:     public function remove($key)
136:     {
137:         $this->pdo->prepare('DELETE FROM cache WHERE key=?')
138:             ->execute([$key]);
139:     }
140: 
141: 
142:     /**
143:      * Removes items from the cache by conditions & garbage collector.
144:      * @param  array  conditions
145:      * @return void
146:      */
147:     public function clean(array $conditions)
148:     {
149:         if (!empty($conditions[Cache::ALL])) {
150:             $this->pdo->prepare('DELETE FROM cache')->execute();
151: 
152:         } else {
153:             $sql = 'DELETE FROM cache WHERE expire < ?';
154:             $args = [time()];
155: 
156:             if (!empty($conditions[Cache::TAGS])) {
157:                 $tags = (array) $conditions[Cache::TAGS];
158:                 $sql .= ' OR key IN (SELECT key FROM tags WHERE tag IN (?' . str_repeat(',?', count($tags) - 1) . '))';
159:                 $args = array_merge($args, $tags);
160:             }
161: 
162:             $this->pdo->prepare($sql)->execute($args);
163:         }
164:     }
165: 
166: }
167: 
Nette 2.4-20160930 API API documentation generated by ApiGen 2.8.0