-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathMemcacheCache.php
140 lines (125 loc) · 3.49 KB
/
MemcacheCache.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
<?php
namespace Luracast\Restler;
/**
* Class MemcacheCache provides a memcache based cache for Restler
*
* @category Framework
* @package Restler
* @author Dave Drager <[email protected]>
* @copyright 2014 Luracast
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link http://luracast.com/products/restler/
* @version 3.0.0rc5
*/
class MemcacheCache implements iCache
{
/**
* The namespace that all of the cached entries will be stored under. This allows multiple APIs to run concurrently.
*
* @var string
*/
static public $namespace;
/**
* @var string the memcache server hostname / IP address. For the memcache
* cache method.
*/
static public $memcacheServer = '127.0.0.1';
/**
* @var int the memcache server port. For the memcache cache method.
*/
static public $memcachePort = 11211;
private $memcache;
/**
* @param string $namespace
*/
function __construct($namespace = 'restler')
{
self::$namespace = $namespace;
if (function_exists('memcache_connect')) {
$this->memcache = new \Memcache;
$this->memcache->connect(self::$memcacheServer, self::$memcachePort);
} else {
$this->memcacheNotAvailable('Memcache is not available for use as Restler Cache. Please make sure the the memcache php extension is installed.');
}
}
/**
* store data in the cache
*
*
* @param string $name
* @param mixed $data
*
* @return boolean true if successful
*/
public function set($name, $data)
{
function_exists('memcache_set') || $this->memcacheNotAvailable();
try {
return $this->memcache->set(self::$namespace . "-" . $name, $data);
} catch
(\Exception $exception) {
return false;
}
}
private function memcacheNotAvailable($message = 'Memcache is not available.')
{
throw new \Exception($message);
}
/**
* retrieve data from the cache
*
*
* @param string $name
* @param bool $ignoreErrors
*
* @throws \Exception
* @return mixed
*/
public function get($name, $ignoreErrors = false)
{
function_exists('memcache_get') || $this->memcacheNotAvailable();
try {
return $this->memcache->get(self::$namespace . "-" . $name);
} catch (\Exception $exception) {
if (!$ignoreErrors) {
throw $exception;
}
return null;
}
}
/**
* delete data from the cache
*
*
* @param string $name
* @param bool $ignoreErrors
*
* @throws \Exception
* @return boolean true if successful
*/
public function clear($name, $ignoreErrors = false)
{
function_exists('memcache_delete') || $this->memcacheNotAvailable();
try {
$this->memcache->delete(self::$namespace . "-" . $name);
} catch (\Exception $exception) {
if (!$ignoreErrors) {
throw $exception;
}
}
}
/**
* check if the given name is cached
*
*
* @param string $name
*
* @return boolean true if cached
*/
public function isCached($name)
{
function_exists('memcache_get') || $this->memcacheNotAvailable();
$data = $this->memcache->get(self::$namespace . "-" . $name);
return !empty($data);
}
}