1 | <?php |
||
33 | class MongoDBCache extends CacheProvider |
||
34 | { |
||
35 | /** |
||
36 | * The data field will store the serialized PHP value. |
||
37 | */ |
||
38 | const DATA_FIELD = 'd'; |
||
39 | |||
40 | /** |
||
41 | * The expiration field will store a MongoDate value indicating when the |
||
42 | * cache entry should expire. |
||
43 | * |
||
44 | * With MongoDB 2.2+, entries can be automatically deleted by MongoDB by |
||
45 | * indexing this field with the "expireAfterSeconds" option equal to zero. |
||
46 | * This will direct MongoDB to regularly query for and delete any entries |
||
47 | * whose date is older than the current time. Entries without a date value |
||
48 | * in this field will be ignored. |
||
49 | * |
||
50 | * The cache provider will also check dates on its own, in case expired |
||
51 | * entries are fetched before MongoDB's TTLMonitor pass can expire them. |
||
52 | * |
||
53 | * @see http://docs.mongodb.org/manual/tutorial/expire-data/ |
||
54 | */ |
||
55 | const EXPIRATION_FIELD = 'e'; |
||
56 | |||
57 | /** |
||
58 | * @var MongoCollection |
||
59 | */ |
||
60 | private $collection; |
||
61 | |||
62 | /** |
||
63 | * @var bool |
||
64 | */ |
||
65 | private $expirationIndexCreated = false; |
||
66 | |||
67 | /** |
||
68 | * Constructor. |
||
69 | * |
||
70 | * This provider will default to the write concern and read preference |
||
71 | * options set on the MongoCollection instance (or inherited from MongoDB or |
||
72 | * MongoClient). Using an unacknowledged write concern (< 1) may make the |
||
73 | * return values of delete() and save() unreliable. Reading from secondaries |
||
74 | * may make contain() and fetch() unreliable. |
||
75 | * |
||
76 | * @see http://www.php.net/manual/en/mongo.readpreferences.php |
||
77 | * @see http://www.php.net/manual/en/mongo.writeconcerns.php |
||
78 | * @param MongoCollection $collection |
||
79 | */ |
||
80 | 78 | public function __construct(MongoCollection $collection) |
|
84 | |||
85 | /** |
||
86 | * {@inheritdoc} |
||
87 | */ |
||
88 | 76 | protected function doFetch($id) |
|
104 | |||
105 | /** |
||
106 | * {@inheritdoc} |
||
107 | */ |
||
108 | 72 | protected function doContains($id) |
|
124 | |||
125 | /** |
||
126 | * {@inheritdoc} |
||
127 | */ |
||
128 | 74 | protected function doSave($id, $data, $lifeTime = 0) |
|
145 | |||
146 | /** |
||
147 | * {@inheritdoc} |
||
148 | */ |
||
149 | 46 | protected function doDelete($id) |
|
155 | |||
156 | /** |
||
157 | * {@inheritdoc} |
||
158 | */ |
||
159 | 2 | protected function doFlush() |
|
166 | |||
167 | /** |
||
168 | * {@inheritdoc} |
||
169 | */ |
||
170 | 1 | protected function doGetStats() |
|
190 | |||
191 | /** |
||
192 | * Check if the document is expired. |
||
193 | * |
||
194 | * @param array $document |
||
195 | * |
||
196 | * @return bool |
||
197 | */ |
||
198 | 69 | private function isExpired(array $document) : bool |
|
204 | |||
205 | 1 | private function createExpirationIndex(): void |
|
214 | } |
||
215 |