You are here

class Redis_CacheCompressed in Redis 7.3

This typically brings 80..85% compression in ~20ms/mb write, 5ms/mb read.

Hierarchy

Expanded class hierarchy of Redis_CacheCompressed

File

lib/Redis/CacheCompressed.php, line 6

View source
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);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
Redis_Cache::$allowPipeline protected property When in shard mode, the proxy may or may not support pipelining, Twemproxy is known to support it.
Redis_Cache::$allowTemporaryFlush protected property When the global 'cache_lifetime' Drupal variable is set to a value, the cache backends should not expire temporary entries by themselves per Drupal signature. Volatile items will be dropped accordingly to their set lifetime.
Redis_Cache::$backend protected property
Redis_Cache::$bin protected property
Redis_Cache::$flushCache protected property Flush permanent and volatile cached values
Redis_Cache::$isSharded protected property When in shard mode, the backend cannot proceed to multiple keys operations, and won't delete keys on flush calls.
Redis_Cache::$maxTtl protected property Maximum TTL for this bin from Drupal configuration.
Redis_Cache::$permTtl protected property Default TTL for CACHE_PERMANENT items.
Redis_Cache::allowPipeline public function Does this bin allow pipelining through sharded environment
Redis_Cache::allowTemporaryFlush public function Does this bin allow temporary item flush
Redis_Cache::clear public function Expires data from the cache. Overrides DrupalCacheInterface::clear
Redis_Cache::FLUSH_NORMAL constant Uses EVAL scripts to flush data when called
Redis_Cache::FLUSH_SHARD constant This mode is tailored for sharded Redis servers instances usage: it will never delete entries but only mark the latest flush timestamp into one of the servers in the shard. It will proceed to delete on read single entries when invalid entries are…
Redis_Cache::FLUSH_SHARD_WITH_PIPELINING constant Same as the one above, plus attempt to do pipelining when possible.
Redis_Cache::get public function Returns data from the persistent cache. Overrides DrupalCacheInterface::get
Redis_Cache::getLastFlushTime public function Get latest flush time
Redis_Cache::getMaxTtl public function Get maximum TTL for all items.
Redis_Cache::getMultiple public function Returns data from the persistent cache when given an array of cache IDs. Overrides DrupalCacheInterface::getMultiple
Redis_Cache::getNextIncrement public function From the given timestamp build an incremental safe time-based identifier.
Redis_Cache::getPermTtl public function Get TTL for CACHE_PERMANENT items.
Redis_Cache::getValidChecksum public function Get valid checksum
Redis_Cache::isEmpty public function Checks if a cache bin is empty. Overrides DrupalCacheInterface::isEmpty
Redis_Cache::isSharded public function Is this bin in shard mode
Redis_Cache::KEY_THRESHOLD constant Computed keys are let's say arround 60 characters length due to key prefixing, which makes 1,000 keys DEL command to be something arround 50,000 bytes length: this is huge and may not pass into Redis, let's split this off. Some recommend to…
Redis_Cache::LIFETIME_PERM_DEFAULT constant Default lifetime for permanent items. Approximatively 1 year.
Redis_Cache::refreshCapabilities public function Find from Drupal variables the clear mode.
Redis_Cache::refreshMaxTtl public function Find from Drupal variables the maximum cache lifetime.
Redis_Cache::refreshPermTtl protected function Find from Drupal variables the right permanent items TTL.
Redis_Cache::set public function Stores data in the persistent cache. Overrides DrupalCacheInterface::set
Redis_Cache::setLastFlushTime public function Set last flush time
Redis_CacheCompressed::$compressionRatio private property
Redis_CacheCompressed::$compressionSizeThreshold private property
Redis_CacheCompressed::createEntryHash protected function Create cache entry Overrides Redis_Cache::createEntryHash
Redis_CacheCompressed::expandEntry protected function Expand cache entry from fetched data Overrides Redis_Cache::expandEntry
Redis_CacheCompressed::__construct public function Overrides Redis_Cache::__construct