1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Author: Nil Portugués Calderó <[email protected]> |
4
|
|
|
* Date: 4/3/15 |
5
|
|
|
* Time: 6:01 PM |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace NilPortugues\Cache\Adapter\Redis; |
12
|
|
|
|
13
|
|
|
use NilPortugues\Cache\Adapter\Adapter; |
14
|
|
|
use NilPortugues\Cache\Adapter\InMemoryAdapter; |
15
|
|
|
use NilPortugues\Cache\CacheAdapter; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* Class AbstractAdapter |
19
|
|
|
* @package NilPortugues\Cache\Adapter\Redis |
20
|
|
|
*/ |
21
|
|
|
abstract class AbstractAdapter extends Adapter implements CacheAdapter |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* Redis client instance |
25
|
|
|
* |
26
|
|
|
* @var \Redis|\Predis\Client |
27
|
|
|
*/ |
28
|
|
|
protected $redis; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var bool |
32
|
|
|
*/ |
33
|
|
|
protected $connected; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var InMemoryAdapter |
37
|
|
|
*/ |
38
|
|
|
protected $inMemoryAdapter; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @param array $connections |
42
|
|
|
* @param CacheAdapter $next |
43
|
|
|
*/ |
44
|
|
|
abstract public function __construct(array $connections, CacheAdapter $next = null); |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Get a value identified by $key. |
48
|
|
|
* |
49
|
|
|
* @param string $key |
50
|
|
|
* |
51
|
|
|
* @return bool|mixed |
52
|
|
|
*/ |
53
|
|
View Code Duplication |
public function get($key) |
|
|
|
|
54
|
|
|
{ |
55
|
|
|
$this->hit = false; |
56
|
|
|
|
57
|
|
|
$inMemoryValue = InMemoryAdapter::getInstance()->get($key); |
58
|
|
|
|
59
|
|
|
if (InMemoryAdapter::getInstance()->isHit()) { |
60
|
|
|
$this->hit = true; |
61
|
|
|
return $inMemoryValue; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
if ($this->isAvailable()) { |
65
|
|
|
$value = $this->redis->get($key); |
66
|
|
|
|
67
|
|
|
if ($value) { |
68
|
|
|
$this->hit = true; |
69
|
|
|
$value = $this->restoreDataStructure($value); |
70
|
|
|
InMemoryAdapter::getInstance()->set($key, $value, 0); |
71
|
|
|
return $value; |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
return (null !== $this->nextAdapter) ? $this->nextAdapter->get($key) : null; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Set a value identified by $key and with an optional $ttl. |
81
|
|
|
* |
82
|
|
|
* @param string $key |
83
|
|
|
* @param mixed $value |
84
|
|
|
* @param int $ttl |
85
|
|
|
* |
86
|
|
|
* @return $this |
87
|
|
|
*/ |
88
|
|
|
public function set($key, $value, $ttl = 0) |
89
|
|
|
{ |
90
|
|
|
$ttl = $this->fromDefaultTtl($ttl); |
91
|
|
|
|
92
|
|
|
if ($ttl >= 0) { |
93
|
|
|
if ($this->isAvailable()) { |
94
|
|
|
$this->redis->set($key, $this->storageDataStructure($value)); |
95
|
|
|
|
96
|
|
|
if ($ttl > 0) { |
97
|
|
|
$this->redis->expire($key, $ttl); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
$this->setChain($key, $value, $ttl); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
return $this; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Delete a value identified by $key. |
110
|
|
|
* |
111
|
|
|
* @param string $key |
112
|
|
|
*/ |
113
|
|
|
public function delete($key) |
114
|
|
|
{ |
115
|
|
|
$this->redis->del($key); |
116
|
|
|
$this->deleteChain($key); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Clears all expired values from cache. |
121
|
|
|
* |
122
|
|
|
* @return mixed |
123
|
|
|
*/ |
124
|
|
|
public function clear() |
125
|
|
|
{ |
126
|
|
|
$this->clearChain(); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Clears all values from the cache. |
131
|
|
|
* |
132
|
|
|
* @return mixed |
133
|
|
|
*/ |
134
|
|
|
public function drop() |
135
|
|
|
{ |
136
|
|
|
$this->redis->flushDB(); |
137
|
|
|
$this->dropChain(); |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.