Skip to content

Commit

Permalink
Add basic Redis support for caching.
Browse files Browse the repository at this point in the history
  • Loading branch information
Arantor committed Aug 4, 2018
1 parent 331fb71 commit 67675a5
Show file tree
Hide file tree
Showing 5 changed files with 184 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Sources/ManageServer.php
Expand Up @@ -984,7 +984,7 @@ function saveSettings(&$config_vars)
'webmaster_email',
'db_name', 'db_user', 'db_server', 'db_prefix', 'ssi_db_user',
'boarddir', 'sourcedir',
'cachedir', 'cachedir_sqlite', 'cache_accelerator', 'cache_memcached',
'cachedir', 'cachedir_sqlite', 'cache_accelerator', 'cache_memcached', 'cache_redis',
'image_proxy_secret',
);

Expand Down
170 changes: 170 additions & 0 deletions Sources/StoryBB/Cache/Redis.php
@@ -0,0 +1,170 @@
<?php

/**
* This class provides connections to Redis for short-term caching
*
* @package StoryBB (storybb.org) - A roleplayer's forum software
* @copyright 2018 StoryBB and individual contributors (see contributors.txt)
* @license 3-clause BSD (see accompanying LICENSE file)
*
* @version 3.0 Alpha 1
*/

namespace StoryBB\Cache;

/**
* Our Cache API class
* @package cacheAPI
*/
class Redis extends API
{
/**
* @var \Redis The memcache instance.
*/
private $redis = null;

/**
* Checks whether we can use the cache method performed by this API.
*
* @param boolean $test Test if this is supported or enabled.
* @return boolean Whether or not the cache is supported
*/
public function isSupported($test = false)
{
global $cache_redis;

$supported = class_exists('Redis');

if ($test)
return $supported;
return parent::isSupported() && $supported && !empty($cache_redis);
}

/**
* Connects to the cache method. This defines our $key. If this fails, we return false, otherwise we return true.
*
* @return boolean Whether or not the cache method was connected to.
*/
public function connect()
{
global $cache_redis;

switch (substr_count($cache_redis, ':'))
{
case 0:
default:
return false;

case 1:
list ($host, $port) = explode(':', $cache_redis);
$password = null;
break;

case 2:
list ($host, $port, $password) = explode(':', $cache_redis, 3);
break;
}

$this->redis = new \Redis;
if (empty($port))
{
$port = 6379;
}

// Do the initial connection.
$connected = $this->redis->connect($host, $port);
if (!$connected)
{
return false;
}

// If we have a password, use it.
if (!empty($password))
{
$connected = $this->redis->auth($password);
}

if ($connected)
{
$this->redis->setOption(\Redis::OPT_PREFIX, $this->prefix . ':');
$this->redis->setOption(\Redis::OPT_SERIALIZER, \Redis::SERIALIZER_PHP);
}

return $connected;
}

/**
* Returns the name for the cache method performed by this API. Likely to be a brand of sorts.
*
* @return string The name of the cache backend
*/
public function getName()
{
return 'Redis';
}

/**
* Gets data from the cache.
*
* @param string $key The key to use, the prefix is applied to the key name.
* @param string $ttl Overrides the default TTL.
* @return mixed The result from the cache, if there is no data or it is invalid, we return null.
*/
public function getData($key, $ttl = null)
{
$value = $this->redis->get($key);

// $value should return either data or false (from failure, key not found or empty array).
if ($value === false)
return null;
return $value;
}

/**
* Saves to data the cache.
*
* @param string $key The key to use, the prefix is applied to the key name.
* @param mixed $value The data we wish to save.
* @param string $ttl Overrides the default TTL.
* @return bool Whether or not we could save this to the cache.
*/
public function putData($key, $value, $ttl = null)
{
return $this->redis->set($key, $value, $ttl ? $ttl : $this->getDefaultTTL());
}

/**
* Closes connections to the cache method.
*
* @return bool Whether or not we could close connections.
*/
public function quit()
{
return $this->redis->close();
}

/**
* Clean out the cache.
*
* @param string $type If supported, the type of cache to clear, blank/data or user.
* @return bool Whether or not we could clean the cache.
*/
public function cleanCache($type = '')
{
return $this->redis->flushDb();
}

/**
* Specify custom settings that the cache API supports.
*
* @param array $config_vars Additional config_vars, see ManageSettings.php for usage.
* @return void No return is needed.
*/
public function cacheSettings(array &$config_vars)
{
global $context, $txt;

$config_vars[] = $txt['cache_redis_settings'];
$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>');
}
}
3 changes: 3 additions & 0 deletions Themes/default/languages/ManageSettings.english.php
Expand Up @@ -117,6 +117,9 @@
$txt['cache_memcache_settings'] = 'Memcache(d) settings';
$txt['cache_memcache_servers'] = 'Memcache(d) servers';
$txt['cache_memcache_servers_subtext'] = 'Example: 127.0.0.1:11211,127.0.0.2';
$txt['cache_redis_settings'] = 'Redis settings';
$txt['cache_redis_server'] = 'Redis server';
$txt['cache_redis_server_subtext'] = 'Example: 127.0.0.1:6379 or 127.0.0.1:6379:password if a password is needed';
$txt['cache_xcache_settings'] = 'XCache settings';
$txt['cache_xcache_adminuser'] = 'XCache Admin User';
$txt['cache_xcache_adminpass'] = 'XCache Admin Password';
Expand Down
5 changes: 5 additions & 0 deletions other/Settings.php
Expand Up @@ -126,6 +126,11 @@
* @var array
*/
$cache_memcached = '';
/**
* This is only used for Redis. Should be: server:port:password
* @var string
*/
$cache_redis = '';
/**
* This is only for the file cache system. It is the path to the cache directory.
* It is also recommended that you place this in /tmp/ if you are going to use this.
Expand Down
5 changes: 5 additions & 0 deletions other/Settings_behat.php
Expand Up @@ -126,6 +126,11 @@
* @var array
*/
$cache_memcached = '';
/**
* This is only used for Redis. Should be: server:port:password
* @var string
*/
$cache_redis = '';
/**
* This is only for the file cache system. It is the path to the cache directory.
* It is also recommended that you place this in /tmp/ if you are going to use this.
Expand Down

0 comments on commit 67675a5

Please sign in to comment.