Skip to content

Commit 050796d

Browse files
authored
Merge pull request #340 from Hammermaps-DEV/final
Add Drivers for Zend Data Cache
2 parents d7128a1 + 488ed65 commit 050796d

File tree

10 files changed

+525
-13
lines changed

10 files changed

+525
-13
lines changed

README.md

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,17 @@ One Class uses for All Cache. You don't need to rewrite your code many times aga
1111

1212
### Supported drivers at this day *
1313

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

2526
\* Driver descriptions available in DOCS/DRIVERS.md
2627

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
22
"name": "phpfastcache/phpfastcache",
33
"type" : "library",
4-
"description": "PHP Cache Class - Reduce your database call using cache system. PhpFastCache handles a lot of drivers such as Apc(u), CouchBase, Mongodb, Files, (P)redis, Leveldb, Memcache(d), Ssdb, Sqlite, Wincache, Xcache.",
5-
"keywords": ["cache","caching","php cache","mysql cache","apc","apcu","memcache","memcached","wincache","files cache","pdo cache","cache class","redis","predis","cookie", "mongodb", "couchbase", "LevelDb", "Ssdb", "Wincache", "xcache"],
4+
"description": "PHP Cache Class - Reduce your database call using cache system. PhpFastCache handles a lot of drivers such as Apc(u), CouchBase, Mongodb, Files, (P)redis, Leveldb, Memcache(d), Ssdb, Sqlite, Wincache, Xcache, Zend Data Cache.",
5+
"keywords": ["cache","caching","php cache","mysql cache","apc","apcu","memcache","memcached","wincache","files cache","pdo cache","cache class","redis","predis","cookie", "mongodb", "couchbase", "LevelDb", "Ssdb", "Wincache", "xcache","zend","zend disk cache","zend memory cache","zend data cache","zend server"],
66
"homepage": "http://www.phpfastcache.com",
77
"license": "MIT",
88
"authors": [

docs/DRIVERS.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,8 @@
3434
* Wincache
3535
* The Wincache driver. A memory cache for regular performances on Windows platforms.
3636
* Xcache
37-
* The Xcache driver. A memory cache for regular performances.
37+
* The Xcache driver. A memory cache for regular performances.
38+
* Zend Disk Cache ( * Requires ZendServer Version 4 or higher * )
39+
* The Zend Data Cache is a by ZendServer supported file cache. The cache is for regular performance.
40+
* Zend Memory Cache ( * Requires ZendServer Version 4 or higher * )
41+
* The Zend Memory Cache is a by ZendServer supported memory cache. The cache is for high-performance applications.

examples/zend_disk.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
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 Lucas Brucksch <[email protected]>
11+
*
12+
*/
13+
// Include composer autoloader
14+
require __DIR__ . '/../vendor/autoload.php';
15+
// OR require_once("../src/phpFastCache/phpFastCache.php");
16+
date_default_timezone_set("Europe/Paris");
17+
18+
19+
use phpFastCache\CacheManager;
20+
use phpFastCache\Core\phpFastCache;
21+
22+
// In your class, function, you can call the Cache
23+
$InstanceCache = CacheManager::getInstance('zenddisk');
24+
// OR $InstanceCache = CacheManager::getInstance() <-- open examples/global.setup.php to see more
25+
26+
/**
27+
* Try to get $products from Caching First
28+
* product_page is "identity keyword";
29+
*/
30+
$key = "product_page";
31+
$CachedString = $InstanceCache->getItem($key);
32+
33+
if (is_null($CachedString->get())) {
34+
//$CachedString = "Zend Disk Cache --> Cache Enabled --> Well done !";
35+
// Write products to Cache in 10 minutes with same keyword
36+
$CachedString->set("Zend Disk Cache --> Cache Enabled --> Well done !");
37+
$InstanceCache->save($CachedString);
38+
39+
echo "FIRST LOAD // WROTE OBJECT TO CACHE // RELOAD THE PAGE AND SEE // ";
40+
echo $CachedString->get();
41+
42+
} else {
43+
echo "READ FROM CACHE // ";
44+
echo $CachedString->getExpirationDate()->format(Datetime::W3C);
45+
echo $CachedString->get();
46+
}
47+
48+
echo '<br /><br /><a href="/">Back to index</a>&nbsp;--&nbsp;<a href="./' . basename(__FILE__) . '">Reload</a>';

examples/zend_shm.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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 Lucas Brucksch <[email protected]>
11+
*
12+
*/
13+
use phpFastCache\CacheManager;
14+
15+
// Include composer autoloader
16+
require __DIR__ . '/../vendor/autoload.php';
17+
18+
$InstanceCache = CacheManager::getInstance('zendshm');
19+
20+
/**
21+
* Try to get $products from Caching First
22+
* product_page is "identity keyword";
23+
*/
24+
$key = "product_page";
25+
$CachedString = $InstanceCache->getItem($key);
26+
27+
if (is_null($CachedString->get())) {
28+
//$CachedString = "Zend Memory Cache --> Cache Enabled --> Well done !";
29+
// Write products to Cache in 10 minutes with same keyword
30+
$CachedString->set("Zend Memory Cache --> Cache Enabled --> Well done !")->expiresAfter(5);
31+
$InstanceCache->save($CachedString);
32+
33+
echo "FIRST LOAD // WROTE OBJECT TO CACHE // RELOAD THE PAGE AND SEE // ";
34+
echo $CachedString->get();
35+
36+
} else {
37+
echo "READ FROM CACHE // ";
38+
echo $CachedString->get();
39+
}
40+
41+
echo '<br /><br /><a href="/">Back to index</a>&nbsp;--&nbsp;<a href="./' . basename(__FILE__) . '">Reload</a>';

src/phpFastCache/CacheManager.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737
* @method static ExtendedCacheItemPoolInterface Ssdb() Ssdb($config = []) Return a driver "ssdb" instance
3838
* @method static ExtendedCacheItemPoolInterface Wincache() Wincache($config = []) Return a driver "wincache" instance
3939
* @method static ExtendedCacheItemPoolInterface Xcache() Xcache($config = []) Return a driver "xcache" instance
40+
* @method static ExtendedCacheItemPoolInterface Zenddisk() Zenddisk($config = []) Return a driver "zend disk cache" instance
41+
* @method static ExtendedCacheItemPoolInterface Zendshm() Zendshm($config = []) Return a driver "zend memory cache" instance
4042
*
4143
*/
4244
class CacheManager
@@ -235,6 +237,8 @@ public static function getStaticSystemDrivers()
235237
'Leveldb',
236238
'Wincache',
237239
'Xcache',
240+
'Zenddisk',
241+
'Zendshm',
238242
'Devnull',
239243
];
240244
}
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
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 Lucas Brucksch <[email protected]>
11+
*
12+
*/
13+
14+
namespace phpFastCache\Drivers\Zenddisk;
15+
16+
use phpFastCache\Core\DriverAbstract;
17+
use phpFastCache\Core\StandardPsr6StructureTrait;
18+
use phpFastCache\Entities\driverStatistic;
19+
use phpFastCache\Exceptions\phpFastCacheDriverCheckException;
20+
use phpFastCache\Exceptions\phpFastCacheDriverException;
21+
use Psr\Cache\CacheItemInterface;
22+
23+
/**
24+
* Class Driver (zend disk cache)
25+
* Requires Zend Data Cache Functions from ZendServer
26+
* @package phpFastCache\Drivers
27+
*/
28+
class Driver extends DriverAbstract
29+
{
30+
/**
31+
* Driver constructor.
32+
* @param array $config
33+
* @throws phpFastCacheDriverException
34+
*/
35+
public function __construct(array $config = [])
36+
{
37+
$this->setup($config);
38+
39+
if (!$this->driverCheck()) {
40+
throw new phpFastCacheDriverCheckException(sprintf(self::DRIVER_CHECK_FAILURE, $this->getDriverName()));
41+
}
42+
}
43+
44+
/**
45+
* @return bool
46+
*/
47+
public function driverCheck()
48+
{
49+
if (extension_loaded('Zend Data Cache') && function_exists('zend_disk_cache_store')) {
50+
return true;
51+
} else {
52+
return false;
53+
}
54+
}
55+
56+
/**
57+
* @param \Psr\Cache\CacheItemInterface $item
58+
* @return mixed
59+
* @throws \InvalidArgumentException
60+
*/
61+
protected function driverWrite(CacheItemInterface $item)
62+
{
63+
/**
64+
* Check for Cross-Driver type confusion
65+
*/
66+
if ($item instanceof Item) {
67+
$ttl = $item->getExpirationDate()->getTimestamp() - time();
68+
69+
return zend_disk_cache_store($item->getKey(), $this->driverPreWrap($item), ($ttl > 0 ? $ttl : 0));
70+
} else {
71+
throw new \InvalidArgumentException('Cross-Driver type confusion detected');
72+
}
73+
}
74+
75+
/**
76+
* @param \Psr\Cache\CacheItemInterface $item
77+
* @return mixed
78+
*/
79+
protected function driverRead(CacheItemInterface $item)
80+
{
81+
$data = zend_disk_cache_fetch($item->getKey());
82+
if ($data === false) {
83+
return null;
84+
}
85+
86+
return $data;
87+
}
88+
89+
/**
90+
* @param \Psr\Cache\CacheItemInterface $item
91+
* @return bool
92+
* @throws \InvalidArgumentException
93+
*/
94+
protected function driverDelete(CacheItemInterface $item)
95+
{
96+
/**
97+
* Check for Cross-Driver type confusion
98+
*/
99+
if ($item instanceof Item) {
100+
return zend_disk_cache_delete($item->getKey());
101+
} else {
102+
throw new \InvalidArgumentException('Cross-Driver type confusion detected');
103+
}
104+
}
105+
106+
/**
107+
* @return bool
108+
*/
109+
protected function driverClear()
110+
{
111+
return @zend_disk_cache_clear();
112+
}
113+
114+
/**
115+
* @return bool
116+
*/
117+
protected function driverConnect()
118+
{
119+
return true;
120+
}
121+
122+
/********************
123+
*
124+
* PSR-6 Extended Methods
125+
*
126+
*******************/
127+
128+
/**
129+
* @return driverStatistic
130+
*/
131+
public function getStats()
132+
{
133+
$stat = new driverStatistic();
134+
$stat->setInfo('[ZendDisk] A void info string')
135+
->setSize(0)
136+
->setData(implode(', ', array_keys($this->itemInstances)))
137+
->setRawData(false);
138+
139+
return $stat;
140+
}
141+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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 Lucas Brucksch <[email protected]>
11+
*
12+
*/
13+
14+
namespace phpFastCache\Drivers\Zenddisk;
15+
16+
use phpFastCache\Cache\ExtendedCacheItemInterface;
17+
use phpFastCache\Cache\ExtendedCacheItemPoolInterface;
18+
use phpFastCache\Cache\ItemBaseTrait;
19+
use phpFastCache\Drivers\Zenddisk\Driver as ZendDiskDriver;
20+
21+
/**
22+
* Class Item
23+
* @package phpFastCache\Drivers\Zenddisk
24+
*/
25+
class Item implements ExtendedCacheItemInterface
26+
{
27+
use ItemBaseTrait;
28+
29+
/**
30+
* Item constructor.
31+
* @param \phpFastCache\Drivers\Zenddisk\Driver $driver
32+
* @param $key
33+
* @throws \InvalidArgumentException
34+
*/
35+
public function __construct(ZendDiskDriver $driver, $key)
36+
{
37+
if (is_string($key)) {
38+
$this->key = $key;
39+
$this->driver = $driver;
40+
$this->driver->setItem($this);
41+
$this->expirationDate = new \DateTime();
42+
} else {
43+
throw new \InvalidArgumentException(sprintf('$key must be a string, got type "%s" instead.', gettype($key)));
44+
}
45+
}
46+
47+
/**
48+
* @param ExtendedCacheItemPoolInterface $driver
49+
* @throws \InvalidArgumentException
50+
* @return static
51+
*/
52+
public function setDriver(ExtendedCacheItemPoolInterface $driver)
53+
{
54+
if ($driver instanceof ZendDiskDriver) {
55+
$this->driver = $driver;
56+
57+
return $this;
58+
} else {
59+
throw new \InvalidArgumentException('Invalid driver instance');
60+
}
61+
}
62+
}

0 commit comments

Comments
 (0)