1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Doctrine\Common\Cache; |
4
|
|
|
|
5
|
|
|
use MongoCollection; |
6
|
|
|
use MongoDB\Collection; |
7
|
|
|
use const E_USER_DEPRECATED; |
8
|
|
|
use function trigger_error; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* MongoDB cache provider. |
12
|
|
|
*/ |
13
|
|
|
class MongoDBCache extends CacheProvider |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* The data field will store the serialized PHP value. |
17
|
|
|
*/ |
18
|
|
|
public const DATA_FIELD = 'd'; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* The expiration field will store a MongoDate value indicating when the |
22
|
|
|
* cache entry should expire. |
23
|
|
|
* |
24
|
|
|
* With MongoDB 2.2+, entries can be automatically deleted by MongoDB by |
25
|
|
|
* indexing this field with the "expireAfterSeconds" option equal to zero. |
26
|
|
|
* This will direct MongoDB to regularly query for and delete any entries |
27
|
|
|
* whose date is older than the current time. Entries without a date value |
28
|
|
|
* in this field will be ignored. |
29
|
|
|
* |
30
|
|
|
* The cache provider will also check dates on its own, in case expired |
31
|
|
|
* entries are fetched before MongoDB's TTLMonitor pass can expire them. |
32
|
|
|
* |
33
|
|
|
* @see http://docs.mongodb.org/manual/tutorial/expire-data/ |
34
|
|
|
*/ |
35
|
|
|
public const EXPIRATION_FIELD = 'e'; |
36
|
|
|
|
37
|
|
|
/** @var CacheProvider */ |
38
|
|
|
private $provider; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* This provider will default to the write concern and read preference |
42
|
|
|
* options set on the collection instance (or inherited from MongoDB or |
43
|
|
|
* MongoClient). Using an unacknowledged write concern (< 1) may make the |
44
|
|
|
* return values of delete() and save() unreliable. Reading from secondaries |
45
|
|
|
* may make contain() and fetch() unreliable. |
46
|
|
|
* |
47
|
|
|
* @see http://www.php.net/manual/en/mongo.readpreferences.php |
48
|
|
|
* @see http://www.php.net/manual/en/mongo.writeconcerns.php |
49
|
|
|
* @param MongoCollection|Collection $collection |
50
|
|
|
*/ |
51
|
|
|
public function __construct($collection) |
52
|
|
|
{ |
53
|
|
|
if ($collection instanceof MongoCollection) { |
54
|
|
|
@trigger_error('Using a MongoCollection instance for creating a cache adapter is deprecated and will be removed in 2.0', E_USER_DEPRECATED); |
55
|
|
|
$this->provider = new LegacyMongoDBCache($collection); |
56
|
|
|
} elseif ($collection instanceof Collection) { |
|
|
|
|
57
|
|
|
$this->provider = new ExtMongoDBCache($collection); |
58
|
|
|
} else { |
59
|
|
|
throw new \InvalidArgumentException('Invalid collection given - expected a MongoCollection or MongoDB\Collection instance'); |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* {@inheritdoc} |
65
|
|
|
*/ |
66
|
|
|
protected function doFetch($id) |
67
|
|
|
{ |
68
|
|
|
return $this->provider->doFetch($id); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* {@inheritdoc} |
73
|
156 |
|
*/ |
74
|
|
|
protected function doContains($id) |
75
|
156 |
|
{ |
76
|
78 |
|
return $this->provider->doContains($id); |
77
|
78 |
|
} |
78
|
78 |
|
|
79
|
78 |
|
/** |
80
|
|
|
* {@inheritdoc} |
81
|
|
|
*/ |
82
|
|
|
protected function doSave($id, $data, $lifeTime = 0) |
83
|
156 |
|
{ |
84
|
|
|
return $this->provider->doSave($id, $data, $lifeTime); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
152 |
|
* {@inheritdoc} |
89
|
|
|
*/ |
90
|
152 |
|
protected function doDelete($id) |
91
|
|
|
{ |
92
|
|
|
return $this->provider->doDelete($id); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
146 |
|
* {@inheritdoc} |
97
|
|
|
*/ |
98
|
146 |
|
protected function doFlush() |
99
|
|
|
{ |
100
|
|
|
return $this->provider->doFlush(); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
148 |
|
* {@inheritdoc} |
105
|
|
|
*/ |
106
|
148 |
|
protected function doGetStats() |
107
|
|
|
{ |
108
|
|
|
return $this->provider->doGetStats(); |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|