Completed
Push — 6.13 ( 7e7b66...6c34c5 )
by André
53:02 queued 27:51
created

RedisIgbinary::storeData()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
nc 3
nop 3
dl 0
loc 17
rs 9.7
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * File containing the RedisIgbinary class.
5
 *
6
 * @copyright Copyright (c) 2009, Robert Hafner. All rights reserved.
7
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
8
 *
9
 * Original source: https://github.com/tedious/Stash/blob/master/src/Stash/Driver/Redis.php
10
 *
11
 * @license For full copyright and license information view LICENSE file distributed with this source code.
12
 */
13
namespace eZ\Bundle\EzPublishCoreBundle\Cache\Driver\Redis;
14
15
use Stash\Driver\Redis as StashRedis;
16
17
class RedisIgbinary extends StashRedis
18
{
19
    public function getData($key)
20
    {
21
        $data = $this->redis->get($this->makeKeyString($key));
22
        if (false !== $data) {
23
            return igbinary_unserialize($data);
24
        }
25
26
        return $data;
27
    }
28
29
    public function storeData($key, $data, $expiration)
30
    {
31
        $store = igbinary_serialize(array('data' => $data, 'expiration' => $expiration));
32
        if (null === $expiration) {
33
            return $this->redis->set($this->makeKeyString($key), $store);
34
        } else {
35
            $ttl = $expiration - time();
36
37
            // Prevent us from even passing a negative ttl'd item to redis,
38
            // since it will just round up to zero and cache forever.
39
            if ($ttl < 1) {
40
                return true;
41
            }
42
43
            return $this->redis->setex($this->makeKeyString($key), $ttl, $store);
44
        }
45
    }
46
47
    public static function isAvailable()
48
    {
49
        return class_exists('Redis', false)
50
            && extension_loaded('igbinary');
51
    }
52
}
53