Skip to content

Commit 2ca6e1b

Browse files
committed
Pridan CacheAdapter pro pouziti nette/caching jako backendu pro CacheDecorator
1 parent 1fc295a commit 2ca6e1b

File tree

3 files changed

+163
-1
lines changed

3 files changed

+163
-1
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,8 @@
4545
"tracy/tracy": "~2.1@dev"
4646
},
4747
"suggest": {
48-
"tracy/tracy": "to enable skautis panel for Tracy debug bar."
48+
"tracy/tracy": "to enable skautis panel for Tracy debug bar.",
49+
"nette/caching": "to use Nette caching backend in CacheDecorator."
4950
},
5051
"autoload": {
5152
"psr-4": {

src/CacheAdapter.php

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
<?php
2+
3+
namespace Skautis\Nette;
4+
5+
use Nette;
6+
use Nette\Caching\Cache;
7+
use Nette\Caching\IStorage;
8+
use Skautis\Wsdl\Decorator\Cache\CacheInterface;
9+
10+
11+
if (!class_exists('Nette\Utils\DateTime') && class_exists('Nette\DateTime')) {
12+
// BC with Nette 2.1
13+
class_alias('Nette\DateTime', 'Nette\Utils\DateTime');
14+
}
15+
16+
17+
/**
18+
* Nette cache adapter for Skautis library
19+
*/
20+
class CacheAdapter implements CacheInterface
21+
{
22+
23+
/** @var IStorage */
24+
private $storage;
25+
26+
/** @var string */
27+
private $namespace;
28+
29+
/** @var string|int|\DateTime */
30+
private $expiration;
31+
32+
33+
/**
34+
* @param IStorage $storage
35+
* @param string|null $namespace
36+
*/
37+
public function __construct(IStorage $storage, $namespace = NULL)
38+
{
39+
$this->storage = $storage;
40+
$this->namespace = $namespace . Cache::NAMESPACE_SEPARATOR;
41+
}
42+
43+
44+
/**
45+
* Returns cache storage.
46+
* @return IStorage
47+
*/
48+
public function getStorage()
49+
{
50+
return $this->storage;
51+
}
52+
53+
54+
/**
55+
* Returns cache namespace.
56+
* @return string
57+
*/
58+
public function getNamespace()
59+
{
60+
return (string) substr($this->namespace, 0, -1);
61+
}
62+
63+
64+
/**
65+
* @param \DateTime|int|string $expiration
66+
* @return self
67+
*/
68+
public function setExpiration($expiration)
69+
{
70+
$this->expiration = $expiration;
71+
return $this;
72+
}
73+
74+
75+
/**
76+
* @inheritdoc
77+
*/
78+
public function get($key)
79+
{
80+
return $this->storage->read($this->generateKey($key));
81+
}
82+
83+
84+
/**
85+
* @inheritdoc
86+
*/
87+
public function set($key, $data)
88+
{
89+
$key = $this->generateKey($key);
90+
91+
if ($data === NULL) {
92+
$this->storage->remove($key);
93+
} else {
94+
$this->storage->write($key, $data, $this->getDependencies());
95+
}
96+
}
97+
98+
99+
/**
100+
* @return array
101+
*/
102+
private function getDependencies()
103+
{
104+
$dependencies = array();
105+
106+
if (isset($this->expiration)) {
107+
$dependencies[Cache::EXPIRATION] = Nette\Utils\DateTime::from($this->expiration)->format('U') - time();
108+
}
109+
110+
return $dependencies;
111+
}
112+
113+
114+
/**
115+
* Generates internal cache key.
116+
*
117+
* @param string $key
118+
* @return string
119+
*/
120+
protected function generateKey($key)
121+
{
122+
return $this->namespace . md5(is_scalar($key) ? $key : serialize($key));
123+
}
124+
125+
126+
/**
127+
* Clears all cached items.
128+
*/
129+
public function clean()
130+
{
131+
$this->storage->clean(array(Cache::ALL => TRUE));
132+
}
133+
134+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
use Tester\Assert;
4+
use Nette\Http;
5+
use Skautis\Nette\CacheAdapter;
6+
7+
8+
require __DIR__ . '/../bootstrap.php';
9+
10+
$storage = new \Nette\Caching\Storages\FileStorage(TEMP_DIR);
11+
$cache = new CacheAdapter($storage, 'skautis');
12+
13+
Assert::equal('skautis', $cache->getNamespace());
14+
Assert::equal($storage, $cache->getStorage());
15+
16+
Assert::equal(NULL, $cache->get('unknown'));
17+
18+
$cache->set('key', 'value');
19+
Assert::equal('value', $cache->get('key'));
20+
21+
$cache->clean();
22+
Assert::equal(NULL, $cache->get('key'));
23+
24+
$cache->setExpiration('1 second');
25+
$cache->set('key', 'value');
26+
sleep(2);
27+
Assert::equal(NULL, $cache->get('key'));

0 commit comments

Comments
 (0)