1212namespace Cache \Adapter \Memcached ;
1313
1414use Cache \Adapter \Common \AbstractCachePool ;
15+ use Cache \Hierarchy \HierarchicalCachePoolTrait ;
16+ use Cache \Hierarchy \HierarchicalPoolInterface ;
1517use Psr \Cache \CacheItemInterface ;
1618
1719/**
1820 * @author Aaron Scherer <[email protected] > 1921 * @author Tobias Nyholm <[email protected] > 2022 */
21- class MemcachedCachePool extends AbstractCachePool
23+ class MemcachedCachePool extends AbstractCachePool implements HierarchicalPoolInterface
2224{
25+ use HierarchicalCachePoolTrait;
26+
2327 /**
2428 * @type \Memcached
2529 */
@@ -31,11 +35,16 @@ class MemcachedCachePool extends AbstractCachePool
3135 public function __construct (\Memcached $ cache )
3236 {
3337 $ this ->cache = $ cache ;
38+ $ this ->cache ->setOption (\Memcached::OPT_BINARY_PROTOCOL , true );
3439 }
3540
3641 protected function fetchObjectFromCache ($ key )
3742 {
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 ;
3948 }
4049
4150 protected function clearAllObjectsFromCache ()
@@ -45,6 +54,11 @@ protected function clearAllObjectsFromCache()
4554
4655 protected function clearOneObjectFromCache ($ key )
4756 {
57+ $ this ->commit ();
58+ $ key = $ this ->getHierarchyKey ($ key , $ path );
59+ $ this ->cache ->increment ($ path , 1 , 0 );
60+ $ this ->clearHierarchyKeyCache ();
61+
4862 if ($ this ->cache ->delete ($ key )) {
4963 return true ;
5064 }
@@ -59,6 +73,13 @@ protected function storeItemInCache($key, CacheItemInterface $item, $ttl)
5973 $ ttl = 0 ;
6074 }
6175
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 );
6384 }
6485}
0 commit comments