-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMemcacheStore.php
More file actions
116 lines (99 loc) · 2.36 KB
/
MemcacheStore.php
File metadata and controls
116 lines (99 loc) · 2.36 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
<?php
/**
* Memcache Store. This store is deprecated. Use Memchached store instead.
*
* @package SugiPHP.Cache
* @subpackage Cache
* @author Plamen Popov <tzappa@gmail.com>
* @license http://opensource.org/licenses/mit-license.php (MIT License)
*/
namespace SugiPHP\Cache;
use Memcache;
class MemcacheStore implements StoreInterface
{
/**
* Memcache instance
*/
protected $memcache;
protected $connected = false;
/**
* Creates a Memcache store
*
* @param Memcache $memcache
*/
public function __construct(Memcache $memcache)
{
$this->memcache = $memcache;
}
/**
* Creates MemcacheStore instance.
*
* @param array $config Server Configurations
*
* @return MemcacheStore
*/
public static function factory(array $config = array())
{
$memcache = new Memcache();
$host = empty($config["host"]) ? "127.0.0.1" : $config["host"];
$port = empty($config["port"]) ? 11211 : $config["port"];
$connected = $memcache->connect($host, $port);
// The code using a store should work no matter if the store is running or not
// Check is the memcache store is working with checkRunning() method
$store = new MemcacheStore($memcache);
$store->connected = $connected;
return $store;
}
/**
* {@inheritdoc}
*/
public function add($key, $value, $ttl = 0)
{
return $this->memcache->add($key, $value, 0, $ttl);
}
/**
* {@inheritdoc}
*/
public function set($key, $value, $ttl = 0)
{
return $this->memcache->set($key, $value, 0, $ttl);
}
/**
* {@inheritdoc}
*/
public function get($key)
{
$result = $this->memcache->get($key);
return ($result === false) ? null : $result;
}
/**
* {@inheritdoc}
*/
public function has($key)
{
return (!is_null($this->memcache->get($key)));
}
/**
* {@inheritdoc}
*/
public function delete($key)
{
$this->memcache->delete($key);
}
/**
* {@inheritdoc}
*/
public function flush()
{
$this->memcache->flush();
}
/**
* Checks is the memcache server is running
*
* @return boolean
*/
public function checkRunning()
{
return $this->connected;
}
}