1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* AppserverIo\Storage\MemcachedStorage |
5
|
|
|
* |
6
|
|
|
* NOTICE OF LICENSE |
7
|
|
|
* |
8
|
|
|
* This source file is subject to the Open Software License (OSL 3.0) |
9
|
|
|
* that is available through the world-wide-web at this URL: |
10
|
|
|
* http://opensource.org/licenses/osl-3.0.php |
11
|
|
|
* |
12
|
|
|
* PHP version 5 |
13
|
|
|
* |
14
|
|
|
* @author Tim Wagner <[email protected]> |
15
|
|
|
* @copyright 2015 TechDivision GmbH <[email protected]> |
16
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
17
|
|
|
* @link http://github.com/appserver-io/storage |
18
|
|
|
* @link http://www.appserver.io |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
namespace AppserverIo\Storage; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* A Memcached storage implementation. |
25
|
|
|
* |
26
|
|
|
* @author Tim Wagner <[email protected]> |
27
|
|
|
* @copyright 2015 TechDivision GmbH <[email protected]> |
28
|
|
|
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0) |
29
|
|
|
* @link http://github.com/appserver-io/storage |
30
|
|
|
* @link http://www.appserver.io |
31
|
|
|
*/ |
32
|
|
|
class MemcachedStorage extends AbstractStorage implements MemcachedStorageInterface |
33
|
|
|
{ |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Array that contains servers the storage is bound to. |
37
|
|
|
* |
38
|
|
|
* @var array |
39
|
|
|
*/ |
40
|
|
|
protected $servers = array(); |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Adds an server to the internal list with servers this storage |
44
|
|
|
* is bound to, used by MemcachedStorage for example. |
45
|
|
|
* |
46
|
|
|
* @param string $host The server host |
47
|
|
|
* @param integer $port The server port |
48
|
|
|
* @param integer $weight The weight the server has |
49
|
|
|
* |
50
|
|
|
* @return void |
51
|
|
|
* @see \AppserverIo\Storage\StorageInterface::addServer() |
52
|
|
|
*/ |
53
|
|
|
public function addServer($host, $port, $weight) |
54
|
|
|
{ |
55
|
|
|
$this->servers[] = array($host, $port, $weight); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Returns the list with servers this storage is bound to. |
60
|
|
|
* |
61
|
|
|
* @return array The server list |
62
|
|
|
* @see \AppserverIo\Storage\StorageInterface::getServers() |
63
|
|
|
*/ |
64
|
|
|
public function getServers() |
65
|
|
|
{ |
66
|
|
|
return $this->servers; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* (non-PHPdoc) |
71
|
|
|
* |
72
|
|
|
* @return void |
73
|
|
|
* @see AppserverIo\Storage\AbstractStorage::init(); |
74
|
|
|
*/ |
75
|
|
|
public function init() |
76
|
|
|
{ |
77
|
|
|
// inject the \Memcached storage |
78
|
|
|
$this->injectStorage(new \Memcached(__CLASS__)); |
79
|
|
|
// initialize the storage |
80
|
|
|
$serverList = $this->storage->getServerList(); |
81
|
|
|
if (empty($serverList)) { |
82
|
|
|
$this->storage->setOption(\Memcached::OPT_LIBKETAMA_COMPATIBLE, true); |
83
|
|
|
foreach ($this->getServers() as $server) { |
84
|
|
|
list ($host, $port, $weight) = $server; |
85
|
|
|
$this->storage->addServer($host, $port, $weight); |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* (non-PHPdoc) |
92
|
|
|
* |
93
|
|
|
* @param string $entryIdentifier Something which identifies the data - depends on concrete cache |
94
|
|
|
* @param mixed $data The data to cache - also depends on the concrete cache implementation |
95
|
|
|
* @param array $tags Tags to associate with this cache entry |
96
|
|
|
* @param integer $lifetime Lifetime of this cache entry in seconds. If NULL is specified, |
97
|
|
|
* the default lifetime is used. "0" means unlimited lifetime. |
98
|
|
|
* |
99
|
|
|
* @return void |
100
|
|
|
* @see \AppserverIo\Storage\StorageInterface::set() |
101
|
|
|
*/ |
102
|
|
|
public function set($entryIdentifier, $data, array $tags = array(), $lifetime = null) |
103
|
|
|
{ |
104
|
|
|
// add the passed value to the storage |
105
|
|
|
$this->storage->set($entryIdentifier, $data, $lifetime); |
106
|
|
|
|
107
|
|
|
// if tags has been set, tag the data additionally |
108
|
|
|
foreach ($tags as $tag) { |
109
|
|
|
$tagData = $this->get($tag); |
110
|
|
|
if (is_array($tagData) && in_array($entryIdentifier, $tagData, true) === true) { |
111
|
|
|
// do nothing here |
112
|
|
|
} elseif (is_array($tagData) && in_array($entryIdentifier, $tagData, true) === false) { |
113
|
|
|
$tagData[] = $entryIdentifier; |
114
|
|
|
} else { |
115
|
|
|
$tagData = array($entryIdentifier); |
116
|
|
|
} |
117
|
|
|
$this->storage->set($tag, $tagData); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* (non-PHPdoc) |
123
|
|
|
* |
124
|
|
|
* @param string $entryIdentifier Something which identifies the cache entry - depends on concrete cache |
125
|
|
|
* |
126
|
|
|
* @return mixed |
127
|
|
|
* @see \AppserverIo\Storage\StorageInterface::get() |
128
|
|
|
*/ |
129
|
|
|
public function get($entryIdentifier) |
130
|
|
|
{ |
131
|
|
|
return $this->storage->get($entryIdentifier); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* (non-PHPdoc) |
136
|
|
|
* |
137
|
|
|
* @param string $entryIdentifier An identifier specifying the cache entry |
138
|
|
|
* |
139
|
|
|
* @return boolean TRUE if such an entry exists, FALSE if not |
140
|
|
|
* @see \AppserverIo\Storage\StorageInterface::remove() |
141
|
|
|
*/ |
142
|
|
|
public function remove($entryIdentifier) |
143
|
|
|
{ |
144
|
|
|
$this->storage->delete($entryIdentifier); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* (non-PHPdoc) |
149
|
|
|
* |
150
|
|
|
* @return array |
151
|
|
|
* @see \AppserverIo\Storage\StorageInterface::getAllKeys() |
152
|
|
|
*/ |
153
|
|
|
public function getAllKeys() |
154
|
|
|
{ |
155
|
|
|
$this->storage->getAllKeys(); |
156
|
|
|
} |
157
|
|
|
} |
158
|
|
|
|