12
12
namespace Cache \Adapter \Memcached ;
13
13
14
14
use Cache \Adapter \Common \AbstractCachePool ;
15
+ use Cache \Hierarchy \HierarchicalCachePoolTrait ;
16
+ use Cache \Hierarchy \HierarchicalPoolInterface ;
15
17
use Psr \Cache \CacheItemInterface ;
16
18
17
19
/**
18
20
* @author Aaron Scherer <[email protected] >
19
21
* @author Tobias Nyholm <[email protected] >
20
22
*/
21
- class MemcachedCachePool extends AbstractCachePool
23
+ class MemcachedCachePool extends AbstractCachePool implements HierarchicalPoolInterface
22
24
{
25
+ use HierarchicalCachePoolTrait;
26
+
23
27
/**
24
28
* @type \Memcached
25
29
*/
@@ -31,11 +35,16 @@ class MemcachedCachePool extends AbstractCachePool
31
35
public function __construct (\Memcached $ cache )
32
36
{
33
37
$ this ->cache = $ cache ;
38
+ $ this ->cache ->setOption (\Memcached::OPT_BINARY_PROTOCOL , true );
34
39
}
35
40
36
41
protected function fetchObjectFromCache ($ key )
37
42
{
38
- return $ this ->cache ->get ($ key );
43
+ if (false === $ result = unserialize ($ this ->cache ->get ($ this ->getHierarchyKey ($ key )))) {
44
+ return [false , null ];
45
+ }
46
+
47
+ return $ result ;
39
48
}
40
49
41
50
protected function clearAllObjectsFromCache ()
@@ -45,6 +54,11 @@ protected function clearAllObjectsFromCache()
45
54
46
55
protected function clearOneObjectFromCache ($ key )
47
56
{
57
+ $ this ->commit ();
58
+ $ key = $ this ->getHierarchyKey ($ key , $ path );
59
+ $ this ->cache ->increment ($ path , 1 , 0 );
60
+ $ this ->clearHierarchyKeyCache ();
61
+
48
62
if ($ this ->cache ->delete ($ key )) {
49
63
return true ;
50
64
}
@@ -59,6 +73,13 @@ protected function storeItemInCache($key, CacheItemInterface $item, $ttl)
59
73
$ ttl = 0 ;
60
74
}
61
75
62
- return $ this ->cache ->set ($ key , $ item , $ ttl );
76
+ $ key = $ this ->getHierarchyKey ($ key );
77
+
78
+ return $ this ->cache ->set ($ key , serialize ([true , $ item ->get ()]), $ ttl );
79
+ }
80
+
81
+ protected function getValueFormStore ($ key )
82
+ {
83
+ return $ this ->cache ->get ($ key );
63
84
}
64
85
}
0 commit comments