-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMongoDBCachePool.php
159 lines (133 loc) · 3.68 KB
/
MongoDBCachePool.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
<?php
/*
* This file is part of php-cache organization.
*
* (c) 2015 Aaron Scherer <[email protected]>, Tobias Nyholm <[email protected]>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace Cache\Adapter\MongoDB;
use Cache\Adapter\Common\AbstractCachePool;
use Cache\Adapter\Common\JsonBinaryArmoring;
use Cache\Adapter\Common\PhpCacheItem;
use Cache\Adapter\Common\TagSupportWithArray;
use MongoDB\Collection;
use MongoDB\Driver\Manager;
/**
* @author Tobias Nyholm <[email protected]>
* @author Magnus Nordlander
*/
class MongoDBCachePool extends AbstractCachePool
{
use JsonBinaryArmoring;
use TagSupportWithArray;
/**
* @type Collection
*/
private $collection;
/**
* @param Collection $collection
*/
public function __construct(Collection $collection)
{
$this->collection = $collection;
}
/**
* @param Manager $manager
* @param string $database
* @param string $collection
*
* @return Collection
*/
public static function createCollection(Manager $manager, $database, $collection)
{
$collection = new Collection($manager, $database, $collection);
$collection->createIndex(['expireAt' => 1], ['expireAfterSeconds' => 0]);
return $collection;
}
/**
* {@inheritdoc}
*/
protected function fetchObjectFromCache($key)
{
$object = $this->collection->findOne(['_id' => $key]);
if (!$object || !isset($object->data)) {
return [false, null, [], null];
}
if (isset($object->expiresAt)) {
if ($object->expiresAt < time()) {
return [false, null, [], null];
}
}
return [
true,
$this->thawValue($object->data),
$this->thawValue($object->tags),
$object->expirationTimestamp,
];
}
/**
* {@inheritdoc}
*/
protected function clearAllObjectsFromCache()
{
$this->collection->deleteMany([]);
return true;
}
/**
* {@inheritdoc}
*/
protected function clearOneObjectFromCache($key)
{
$this->collection->deleteOne(['_id' => $key]);
return true;
}
/**
* {@inheritdoc}
*/
protected function storeItemInCache(PhpCacheItem $item, $ttl)
{
$object = [
'_id' => $item->getKey(),
'data' => $this->freezeValue($item->get()),
'tags' => $this->freezeValue($item->getTags()),
'expirationTimestamp' => $item->getExpirationTimestamp(),
];
if ($ttl) {
$object['expiresAt'] = time() + $ttl;
}
$this->collection->updateOne(['_id' => $item->getKey()], ['$set' => $object], ['upsert' => true]);
return true;
}
/**
* {@inheritdoc}
*/
public function getDirectValue($name)
{
$object = $this->collection->findOne(['_id' => $name]);
if (!$object || !isset($object->data)) {
return;
}
return $this->thawValue($object->data);
}
/**
* {@inheritdoc}
*/
public function setDirectValue($name, $value)
{
$object = [
'_id' => $name,
'data' => $this->freezeValue($value),
];
$this->collection->updateOne(['_id' => $name], ['$set' => $object], ['upsert' => true]);
}
private function freezeValue($value)
{
return static::jsonArmor(serialize($value));
}
private function thawValue($value)
{
return unserialize(static::jsonDeArmor($value));
}
}