Skip to content

Commit 67675a5

Browse files
committed
Add basic Redis support for caching.
1 parent 331fb71 commit 67675a5

File tree

5 files changed

+184
-1
lines changed

5 files changed

+184
-1
lines changed

Sources/ManageServer.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -984,7 +984,7 @@ function saveSettings(&$config_vars)
984984
'webmaster_email',
985985
'db_name', 'db_user', 'db_server', 'db_prefix', 'ssi_db_user',
986986
'boarddir', 'sourcedir',
987-
'cachedir', 'cachedir_sqlite', 'cache_accelerator', 'cache_memcached',
987+
'cachedir', 'cachedir_sqlite', 'cache_accelerator', 'cache_memcached', 'cache_redis',
988988
'image_proxy_secret',
989989
);
990990

Sources/StoryBB/Cache/Redis.php

+170
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
<?php
2+
3+
/**
4+
* This class provides connections to Redis for short-term caching
5+
*
6+
* @package StoryBB (storybb.org) - A roleplayer's forum software
7+
* @copyright 2018 StoryBB and individual contributors (see contributors.txt)
8+
* @license 3-clause BSD (see accompanying LICENSE file)
9+
*
10+
* @version 3.0 Alpha 1
11+
*/
12+
13+
namespace StoryBB\Cache;
14+
15+
/**
16+
* Our Cache API class
17+
* @package cacheAPI
18+
*/
19+
class Redis extends API
20+
{
21+
/**
22+
* @var \Redis The memcache instance.
23+
*/
24+
private $redis = null;
25+
26+
/**
27+
* Checks whether we can use the cache method performed by this API.
28+
*
29+
* @param boolean $test Test if this is supported or enabled.
30+
* @return boolean Whether or not the cache is supported
31+
*/
32+
public function isSupported($test = false)
33+
{
34+
global $cache_redis;
35+
36+
$supported = class_exists('Redis');
37+
38+
if ($test)
39+
return $supported;
40+
return parent::isSupported() && $supported && !empty($cache_redis);
41+
}
42+
43+
/**
44+
* Connects to the cache method. This defines our $key. If this fails, we return false, otherwise we return true.
45+
*
46+
* @return boolean Whether or not the cache method was connected to.
47+
*/
48+
public function connect()
49+
{
50+
global $cache_redis;
51+
52+
switch (substr_count($cache_redis, ':'))
53+
{
54+
case 0:
55+
default:
56+
return false;
57+
58+
case 1:
59+
list ($host, $port) = explode(':', $cache_redis);
60+
$password = null;
61+
break;
62+
63+
case 2:
64+
list ($host, $port, $password) = explode(':', $cache_redis, 3);
65+
break;
66+
}
67+
68+
$this->redis = new \Redis;
69+
if (empty($port))
70+
{
71+
$port = 6379;
72+
}
73+
74+
// Do the initial connection.
75+
$connected = $this->redis->connect($host, $port);
76+
if (!$connected)
77+
{
78+
return false;
79+
}
80+
81+
// If we have a password, use it.
82+
if (!empty($password))
83+
{
84+
$connected = $this->redis->auth($password);
85+
}
86+
87+
if ($connected)
88+
{
89+
$this->redis->setOption(\Redis::OPT_PREFIX, $this->prefix . ':');
90+
$this->redis->setOption(\Redis::OPT_SERIALIZER, \Redis::SERIALIZER_PHP);
91+
}
92+
93+
return $connected;
94+
}
95+
96+
/**
97+
* Returns the name for the cache method performed by this API. Likely to be a brand of sorts.
98+
*
99+
* @return string The name of the cache backend
100+
*/
101+
public function getName()
102+
{
103+
return 'Redis';
104+
}
105+
106+
/**
107+
* Gets data from the cache.
108+
*
109+
* @param string $key The key to use, the prefix is applied to the key name.
110+
* @param string $ttl Overrides the default TTL.
111+
* @return mixed The result from the cache, if there is no data or it is invalid, we return null.
112+
*/
113+
public function getData($key, $ttl = null)
114+
{
115+
$value = $this->redis->get($key);
116+
117+
// $value should return either data or false (from failure, key not found or empty array).
118+
if ($value === false)
119+
return null;
120+
return $value;
121+
}
122+
123+
/**
124+
* Saves to data the cache.
125+
*
126+
* @param string $key The key to use, the prefix is applied to the key name.
127+
* @param mixed $value The data we wish to save.
128+
* @param string $ttl Overrides the default TTL.
129+
* @return bool Whether or not we could save this to the cache.
130+
*/
131+
public function putData($key, $value, $ttl = null)
132+
{
133+
return $this->redis->set($key, $value, $ttl ? $ttl : $this->getDefaultTTL());
134+
}
135+
136+
/**
137+
* Closes connections to the cache method.
138+
*
139+
* @return bool Whether or not we could close connections.
140+
*/
141+
public function quit()
142+
{
143+
return $this->redis->close();
144+
}
145+
146+
/**
147+
* Clean out the cache.
148+
*
149+
* @param string $type If supported, the type of cache to clear, blank/data or user.
150+
* @return bool Whether or not we could clean the cache.
151+
*/
152+
public function cleanCache($type = '')
153+
{
154+
return $this->redis->flushDb();
155+
}
156+
157+
/**
158+
* Specify custom settings that the cache API supports.
159+
*
160+
* @param array $config_vars Additional config_vars, see ManageSettings.php for usage.
161+
* @return void No return is needed.
162+
*/
163+
public function cacheSettings(array &$config_vars)
164+
{
165+
global $context, $txt;
166+
167+
$config_vars[] = $txt['cache_redis_settings'];
168+
$config_vars[] = array('cache_redis', $txt['cache_redis_server'], 'file', 'text', 0, 'cache_redis', 'postinput' => '<br /><div class="smalltext"><em>' . $txt['cache_redis_server_subtext'] . '</em></div>');
169+
}
170+
}

Themes/default/languages/ManageSettings.english.php

+3
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,9 @@
117117
$txt['cache_memcache_settings'] = 'Memcache(d) settings';
118118
$txt['cache_memcache_servers'] = 'Memcache(d) servers';
119119
$txt['cache_memcache_servers_subtext'] = 'Example: 127.0.0.1:11211,127.0.0.2';
120+
$txt['cache_redis_settings'] = 'Redis settings';
121+
$txt['cache_redis_server'] = 'Redis server';
122+
$txt['cache_redis_server_subtext'] = 'Example: 127.0.0.1:6379 or 127.0.0.1:6379:password if a password is needed';
120123
$txt['cache_xcache_settings'] = 'XCache settings';
121124
$txt['cache_xcache_adminuser'] = 'XCache Admin User';
122125
$txt['cache_xcache_adminpass'] = 'XCache Admin Password';

other/Settings.php

+5
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,11 @@
126126
* @var array
127127
*/
128128
$cache_memcached = '';
129+
/**
130+
* This is only used for Redis. Should be: server:port:password
131+
* @var string
132+
*/
133+
$cache_redis = '';
129134
/**
130135
* This is only for the file cache system. It is the path to the cache directory.
131136
* It is also recommended that you place this in /tmp/ if you are going to use this.

other/Settings_behat.php

+5
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,11 @@
126126
* @var array
127127
*/
128128
$cache_memcached = '';
129+
/**
130+
* This is only used for Redis. Should be: server:port:password
131+
* @var string
132+
*/
133+
$cache_redis = '';
129134
/**
130135
* This is only for the file cache system. It is the path to the cache directory.
131136
* It is also recommended that you place this in /tmp/ if you are going to use this.

0 commit comments

Comments
 (0)