|
1
|
|
|
<?php |
|
2
|
|
|
namespace BehEh\Flaps\Storage; |
|
3
|
|
|
|
|
4
|
|
|
use BehEh\Flaps\StorageInterface; |
|
5
|
|
|
use Doctrine\Common\Cache\Cache; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Provides a storage adapter using a Doctrine\Common\Cache\Cache implementation as backend. |
|
9
|
|
|
* |
|
10
|
|
|
* Example using Doctrine\Common\Cache\ApcCache: |
|
11
|
|
|
* <pre><code> |
|
12
|
|
|
* <?php |
|
13
|
|
|
* use Doctrine\Common\Cache\ApcCache; |
|
14
|
|
|
* use BehEh\Flaps\Storage\DoctrineCacheAdapter; |
|
15
|
|
|
* |
|
16
|
|
|
* $apc = new ApcCache(); |
|
17
|
|
|
* $apc->setNamespace('MyApplication'); |
|
18
|
|
|
* $storage = new DoctrineCacheAdapter($apc); |
|
19
|
|
|
* </pre></code> |
|
20
|
|
|
* |
|
21
|
|
|
* @since 0.1 |
|
22
|
|
|
* @author Benedict Etzel <[email protected]> |
|
23
|
|
|
*/ |
|
24
|
|
|
class DoctrineCacheAdapter implements StorageInterface |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* @var Cache |
|
28
|
|
|
*/ |
|
29
|
|
|
protected $cache; |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* Sets up the adapter using the Doctrine cache implementation $cache. |
|
33
|
|
|
* @param Doctrine\Common\Cache\Cache $cache the cache implementation to use as storage backend |
|
34
|
|
|
*/ |
|
35
|
|
|
public function __construct(Cache $cache) |
|
36
|
|
|
{ |
|
37
|
|
|
$this->cache = $cache; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
1 |
|
public function setValue($key, $value) |
|
41
|
|
|
{ |
|
42
|
1 |
|
$this->cache->save($key, intval($value)); |
|
43
|
1 |
|
} |
|
44
|
|
|
|
|
45
|
1 |
|
public function incrementValue($key) |
|
46
|
|
|
{ |
|
47
|
1 |
|
throw new \Exception('incrementValue() is not implemented for DoctrineCacheAdapter'); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
1 |
|
public function getValue($key) |
|
51
|
|
|
{ |
|
52
|
1 |
|
if (!$this->cache->contains($key)) { |
|
53
|
1 |
|
return 0; |
|
54
|
|
|
} |
|
55
|
1 |
|
return intval($this->cache->fetch($key)); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
1 |
|
public function setTimestamp($key, $timestamp) |
|
59
|
|
|
{ |
|
60
|
1 |
|
$this->cache->save($key.':timestamp', floatval($timestamp)); |
|
61
|
1 |
|
} |
|
62
|
|
|
|
|
63
|
1 |
|
public function getTimestamp($key) |
|
64
|
|
|
{ |
|
65
|
1 |
|
if (!$this->cache->contains($key.':timestamp')) { |
|
66
|
1 |
|
return (float) 0; |
|
67
|
|
|
} |
|
68
|
1 |
|
return floatval($this->cache->fetch($key.':timestamp')); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
3 |
|
public function expire($key) |
|
72
|
|
|
{ |
|
73
|
3 |
|
$this->cache->delete($key.':timestamp'); |
|
74
|
3 |
|
return $this->cache->delete($key); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @codeCoverageIgnore |
|
79
|
|
|
*/ |
|
80
|
|
|
public function expireIn($key, $seconds) |
|
81
|
|
|
{ |
|
82
|
|
|
return false; |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|