Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update redis module to 3.17 #11

Open
wants to merge 1 commit into
base: fpfis-conf/2.5
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions sites/all/modules/fpfis/redis/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
predis
40 changes: 37 additions & 3 deletions sites/all/modules/fpfis/redis/README.txt
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ enabled due to the EVAL command usage.

If you can't upgrade you Redis server:

- 3.x release will only officially support Redis server <= 2.6
- 3.x release will only officially support Redis server <= 2.8 and 3.x
nevertheless you may use it with Redis 2.4 if you configure your cache
backend to operate in sharding mode.

Expand Down Expand Up @@ -54,7 +54,7 @@ Quick setup
-----------

Here is a simple yet working easy way to setup the module.
This method will Drupal to use Redis for all caches and locks
This method will allow Drupal to use Redis for all caches and locks
and path alias cache replacement.

$conf['redis_client_interface'] = 'PhpRedis'; // Can be "Predis".
Expand All @@ -77,14 +77,48 @@ settings for different bins; It's today very stable.
Advanced configuration
======================

Use the compressed cache
------------------------

Please note this is for now an experimental feature. As a personnal note
from the module author, it should be safe to use.

Use this cache class setting to enable compression. This will save usually
about 80% RAM at the cost of some milliseconds server time.

$conf['cache_default_class'] = 'Redis_CacheCompressed';

Additionnaly, you can alter the default size compression threshold, under which
entries will not be compressed (size is in bytes, set 0 to always compress):

$conf['cache_compression_size_threshold'] = 100;

You can also change the compression level, which an positive integer between
1 and 9, 1 being the lowest but fastest compression ratio, 9 being the most
aggressive compression but is a lot slower. From testing, setting it to the
lower level (1) gives 80% memory usage decrease, which is more than enough.

$conf['cache_compression_ratio'] = 5;

Please note that those settings are global and not on a cache bin basis, you can
already control whenever the compression is to be used or not by selecting a
different cache class on per cache bin basis.

If you switch from the standard default backend (without compression) to the
compressed cache backend, it will recover transparently uncompressed data and
proceed normally without additional cache eviction, it safe to upgrade.
Donwgrading from compressed data to uncompressed data won't work, but the
cache backend will just give you cache hit miss and it will work seamlessly
too without any danger for the site.

Choose the Redis client library to use
--------------------------------------

Add into your settings.php file:

$conf['redis_client_interface'] = 'PhpRedis';

You can replace 'PhpRedis' with 'Predis', depending on the library you chose.
You can replace 'PhpRedis' with 'Predis', depending on the library you chose.

Note that this is optional but recommended. If you don't set this variable the
module will proceed to class lookups and attempt to choose the best client
Expand Down
5 changes: 4 additions & 1 deletion sites/all/modules/fpfis/redis/lib/Redis/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,9 @@ public function getMaxTtl()
return $this->maxTtl;
}

/**
* {@inheritdoc}
*/
public function __construct($bin)
{
$this->bin = $bin;
Expand Down Expand Up @@ -288,7 +291,7 @@ public function getLastFlushTime()
if (!$this->flushCache) {
$this->flushCache = $this->backend->getLastFlushTime();
}

// At the very first hit, we might not have the timestamps set, thus
// we need to create them to avoid our entry being considered as
// invalid
Expand Down
66 changes: 66 additions & 0 deletions sites/all/modules/fpfis/redis/lib/Redis/CacheCompressed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

/**
* This typically brings 80..85% compression in ~20ms/mb write, 5ms/mb read.
*/
class Redis_CacheCompressed extends Redis_Cache implements DrupalCacheInterface
{
private $compressionSizeThreshold = 100;
private $compressionRatio = 1;

/**
* {@inheritdoc}
*/
public function __construct($bin)
{
parent::__construct($bin);

$this->compressionSizeThreshold = (int)variable_get('cache_compression_size_threshold', 100);
if ($this->compressionSizeThreshold < 0) {
trigger_error('cache_compression_size_threshold must be 0 or a positive integer, negative value found, switching back to default 100', E_USER_WARNING);
$this->compressionSizeThreshold = 100;
}

// Minimum compression level (1) has good ratio in low time.
$this->compressionRatio = (int)variable_get('cache_compression_ratio', 1);
if ($this->compressionRatio < 1 || 9 < $this->compressionRatio) {
trigger_error('cache_compression_ratio must be between 1 and 9, out of bounds value found, switching back to default 1', E_USER_WARNING);
$this->compressionRatio = 1;
}
}

/**
* {@inheritdoc}
*/
protected function createEntryHash($cid, $data, $expire = CACHE_PERMANENT)
{
$hash = parent::createEntryHash($cid, $data, $expire);

// Empiric level when compression makes sense.
if (!$this->compressionSizeThreshold || strlen($hash['data']) > $this->compressionSizeThreshold) {

$hash['data'] = gzcompress($hash['data'], $this->compressionRatio);
$hash['compressed'] = true;
}

return $hash;
}

/**
* {@inheritdoc}
*/
protected function expandEntry(array $values, $flushPerm, $flushVolatile)
{
if (!empty($values['data']) && !empty($values['compressed'])) {
// Uncompress, suppress warnings e.g. for broken CRC32.
$values['data'] = @gzuncompress($values['data']);

// In such cases, void the cache entry.
if ($values['data'] === false) {
return false;
}
}

return parent::expandEntry($values, $flushPerm, $flushVolatile);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,100 @@
abstract class Redis_Path_AbstractHashLookup extends Redis_AbstractBackend implements
Redis_Path_HashLookupInterface
{
/**
* @todo document me
*
* @param string $key
* @param string $hkey
* @param string $hvalue
*/
abstract protected function saveInHash($key, $hkey, $hvalue);

/**
* @todo document me
*
* @param string $key
* @param string $hkey
* @param string $hvalue
*/
abstract protected function deleteInHash($key, $hkey, $hvalue);

/**
* @todo document me
*
* @param string $keyPrefix
* @param string $hkey
* @param string $language
*/
abstract protected function lookupInHash($keyPrefix, $hkey, $language = null);

/**
* Normalize value to avoid duplicate or false negatives
*
* @param string $value
*
* @return string
*/
private function normalize($value)
{
if (null !== $value) {
return strtolower(trim($value));
}
}

/**
* {@inheritdoc}
*/
public function saveAlias($source, $alias, $language = null)
{
$alias = $this->normalize($alias);
$source = $this->normalize($source);

if (null === $language) {
$language = LANGUAGE_NONE;
}

if (!empty($source)) {
$this->saveInHash($this->getKey(array(self::KEY_ALIAS, $language)), $source, $alias);
}
if (!empty($alias)) {
$this->saveInHash($this->getKey(array(self::KEY_SOURCE, $language)), $alias, $source);
}
}

/**
* {@inheritdoc}
*/
public function deleteAlias($source, $alias, $language = null)
{
$alias = $this->normalize($alias);
$source = $this->normalize($source);

if (null === $language) {
$language = LANGUAGE_NONE;
}

$this->deleteInHash($this->getKey(array(self::KEY_ALIAS, $language)), $source, $alias);
$this->deleteInHash($this->getKey(array(self::KEY_SOURCE, $language)), $alias, $source);
}

/**
* {@inheritdoc}
*/
public function lookupAlias($source, $language = null)
{
$source = $this->normalize($source);

return $this->lookupInHash(self::KEY_ALIAS, $source, $language);
}

/**
* {@inheritdoc}
*/
public function lookupSource($alias, $language = null)
{
$alias = $this->normalize($alias);

return $this->lookupInHash(self::KEY_SOURCE, $alias, $language);
}
}
56 changes: 5 additions & 51 deletions sites/all/modules/fpfis/redis/lib/Redis/Path/PhpRedis.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,23 +41,6 @@ protected function saveInHash($key, $hkey, $hvalue)
// Empty value here means that we already got it
}

/**
* {@inheritdoc}
*/
public function saveAlias($source, $alias, $language = null)
{
if (null === $language) {
$language = LANGUAGE_NONE;
}

if (!empty($source)) {
$this->saveInHash($this->getKey(array(self::KEY_ALIAS, $language)), $source, $alias);
}
if (!empty($alias)) {
$this->saveInHash($this->getKey(array(self::KEY_SOURCE, $language)), $alias, $source);
}
}

protected function deleteInHash($key, $hkey, $hvalue)
{
$client = $this->getClient();
Expand All @@ -77,29 +60,6 @@ protected function deleteInHash($key, $hkey, $hvalue)
}
}

/**
* {@inheritdoc}
*/
public function deleteAlias($source, $alias, $language = null)
{
if (null === $language) {
$language = LANGUAGE_NONE;
}

$this->deleteInHash($this->getKey(array(self::KEY_ALIAS, $language)), $source, $alias);
$this->deleteInHash($this->getKey(array(self::KEY_SOURCE, $language)), $alias, $source);
}

/**
* {@inheritdoc}
*/
public function deleteLanguage($language)
{
$client = $this->getClient();
$client->del($this->getKey(array(self::KEY_ALIAS, $language)));
$client->del($this->getKey(array(self::KEY_SOURCE, $language)));
}

protected function lookupInHash($keyPrefix, $hkey, $language = null)
{
$client = $this->getClient();
Expand All @@ -117,7 +77,7 @@ protected function lookupInHash($keyPrefix, $hkey, $language = null)
if ($doNoneLookup && (!$ret || self::VALUE_NULL === $ret)) {
$previous = $ret;
$ret = $client->hget($this->getKey(array($keyPrefix, LANGUAGE_NONE)), $hkey);
if (!$ret && $previous) {
if (!$ret || self::VALUE_NULL === $ret) {
// Restore null placeholder else we loose conversion to false
// and drupal_lookup_path() would attempt saving it once again
$ret = $previous;
Expand All @@ -139,16 +99,10 @@ protected function lookupInHash($keyPrefix, $hkey, $language = null)
/**
* {@inheritdoc}
*/
public function lookupAlias($source, $language = null)
{
return $this->lookupInHash(self::KEY_ALIAS, $source, $language);
}

/**
* {@inheritdoc}
*/
public function lookupSource($alias, $language = null)
public function deleteLanguage($language)
{
return $this->lookupInHash(self::KEY_SOURCE, $alias, $language);
$client = $this->getClient();
$client->del($this->getKey(array(self::KEY_ALIAS, $language)));
$client->del($this->getKey(array(self::KEY_SOURCE, $language)));
}
}
Loading