1 | <?php |
||
31 | class MongoDBCache extends CacheProvider |
||
32 | { |
||
33 | /** |
||
34 | * The data field will store the serialized PHP value. |
||
35 | */ |
||
36 | const DATA_FIELD = 'd'; |
||
37 | |||
38 | /** |
||
39 | * The expiration field will store a MongoDate value indicating when the |
||
40 | * cache entry should expire. |
||
41 | * |
||
42 | * With MongoDB 2.2+, entries can be automatically deleted by MongoDB by |
||
43 | * indexing this field with the "expireAfterSeconds" option equal to zero. |
||
44 | * This will direct MongoDB to regularly query for and delete any entries |
||
45 | * whose date is older than the current time. Entries without a date value |
||
46 | * in this field will be ignored. |
||
47 | * |
||
48 | * The cache provider will also check dates on its own, in case expired |
||
49 | * entries are fetched before MongoDB's TTLMonitor pass can expire them. |
||
50 | * |
||
51 | * @see http://docs.mongodb.org/manual/tutorial/expire-data/ |
||
52 | */ |
||
53 | const EXPIRATION_FIELD = 'e'; |
||
54 | |||
55 | /** |
||
56 | * @var CacheProvider |
||
57 | */ |
||
58 | private $provider; |
||
59 | |||
60 | /** |
||
61 | * Constructor. |
||
62 | * |
||
63 | * This provider will default to the write concern and read preference |
||
64 | * options set on the collection instance (or inherited from MongoDB or |
||
65 | * MongoClient). Using an unacknowledged write concern (< 1) may make the |
||
66 | * return values of delete() and save() unreliable. Reading from secondaries |
||
67 | * may make contain() and fetch() unreliable. |
||
68 | * |
||
69 | * @see http://www.php.net/manual/en/mongo.readpreferences.php |
||
70 | * @see http://www.php.net/manual/en/mongo.writeconcerns.php |
||
71 | * @param MongoCollection|Collection $collection |
||
72 | */ |
||
73 | 154 | public function __construct($collection) |
|
84 | |||
85 | /** |
||
86 | * {@inheritdoc} |
||
87 | */ |
||
88 | 150 | protected function doFetch($id) |
|
92 | |||
93 | /** |
||
94 | * {@inheritdoc} |
||
95 | */ |
||
96 | 144 | protected function doContains($id) |
|
100 | |||
101 | /** |
||
102 | * {@inheritdoc} |
||
103 | */ |
||
104 | 146 | protected function doSave($id, $data, $lifeTime = 0) |
|
108 | |||
109 | /** |
||
110 | * {@inheritdoc} |
||
111 | */ |
||
112 | 90 | protected function doDelete($id) |
|
116 | |||
117 | /** |
||
118 | * {@inheritdoc} |
||
119 | */ |
||
120 | 4 | protected function doFlush() |
|
124 | |||
125 | /** |
||
126 | * {@inheritdoc} |
||
127 | */ |
||
128 | 2 | protected function doGetStats() |
|
132 | } |
||
133 |
If you suppress an error, we recommend checking for the error condition explicitly: