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

RedisIgbinaryLzf::getData()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9

Duplication

Lines 9
Ratio 100 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 9
loc 9
rs 9.9666
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * File containing the RedisIgbinaryLzf 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 View Code Duplication
class RedisIgbinaryLzf extends StashRedis
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
18
{
19
    public function getData($key)
20
    {
21
        $data = $this->redis->get($this->makeKeyString($key));
22
        if (false !== $data) {
23
            return igbinary_unserialize(lzf_decompress($data));
24
        }
25
26
        return $data;
27
    }
28
29
    public function storeData($key, $data, $expiration)
30
    {
31
        lzf_optimized_for(0);
32
33
        $store = lzf_compress(igbinary_serialize(array('data' => $data, 'expiration' => $expiration)));
34
        if (null === $expiration) {
35
            return $this->redis->set($this->makeKeyString($key), $store);
36
        } else {
37
            $ttl = $expiration - time();
38
39
            // Prevent us from even passing a negative ttl'd item to redis,
40
            // since it will just round up to zero and cache forever.
41
            if ($ttl < 1) {
42
                return true;
43
            }
44
45
            return $this->redis->setex($this->makeKeyString($key), $ttl, $store);
46
        }
47
    }
48
49
    public static function isAvailable()
50
    {
51
        return class_exists('Redis', false)
52
            && extension_loaded('igbinary')
53
            && extension_loaded('lzf');
54
    }
55
}
56