|
1
|
|
|
<?php |
|
2
|
|
|
namespace BehEh\Flaps\Storage; |
|
3
|
|
|
|
|
4
|
|
|
use BehEh\Flaps\StorageInterface; |
|
5
|
|
|
use Predis\Client; |
|
6
|
|
|
|
|
7
|
|
|
/** |
|
8
|
|
|
* Provides a storage implementation using Predis\Client as backend. |
|
9
|
|
|
* |
|
10
|
|
|
* Example: |
|
11
|
|
|
* <pre><code> |
|
12
|
|
|
* <?php |
|
13
|
|
|
* use Predis\Client; |
|
14
|
|
|
* use BehEh\Flaps\Storage\PredisStorage; |
|
15
|
|
|
* |
|
16
|
|
|
* $storage = new PredisStorage(new Client('tcp://10.0.0.1:6379')); |
|
17
|
|
|
* </code></pre> |
|
18
|
|
|
* |
|
19
|
|
|
* @since 0.1 |
|
20
|
|
|
* @author Benedict Etzel <[email protected]> |
|
21
|
|
|
*/ |
|
22
|
|
|
class PredisStorage implements StorageInterface |
|
23
|
|
|
{ |
|
24
|
|
|
/** |
|
25
|
|
|
* @var Client |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $client; |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* Sets up the class using the redis instance connected to the predis $client. |
|
31
|
|
|
* @param Predis\Client $client |
|
32
|
|
|
* @param array $options an array of options |
|
33
|
|
|
* @see BehEh\Flaps\PredisStorage::configure |
|
34
|
|
|
*/ |
|
35
|
|
|
public function __construct(Client $client, array $options = array()) |
|
36
|
|
|
{ |
|
37
|
|
|
$this->client = $client; |
|
38
|
|
|
$this->configure($options); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
/** |
|
42
|
|
|
* @var array |
|
43
|
|
|
*/ |
|
44
|
|
|
protected $options; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Configures this class with some options: |
|
48
|
|
|
* <table> |
|
49
|
|
|
* <tr><th>prefix</th><td>the prefix to apply to all unique keys</td></tr> |
|
50
|
|
|
* </table> |
|
51
|
|
|
* @param array $options the key value pairs of options for this class |
|
52
|
|
|
*/ |
|
53
|
|
|
public function configure(array $options) |
|
54
|
|
|
{ |
|
55
|
|
|
$this->options = array_merge(array( |
|
56
|
|
|
'prefix' => 'flaps:' |
|
57
|
|
|
), $options); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
private function prefixKey($key) |
|
61
|
|
|
{ |
|
62
|
|
|
return $this->options['prefix'].$key; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
private function prefixTimestamp($timestamp) |
|
66
|
|
|
{ |
|
67
|
|
|
return $this->prefixKey($timestamp.':timestamp'); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
1 |
|
public function setValue($key, $value) |
|
71
|
|
|
{ |
|
72
|
1 |
|
$this->client->set($this->prefixKey($key), intval($value)); |
|
73
|
1 |
|
} |
|
74
|
|
|
|
|
75
|
1 |
|
public function incrementValue($key) |
|
76
|
|
|
{ |
|
77
|
1 |
|
return intval($this->client->incr($this->prefixKey($key))); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
1 |
|
public function getValue($key) |
|
81
|
|
|
{ |
|
82
|
1 |
|
return intval($this->client->get($this->prefixKey($key))); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
1 |
|
public function setTimestamp($key, $timestamp) |
|
86
|
|
|
{ |
|
87
|
1 |
|
$this->client->set($this->prefixTimestamp($key), floatval($timestamp)); |
|
88
|
1 |
|
} |
|
89
|
|
|
|
|
90
|
1 |
|
public function getTimestamp($key) |
|
91
|
|
|
{ |
|
92
|
1 |
|
return floatval($this->client->get($this->prefixTimestamp($key))); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
3 |
|
public function expire($key) |
|
96
|
|
|
{ |
|
97
|
3 |
|
$this->client->del($this->prefixTimestamp($key)); |
|
98
|
3 |
|
return (int) $this->client->del($this->prefixKey($key)) === 1; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
1 |
|
public function expireIn($key, $seconds) |
|
102
|
|
|
{ |
|
103
|
1 |
|
$redisTime = $this->client->time(); |
|
104
|
1 |
|
$at = ceil($redisTime[0] + $seconds); |
|
105
|
1 |
|
$this->client->expireat($this->prefixTimestamp($key), $at); |
|
106
|
1 |
|
return (int) $this->client->expireat($this->prefixKey($key), $at) === 1; |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
|