Skip to content

Commit 7eca2eb

Browse files
committed
Added Redis cache engine.
1 parent 4286dc5 commit 7eca2eb

File tree

4 files changed

+162
-0
lines changed

4 files changed

+162
-0
lines changed

.travis.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,12 @@ language: php
22

33
services:
44
- memcached
5+
- redis-server
56

67
addons:
78
hosts:
89
- memcached-container
10+
- redis-container
911

1012
php:
1113
- "7.0"

src/Engine/RedisCacheEngine.php

Lines changed: 146 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,146 @@
1+
<?php
2+
3+
namespace ByJG\Cache\Engine;
4+
5+
use ByJG\Cache\CacheEngineInterface;
6+
use Memcached;
7+
use Psr\Log\NullLogger;
8+
9+
class RedisCacheEngine implements CacheEngineInterface
10+
{
11+
12+
/**
13+
*
14+
* @var \Redis
15+
*/
16+
protected $redis = null;
17+
18+
protected $logger = null;
19+
20+
protected $server = null;
21+
22+
protected $password = null;
23+
24+
public function __construct($server = null, $password = null, $logger = null)
25+
{
26+
$this->server = $server;
27+
if (is_null($server)) {
28+
$this->server = '127.0.0.1:6379';
29+
}
30+
31+
$this->password = $password;
32+
33+
$this->logger = $logger;
34+
if (is_null($logger)) {
35+
$this->logger = new NullLogger();
36+
}
37+
}
38+
39+
protected function lazyLoadRedisServer()
40+
{
41+
if (is_null($this->redis)) {
42+
$this->redis = new \Redis();
43+
$data = explode(":", $this->server);
44+
$this->redis->connect($data[0], isset($data[1]) ? $data[1] : 6379);
45+
46+
if (!empty($this->password)) {
47+
$this->redis->auth($this->password);
48+
}
49+
$this->redis->setOption(\Redis::OPT_SERIALIZER, \Redis::SERIALIZER_NONE);
50+
51+
$this->redis->info('redis_version');
52+
}
53+
}
54+
55+
/**
56+
* @param string $key The object KEY
57+
* @param int $ttl IGNORED IN MEMCACHED.
58+
* @return object Description
59+
*/
60+
public function get($key, $ttl = 0)
61+
{
62+
$this->lazyLoadRedisServer();
63+
64+
$value = $this->redis->get($key);
65+
$this->logger->info("[Redis Cache] Get '$key' result ");
66+
67+
return ($value === false ? null : $value);
68+
}
69+
70+
/**
71+
* @param string $key The object Key
72+
* @param object $object The object to be cached
73+
* @param int $ttl The time to live in seconds of this objects
74+
* @return bool If the object is successfully posted
75+
*/
76+
public function set($key, $object, $ttl = 0)
77+
{
78+
$this->lazyLoadRedisServer();
79+
80+
$this->redis->set($key, $object, $ttl);
81+
$this->logger->info("[Redis Cache] Set '$key' result ");
82+
83+
return true;
84+
}
85+
86+
/**
87+
* Unlock resource
88+
* @param string $key
89+
*/
90+
public function release($key)
91+
{
92+
$this->lazyLoadRedisServer();
93+
94+
$this->redis->delete($key);
95+
}
96+
97+
/**
98+
*
99+
* @param string $key
100+
* @param string $str
101+
* @return bool
102+
*/
103+
public function append($key, $str)
104+
{
105+
$this->lazyLoadRedisServer();
106+
107+
$this->logger->info("[Redis Cache] Append '$key' in Memcached");
108+
return $this->redis->append($key, $str);
109+
}
110+
111+
/**
112+
* Lock resource before set it.
113+
* @param string $key
114+
*/
115+
public function lock($key)
116+
{
117+
$this->lazyLoadRedisServer();
118+
119+
return;
120+
}
121+
122+
/**
123+
* UnLock resource after set it
124+
* @param string $key
125+
*/
126+
public function unlock($key)
127+
{
128+
$this->lazyLoadRedisServer();
129+
130+
return;
131+
}
132+
133+
public function isAvailable()
134+
{
135+
if (!class_exists('\Redis')) {
136+
return false;
137+
}
138+
139+
try {
140+
$this->lazyLoadRedisServer();
141+
return true;
142+
} catch (\Exception $ex) {
143+
return false;
144+
}
145+
}
146+
}

src/Factory.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use ByJG\Cache\Engine\FileSystemCacheEngine;
77
use ByJG\Cache\Engine\MemcachedEngine;
88
use ByJG\Cache\Engine\NoCacheEngine;
9+
use ByJG\Cache\Engine\RedisCacheEngine;
910
use ByJG\Cache\Engine\SessionCacheEngine;
1011
use ByJG\Cache\Engine\ShmopCacheEngine;
1112
use ByJG\Cache\Psr\CachePool;
@@ -59,4 +60,12 @@ public static function createMemcachedPool($servers = null, $bufferSize = null,
5960
);
6061
}
6162

63+
public static function createRedisCacheEngine($servers = null, $password = null, $bufferSize = null, $logger = null)
64+
{
65+
return new CachePool(
66+
new RedisCacheEngine($servers, $password, $logger),
67+
$bufferSize
68+
);
69+
}
70+
6271
}

tests/CachePoolTest.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ protected function tearDown()
2525
public function CachePoolProvider()
2626
{
2727
$memcachedServer = ['memcached-container:11211'];
28+
$redisCacheServer = 'redis-container:6379';
29+
$redisPassword = '';
2830

2931
return [
3032
[
@@ -44,6 +46,9 @@ public function CachePoolProvider()
4446
],
4547
[
4648
new CachePool(new \ByJG\Cache\Engine\MemcachedEngine($memcachedServer))
49+
],
50+
[
51+
new CachePool(new \ByJG\Cache\Engine\RedisCacheEngine($redisCacheServer, $redisPassword))
4752
]
4853
];
4954
}

0 commit comments

Comments
 (0)