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 * MemcacheMock for simulating Memcache extension in tests.
17 * @author Andrej Hudec <pulzarraider@gmail.com>
21 private $connected = false;
22 private $storage = array();
25 * Open memcached server connection.
33 public function connect($host, $port = null, $timeout = null)
35 if ('127.0.0.1' == $host && 11211 == $port) {
36 $this->connected = true;
45 * Open memcached server persistent connection.
53 public function pconnect($host, $port = null, $timeout = null)
55 if ('127.0.0.1' == $host && 11211 == $port) {
56 $this->connected = true;
65 * Add a memcached server to connection pool.
69 * @param bool $persistent
72 * @param int $retry_interval
74 * @param callable $failure_callback
75 * @param int $timeoutms
79 public function addServer($host, $port = 11211, $persistent = null, $weight = null, $timeout = null, $retry_interval = null, $status = null, $failure_callback = null, $timeoutms = null)
81 if ('127.0.0.1' == $host && 11211 == $port) {
82 $this->connected = true;
91 * Add an item to the server only if such key doesn't exist at the server yet.
100 public function add($key, $var, $flag = null, $expire = null)
102 if (!$this->connected) {
106 if (!isset($this->storage[$key])) {
107 $this->storeData($key, $var);
116 * Store data at the server.
125 public function set($key, $var, $flag = null, $expire = null)
127 if (!$this->connected) {
131 $this->storeData($key, $var);
137 * Replace value of the existing item.
146 public function replace($key, $var, $flag = null, $expire = null)
148 if (!$this->connected) {
152 if (isset($this->storage[$key])) {
153 $this->storeData($key, $var);
162 * Retrieve item from the server.
164 * @param string|array $key
165 * @param int|array $flags
169 public function get($key, &$flags = null)
171 if (!$this->connected) {
175 if (is_array($key)) {
177 foreach ($key as $k) {
178 if (isset($this->storage[$k])) {
179 $result[] = $this->getData($k);
186 return $this->getData($key);
190 * Delete item from the server.
196 public function delete($key)
198 if (!$this->connected) {
202 if (isset($this->storage[$key])) {
203 unset($this->storage[$key]);
212 * Flush all existing items at the server.
216 public function flush()
218 if (!$this->connected) {
222 $this->storage = array();
228 * Close memcached server connection.
232 public function close()
234 $this->connected = false;
239 private function getData($key)
241 if (isset($this->storage[$key])) {
242 return unserialize($this->storage[$key]);
248 private function storeData($key, $value)
250 $this->storage[$key] = serialize($value);