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 * RedisMock for simulating Redis extension in tests.
17 * @author Andrej Hudec <pulzarraider@gmail.com>
21 private $connected = false;
22 private $storage = array();
25 * Add a server to connection pool.
29 * @param float $timeout
33 public function connect($host, $port = 6379, $timeout = 0)
35 if ('127.0.0.1' == $host && 6379 == $port) {
36 $this->connected = true;
52 public function setOption($name, $value)
54 if (!$this->connected) {
62 * Verify if the specified key exists.
68 public function exists($key)
70 if (!$this->connected) {
74 return isset($this->storage[$key]);
78 * Store data at the server with expiration time.
86 public function setex($key, $ttl, $value)
88 if (!$this->connected) {
92 $this->storeData($key, $value);
98 * Sets an expiration time on an item.
105 public function setTimeout($key, $ttl)
107 if (!$this->connected) {
111 if (isset($this->storage[$key])) {
119 * Retrieve item from the server.
125 public function get($key)
127 if (!$this->connected) {
131 return $this->getData($key);
135 * Append data to an existing item.
138 * @param string $value
140 * @return int Size of the value after the append
142 public function append($key, $value)
144 if (!$this->connected) {
148 if (isset($this->storage[$key])) {
149 $this->storeData($key, $this->getData($key).$value);
151 return strlen($this->storage[$key]);
158 * Remove specified keys.
160 * @param string|array $key
164 public function delete($key)
166 if (!$this->connected) {
170 if (is_array($key)) {
172 foreach ($key as $k) {
173 if (isset($this->storage[$k])) {
174 unset($this->storage[$k]);
182 if (isset($this->storage[$key])) {
183 unset($this->storage[$key]);
192 * Flush all existing items from all databases at the server.
196 public function flushAll()
198 if (!$this->connected) {
202 $this->storage = array();
208 * Close Redis server connection.
212 public function close()
214 $this->connected = false;
219 private function getData($key)
221 if (isset($this->storage[$key])) {
222 return unserialize($this->storage[$key]);
228 private function storeData($key, $value)
230 $this->storage[$key] = serialize($value);
235 public function select($dbnum)
237 if (!$this->connected) {