function Redis_Cache_PhpRedis::set in Redis 7.2
Same name and namespace in other branches
- 7.3 lib/Redis/Cache/PhpRedis.php \Redis_Cache_PhpRedis::set()
- 7 lib/Redis/Cache/PhpRedis.php \Redis_Cache_PhpRedis::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/ PhpRedis.php, line 65
Class
- Redis_Cache_PhpRedis
- Predis cache backend.
Code
function set($cid, $data, $expire = CACHE_PERMANENT) {
$client = Redis_Client::getClient();
$skey = $this
->getKey(Redis_Cache_Base::TEMP_SET);
$key = $this
->getKey($cid);
$hash = array(
'cid' => $cid,
'created' => time(),
'expire' => $expire,
'volatile' => (int) (CACHE_TEMPORARY === $expire),
);
// Let Redis handle the data types itself.
if (!is_string($data)) {
$hash['data'] = serialize($data);
$hash['serialized'] = 1;
}
else {
$hash['data'] = $data;
$hash['serialized'] = 0;
}
$pipe = $client
->multi(Redis::PIPELINE);
$pipe
->hmset($key, $hash);
switch ($expire) {
case CACHE_TEMPORARY:
$lifetime = variable_get('cache_lifetime', Redis_Cache_Base::LIFETIME_DEFAULT);
if (0 < $lifetime) {
$pipe
->expire($key, $lifetime);
}
$pipe
->sadd($skey, $cid);
break;
case CACHE_PERMANENT:
if (0 !== ($ttl = $this
->getPermTtl())) {
$pipe
->expire($key, $ttl);
}
// We dont need the PERSIST command, since it's the default.
break;
default:
// If caller gives us an expiry timestamp in the past
// the key will expire now and will never be read.
$ttl = $expire - time();
if ($ttl < 0) {
// Behavior between Predis and PhpRedis seems to change here: when
// setting a negative expire time, PhpRedis seems to ignore the
// command and leave the key permanent.
$pipe
->expire($key, 0);
}
else {
$pipe
->expire($key, $ttl);
$pipe
->sadd($skey, $cid);
}
break;
}
$pipe
->exec();
}