4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\HttpKernel\Tests\Profiler\Mock;
15 * MemcachedMock for simulating Memcached extension in tests.
17 * @author Andrej Hudec <pulzarraider@gmail.com>
21 private $connected = false;
22 private $storage = array();
25 * Set a Memcached option.
32 public function setOption($option, $value)
38 * Add a memcached server to connection pool.
46 public function addServer($host, $port = 11211, $weight = 0)
48 if ('127.0.0.1' == $host && 11211 == $port) {
49 $this->connected = true;
58 * Add an item to the server only if such key doesn't exist at the server yet.
62 * @param int $expiration
66 public function add($key, $value, $expiration = 0)
68 if (!$this->connected) {
72 if (!isset($this->storage[$key])) {
73 $this->storeData($key, $value);
82 * Store data at the server.
86 * @param int $expiration
90 public function set($key, $value, $expiration = null)
92 if (!$this->connected) {
96 $this->storeData($key, $value);
102 * Replace value of the existing item.
105 * @param mixed $value
106 * @param int $expiration
110 public function replace($key, $value, $expiration = null)
112 if (!$this->connected) {
116 if (isset($this->storage[$key])) {
117 $this->storeData($key, $value);
126 * Retrieve item from the server.
129 * @param callable $cache_cb
130 * @param float $cas_token
134 public function get($key, $cache_cb = null, &$cas_token = null)
136 if (!$this->connected) {
140 return $this->getData($key);
144 * Append data to an existing item.
147 * @param string $value
151 public function append($key, $value)
153 if (!$this->connected) {
157 if (isset($this->storage[$key])) {
158 $this->storeData($key, $this->getData($key).$value);
167 * Delete item from the server.
173 public function delete($key)
175 if (!$this->connected) {
179 if (isset($this->storage[$key])) {
180 unset($this->storage[$key]);
189 * Flush all existing items at the server.
193 public function flush()
195 if (!$this->connected) {
199 $this->storage = array();
204 private function getData($key)
206 if (isset($this->storage[$key])) {
207 return unserialize($this->storage[$key]);
213 private function storeData($key, $value)
215 $this->storage[$key] = serialize($value);