Skip to content

Commit c850242

Browse files
committed
Added Memstatic driver
1 parent 25e1f22 commit c850242

File tree

6 files changed

+286
-16
lines changed

6 files changed

+286
-16
lines changed

README.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,17 @@ One Class uses for All Cache. You don't need to rewrite your code many times aga
1212
### Supported drivers at this day *
1313
:bulb: Feel free to propose a driver by making a new **Pull Request**, they are welcome !
1414

15-
| Regular drivers | High performances drivers | Development driver |
16-
|--------------------|---------------------------|--------------------|
17-
| `Apc(u)` | `CouchBase` | `Devnull` |
18-
| `Cookie` | `Mongodb` | `Devfalse` |
19-
| `Files` | `Predis` | `Devtrue` |
20-
| `Leveldb` | `Redis` | |
21-
| `Memcache(d)` | `Ssdb` | |
22-
| `Sqlite` | `Zend Memory Cache` | |
23-
| `Wincache` | | |
24-
| `Xcache` | | |
25-
| `Zend Disk Cache` | | |
15+
| Regular drivers | High performances drivers | Development drivers |
16+
|--------------------|---------------------------|---------------------|
17+
| `Apc(u)` | `CouchBase` | `Devnull` |
18+
| `Cookie` | `Mongodb` | `Devfalse` |
19+
| `Files` | `Predis` | `Devtrue` |
20+
| `Leveldb` | `Redis` | `Memstatic` |
21+
| `Memcache(d)` | `Ssdb` | |
22+
| `Sqlite` | `Zend Memory Cache` | |
23+
| `Wincache` | | |
24+
| `Xcache` | | |
25+
| `Zend Disk Cache` | | |
2626

2727
\* Driver descriptions available in DOCS/DRIVERS.md
2828

docs/DRIVERS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
* The Memcache driver. A memory cache for regular performances. Do not cross this driver with Memcached driver
2222
* Memcached
2323
* The Memcached driver. A memory cache for regular performances. Do not cross this driver with Memcache driver
24+
* Memstatic
25+
* The Memstatic driver is a memory static driver that expires when the script execution ends
2426
* Mongodb
2527
* A very high-performance NoSQL driver using a key-value pair system
2628
* Predis

src/phpFastCache/CacheManager.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -260,21 +260,22 @@ public static function getDefaultConfig()
260260
public static function getStaticSystemDrivers()
261261
{
262262
return [
263-
'Sqlite',
264-
'Files',
265263
'Apc',
266264
'Apcu',
265+
'Couchbase',
266+
'Devnull',
267+
'Files',
268+
'Leveldb',
267269
'Memcache',
268270
'Memcached',
269-
'Couchbase',
271+
'Memstatic',
270272
'Mongodb',
271273
'Predis',
272274
'Redis',
273275
'Ssdb',
274-
'Leveldb',
276+
'Sqlite',
275277
'Wincache',
276278
'Xcache',
277-
'Devnull',
278279
];
279280
}
280281

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
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+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
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\Item\ExtendedCacheItemInterface;
18+
use phpFastCache\Core\Pool\ExtendedCacheItemPoolInterface;
19+
use phpFastCache\Core\Item\ItemBaseTrait;
20+
use phpFastCache\Drivers\Memstatic\Driver as MemstaticDriver;
21+
22+
/**
23+
* Class Item
24+
* @package phpFastCache\Drivers\Devnull
25+
*/
26+
class Item implements ExtendedCacheItemInterface
27+
{
28+
use ItemBaseTrait;
29+
30+
/**
31+
* Item constructor.
32+
* @param \phpFastCache\Drivers\Memstatic\Driver $driver
33+
* @param $key
34+
* @throws \InvalidArgumentException
35+
*/
36+
public function __construct(MemstaticDriver $driver, $key)
37+
{
38+
if (is_string($key)) {
39+
$this->key = $key;
40+
$this->driver = $driver;
41+
$this->driver->setItem($this);
42+
$this->expirationDate = new \DateTime();
43+
} else {
44+
throw new \InvalidArgumentException(sprintf('$key must be a string, got type "%s" instead.', gettype($key)));
45+
}
46+
}
47+
48+
/**
49+
* @param ExtendedCacheItemPoolInterface $driver
50+
* @throws \InvalidArgumentException
51+
* @return static
52+
*/
53+
public function setDriver(ExtendedCacheItemPoolInterface $driver)
54+
{
55+
if ($driver instanceof MemstaticDriver) {
56+
$this->driver = $driver;
57+
58+
return $this;
59+
} else {
60+
throw new \InvalidArgumentException('Invalid driver instance');
61+
}
62+
}
63+
}

tests/Memstatic.test.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
/**
4+
* @author Khoa Bui (khoaofgod) <[email protected]> http://www.phpfastcache.com
5+
* @author Georges.L (Geolim4) <[email protected]>
6+
*/
7+
use phpFastCache\CacheManager;
8+
use phpFastCache\Core\Item\ExtendedCacheItemInterface;
9+
use phpFastCache\Core\Pool\ExtendedCacheItemPoolInterface;
10+
use phpFastCache\EventManager;
11+
12+
13+
chdir(__DIR__);
14+
require_once __DIR__ . '/../vendor/autoload.php';
15+
16+
$status = 0;
17+
echo "Testing Memstatic driver\n";
18+
19+
$cacheInstance = CacheManager::getInstance('Memstatic');
20+
$cacheKey = 'testItem';
21+
$randomStr = str_shuffle(sha1(uniqid('pfc', true) . mt_rand(100, 10000)));
22+
echo "Random-generated cache value for key '{$cacheKey}': {$randomStr}\n";
23+
24+
25+
$item = $cacheInstance->getItem($cacheKey);
26+
$item->set($randomStr)->expiresAfter(60);
27+
$cacheInstance->save($item);
28+
$cacheInstance->detachAllItems();
29+
unset($item);
30+
31+
$item = $cacheInstance->getItem($cacheKey);
32+
33+
$cacheResult = $cacheInstance->getItem($cacheKey)->get();
34+
35+
if($cacheResult === $randomStr){
36+
echo "[PASS] The cache key value match, got expected value '{$cacheResult}'\n";
37+
}else{
38+
echo "[FAIL] The cache key value match expected value '{$randomStr}', got '{$cacheResult}'\n";
39+
$status = 1;
40+
}
41+
echo "Clearing the whole cache to test item cleaning...\n";
42+
$cacheInstance->clear();
43+
$cacheResult = ($cacheInstance->getItem($cacheKey)->isHit() === false && $cacheInstance->getItem($cacheKey)->get() === null);
44+
45+
if($cacheResult === true){
46+
echo "[PASS] The cache item is null as expected\n";
47+
}else{
48+
echo "[FAIL] The cache is not null'\n";
49+
$status = 1;
50+
}
51+
52+
exit($status);

0 commit comments

Comments
 (0)