1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of Jitamin. |
5
|
|
|
* |
6
|
|
|
* Copyright (C) Jitamin Team |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Jitamin\Foundation\Cache; |
13
|
|
|
|
14
|
|
|
use Carbon\Carbon; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Memcached Cache Driver. |
18
|
|
|
*/ |
19
|
|
|
class MemcachedCache extends BaseCache |
20
|
|
|
{ |
21
|
|
|
/** |
22
|
|
|
* The Memcached instance. |
23
|
|
|
* |
24
|
|
|
* @var \Memcached |
25
|
|
|
*/ |
26
|
|
|
protected $memcached; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* A string that should be prepended to keys. |
30
|
|
|
* |
31
|
|
|
* @var string |
32
|
|
|
*/ |
33
|
|
|
protected $prefix; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Container. |
37
|
|
|
* |
38
|
|
|
* @var array |
39
|
|
|
*/ |
40
|
|
|
private $storage = []; |
|
|
|
|
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Create a new Memcached store. |
44
|
|
|
* |
45
|
|
|
* @param \Memcached $memcached |
46
|
|
|
* @param string $prefix |
47
|
|
|
* |
48
|
|
|
* @return void |
|
|
|
|
49
|
|
|
*/ |
50
|
|
|
public function __construct($memcached, $prefix = '') |
51
|
|
|
{ |
52
|
|
|
$this->setPrefix($prefix); |
53
|
|
|
$this->memcached = $memcached; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Store an item in the cache. |
58
|
|
|
* |
59
|
|
|
* @param string $key |
60
|
|
|
* @param mixed $value |
61
|
|
|
* @param int $minutes |
62
|
|
|
*/ |
63
|
|
|
public function set($key, $value, $minutes = 0) |
64
|
|
|
{ |
65
|
|
|
$this->memcached->set($this->prefix.$key, $value, $this->toTimestamp($minutes)); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Retrieve an item from the cache by key. |
70
|
|
|
* |
71
|
|
|
* @param string $key |
72
|
|
|
* |
73
|
|
|
* @return mixed Null when not found, cached value otherwise |
74
|
|
|
*/ |
75
|
|
|
public function get($key) |
76
|
|
|
{ |
77
|
|
|
$value = $this->memcached->get($this->prefix.$key); |
78
|
|
|
if ($this->memcached->getResultCode() == 0) { |
79
|
|
|
return $value; |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Clear all cache. |
85
|
|
|
*/ |
86
|
|
|
public function flush() |
87
|
|
|
{ |
88
|
|
|
$this->memcached->flush(); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Remove cached value. |
93
|
|
|
* |
94
|
|
|
* @param string $key |
95
|
|
|
*/ |
96
|
|
|
public function remove($key) |
97
|
|
|
{ |
98
|
|
|
return $this->memcached->delete($this->prefix.$key); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* Get the UNIX timestamp for the given number of minutes. |
103
|
|
|
* |
104
|
|
|
* @parma int $minutes |
105
|
|
|
* |
106
|
|
|
* @return int |
107
|
|
|
*/ |
108
|
|
|
protected function toTimestamp($minutes) |
109
|
|
|
{ |
110
|
|
|
return $minutes > 0 ? Carbon::now()->addMinutes($minutes)->getTimestamp() : 0; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Get the cache key prefix. |
115
|
|
|
* |
116
|
|
|
* @return string |
117
|
|
|
*/ |
118
|
|
|
public function getPrefix() |
119
|
|
|
{ |
120
|
|
|
return $this->prefix; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Set the cache key prefix. |
125
|
|
|
* |
126
|
|
|
* @param string $prefix |
127
|
|
|
* |
128
|
|
|
* @return void |
129
|
|
|
*/ |
130
|
|
|
public function setPrefix($prefix) |
131
|
|
|
{ |
132
|
|
|
$this->prefix = !empty($prefix) ? $prefix.':' : ''; |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|
This check marks private properties in classes that are never used. Those properties can be removed.