1+ <?php
2+ /**
3+ *
4+ * This file is part of phpFastCache.
5+ *
6+ * @license MIT License (MIT)
7+ *
8+ * For full copyright and license information, please see the docs/CREDITS.txt file.
9+ *
10+ * @author Khoa Bui (khoaofgod) <[email protected] > http://www.phpfastcache.com 11+ * @author Georges.L (Geolim4) <[email protected] > 12+ *
13+ */
14+
15+ namespace phpFastCache \Drivers \Memstatic ;
16+
17+ use phpFastCache \Core \Pool \DriverBaseTrait ;
18+ use phpFastCache \Core \Pool \ExtendedCacheItemPoolInterface ;
19+ use phpFastCache \Entities \driverStatistic ;
20+ use phpFastCache \Exceptions \phpFastCacheDriverCheckException ;
21+ use phpFastCache \Exceptions \phpFastCacheDriverException ;
22+ use Psr \Cache \CacheItemInterface ;
23+
24+ /**
25+ * Class Driver
26+ * @package phpFastCache\Drivers
27+ */
28+ class Driver implements ExtendedCacheItemPoolInterface
29+ {
30+ use DriverBaseTrait;
31+
32+ /**
33+ * @var array
34+ */
35+ protected $ staticStack = [];
36+
37+ /**
38+ * Driver constructor.
39+ * @param array $config
40+ * @throws phpFastCacheDriverException
41+ */
42+ public function __construct (array $ config = [])
43+ {
44+ $ this ->setup ($ config );
45+
46+ if (!$ this ->driverCheck ()) {
47+ throw new phpFastCacheDriverCheckException (sprintf (self ::DRIVER_CHECK_FAILURE , $ this ->getDriverName ()));
48+ }
49+ }
50+
51+ /**
52+ * @return bool
53+ */
54+ public function driverCheck ()
55+ {
56+ return true ;
57+ }
58+
59+ /**
60+ * @param \Psr\Cache\CacheItemInterface $item
61+ * @return mixed
62+ * @throws \InvalidArgumentException
63+ */
64+ protected function driverWrite (CacheItemInterface $ item )
65+ {
66+ /**
67+ * Check for Cross-Driver type confusion
68+ */
69+ if ($ item instanceof Item) {
70+ return $ this ->staticStack [md5 ($ item ->getKey ())] = $ this ->driverPreWrap ($ item );
71+ } else {
72+ throw new \InvalidArgumentException ('Cross-Driver type confusion detected ' );
73+ }
74+ }
75+
76+ /**
77+ * @param \Psr\Cache\CacheItemInterface $item
78+ * @return array [
79+ * 'd' => 'THE ITEM DATA'
80+ * 't' => 'THE ITEM DATE EXPIRATION'
81+ * 'g' => 'THE ITEM TAGS'
82+ * ]
83+ */
84+ protected function driverRead (CacheItemInterface $ item )
85+ {
86+ $ key = md5 ($ item ->getKey ());
87+ if (isset ($ this ->staticStack [$ key ])){
88+ return $ this ->staticStack [$ key ];
89+ }
90+ return null ;
91+ }
92+
93+ /**
94+ * @param \Psr\Cache\CacheItemInterface $item
95+ * @return bool
96+ * @throws \InvalidArgumentException
97+ */
98+ protected function driverDelete (CacheItemInterface $ item )
99+ {
100+ /**
101+ * Check for Cross-Driver type confusion
102+ */
103+ if ($ item instanceof Item) {
104+ $ key = md5 ($ item ->getKey ());
105+ if (isset ($ this ->staticStack [$ key ])){
106+ unset($ this ->staticStack [$ key ]);
107+ return true ;
108+ }
109+ return false ;
110+ } else {
111+ throw new \InvalidArgumentException ('Cross-Driver type confusion detected ' );
112+ }
113+ }
114+
115+ /**
116+ * @return bool
117+ */
118+ protected function driverClear ()
119+ {
120+ unset($ this ->staticStack );
121+ $ this ->staticStack = [];
122+ return true ;
123+ }
124+
125+ /**
126+ * @return bool
127+ */
128+ protected function driverConnect ()
129+ {
130+ return true ;
131+ }
132+
133+ /********************
134+ *
135+ * PSR-6 Extended Methods
136+ *
137+ *******************/
138+
139+ /**
140+ * @return driverStatistic
141+ */
142+ public function getStats ()
143+ {
144+ $ stat = new driverStatistic ();
145+ $ stat ->setInfo ('[Memstatic] A memory static driver ' )
146+ ->setSize (mb_strlen (serialize ($ this ->staticStack )))
147+ ->setData (implode (', ' , array_keys ($ this ->itemInstances )))
148+ ->setRawData ($ this ->staticStack );
149+
150+ return $ stat ;
151+ }
152+ }
0 commit comments