-
Notifications
You must be signed in to change notification settings - Fork 21
/
Cache.php
150 lines (133 loc) · 3.57 KB
/
Cache.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?php
namespace Beryllium\CacheBundle;
use Beryllium\CacheBundle\CacheInterface;
use Beryllium\CacheBundle\CacheClientInterface;
/**
* Cache
*
* @uses CacheInterface
* @package
* @version $id$
* @author Kevin Boyd <[email protected]>
* @license See LICENSE.md
*/
class Cache implements CacheInterface
{
public $dic = false;
protected $client = null;
protected $safe = false;
protected $ttl = 300;
/**
* Prep the cache
*
* @param CacheClientInterface $client Optional cache object/service
* @access public
* @return void
*/
public function __construct(CacheClientInterface $client = null)
{
if (!empty($client)) {
if (is_object($client) && ($client instanceof CacheClientInterface)) {
$this->client = $client;
}
else {
throw new \Exception('Invalid Cache Client Interface');
}
}
}
/**
* Inject a dependency injection container (optional)
*
* @param mixed $dic The container
* @access public
* @return void
*/
public function setContainer($dic)
{
$this->dic = $dic;
}
/**
* Change the default lifetime of the data (default: 300 seconds - five minutes)
*
* @param int $ttl
* @access public
* @return void
*/
public function setTtl($ttl)
{
$this->ttl = $ttl;
}
/**
* Inject a cache client interface to interact with a custom cache service
*
* @param CacheClientInterface $client The client object or service
* @access public
* @return void
*/
public function setClient(CacheClientInterface $client)
{
if (is_object($client) && ($client instanceof CacheClientInterface))
$this->client = $client;
else {
throw new \Exception('Invalid Cache Client Interface');
}
}
/**
* Retrieve a value from the cache using the provided key
*
* @param string|array $key The unique key or array of keys identifying the data to be retrieved.
* @access public
* @return mixed The requested data, or false if there is an error
*/
public function get($key)
{
if ($this->isSafe() && !empty($key)) {
return $this->client->get($key);
}
return false;
}
/**
* Add a key/value to the cache
*
* @param string $key A unique key to identify the data you want to store
* @param string $value The value you want to store in the cache
* @param int $ttl Optional: Lifetime of the data
* @access public
* @return mixed Whatever the CacheClientObject returns, or false.
*/
public function set($key, $value, $ttl = null)
{
$ttl = (null !== $ttl) ? $ttl : $this->ttl;
if ($this->isSafe() && !empty($key)) {
return $this->client->set($key, $value, $ttl);
}
return false;
}
/**
* Delete a key from the cache
*
* @param string $key Unique key
* @access public
* @return void
*/
public function delete($key)
{
if ($this->isSafe() && !empty($key)) {
return $this->client->delete($key);
}
return false;
}
/**
* Checks if the cache is in a usable state
*
* @access public
* @return boolean True if the cache is usable, otherwise false
*/
public function isSafe()
{
if ($this->client instanceof CacheClientInterface) {
return $this->client->isSafe();
}
return $this->safe;
}
}