- Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathCache.php
More file actions
Latest commit
150 lines (133 loc) · 3.57 KB
/
Copy pathCache.php
File metadata and controls
150 lines (133 loc) · 3.57 KB
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
namespaceBeryllium\CacheBundle;
useBeryllium\CacheBundle\CacheInterface;
useBeryllium\CacheBundle\CacheClientInterface;
/**
* Cache
*
* @uses CacheInterface
* @package
* @version $id$
* @author Kevin Boyd <beryllium@beryllium.ca>
* @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
*/
publicfunction__construct(CacheClientInterface$client = null)
{
if (!empty($client)) {
if (is_object($client) && ($clientinstanceof CacheClientInterface)) {
$this->client = $client;
}
else {
thrownew \Exception('Invalid Cache Client Interface');
}
}
}
/**
* Inject a dependency injection container (optional)
*
* @param mixed $dic The container
* @access public
* @return void
*/
publicfunctionsetContainer($dic)
{
$this->dic = $dic;
}
/**
* Change the default lifetime of the data (default: 300 seconds - five minutes)
*
* @param int $ttl
* @access public
* @return void
*/
publicfunctionsetTtl($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
*/
publicfunctionsetClient(CacheClientInterface$client)
{
if (is_object($client) && ($clientinstanceof CacheClientInterface))
$this->client = $client;
else {
thrownew \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
*/
publicfunctionget($key)
{
if ($this->isSafe() && !empty($key)) {
return$this->client->get($key);
}
returnfalse;
}
/**
* 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.
*/
publicfunctionset($key, $value, $ttl = null)
{
$ttl = (null !== $ttl) ? $ttl : $this->ttl;
if ($this->isSafe() && !empty($key)) {
return$this->client->set($key, $value, $ttl);
}
returnfalse;
}
/**
* Delete a key from the cache
*
* @param string $key Unique key
* @access public
* @return void
*/
publicfunctiondelete($key)
{
if ($this->isSafe() && !empty($key)) {
return$this->client->delete($key);
}
returnfalse;
}
/**
* Checks if the cache is in a usable state
*
* @access public
* @return boolean True if the cache is usable, otherwise false
*/
publicfunctionisSafe()
{
if ($this->clientinstanceof CacheClientInterface) {
return$this->client->isSafe();
}
return$this->safe;
}
}