You are here

public function Redis_Cache::set in Redis 7.3

Same name and namespace in other branches
  1. 7.2 lib/Redis/Cache.php \Redis_Cache::set()

Stores data in the persistent cache.

Parameters

$cid: The cache ID of the data to store.

$data: The data to store in the cache. Complex data types will be automatically serialized before insertion. Strings will be stored as plain text and not serialized. Some storage engines only allow objects up to a maximum of 1MB in size to be stored by default. When caching large arrays or similar, take care to ensure $data does not exceed this size.

$expire: (optional) Controls the maximum lifetime of this cache entry. Note that caches might be subject to clearing at any time, so this setting does not guarantee a minimum lifetime. With this in mind, the cache should not be used for data that must be kept during a cache clear, like sessions.

Use one of the following values:

  • CACHE_PERMANENT: Indicates that the item should never be removed unless explicitly told to using cache_clear_all() with a cache ID.
  • CACHE_TEMPORARY: Indicates that the item should be removed at the next general cache wipe.
  • A Unix timestamp: Indicates that the item should be kept at least until the given time, after which it behaves like CACHE_TEMPORARY.

Overrides DrupalCacheInterface::set

File

lib/Redis/Cache.php, line 468

Class

Redis_Cache
Because those objects will be spawned during boostrap all its configuration must be set in the settings.php file.

Code

public function set($cid, $data, $expire = CACHE_PERMANENT) {
  $hash = $this
    ->createEntryHash($cid, $data, $expire);
  $maxTtl = $this
    ->getMaxTtl();
  $permTtl = $this
    ->getPermTtl();
  switch ($expire) {
    case CACHE_PERMANENT:
      $this->backend
        ->set($cid, $hash, $permTtl, false);
      break;
    case CACHE_TEMPORARY:
      $this->backend
        ->set($cid, $hash, $maxTtl, true);
      break;
    default:
      $ttl = $expire - time();

      // Ensure $expire consistency
      if ($ttl <= 0) {

        // Entry has already expired, but we may have a stalled
        // older cache entry remaining there, ensure it wont
        // happen by doing a preventive delete
        $this->backend
          ->delete($cid);
      }
      else {
        if ($maxTtl && $maxTtl < $ttl) {
          $ttl = $maxTtl;
        }
        $this->backend
          ->set($cid, $hash, $ttl, false);
      }
      break;
  }
}