UberRedis::deleteItem()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
namespace Sleepness\UberTranslationBundle\Storage;
4
5
use \Redis;
6
use Symfony\Component\Config\Resource\ResourceInterface;
7
8
/**
9
 * Wrapper under standard Redis class,
10
 * which ease work with Redis (using phpredis client - https://github.com/phpredis/phpredis)
11
 *
12
 * @author Viktor Novikov <[email protected]>
13
 */
14
class UberRedis implements ResourceInterface, UberStorageInterface
15
{
16
    /**
17
     * @var Redis
18
     */
19
    private $redis;
20
21
    /**
22
     * @param Redis $redis
23
     */
24
    public function __construct(Redis $redis)
25
    {
26
        $this->redis = $redis;
27
    }
28
29
    /**
30
     * {@inheritdoc}
31
     */
32
    public function setConnection($host, $port)
33
    {
34
        $this->getRedis()->connect($host, $port);
35
    }
36
37
    /**
38
     * @return Redis
39
     */
40
    public function getRedis()
41
    {
42
        return $this->redis;
43
    }
44
45
    /**
46
     * {@inheritdoc}
47
     */
48
    public function addItem($key, $value, $expiration = null)
49
    {
50
        if ($expiration === null) {
51
            $expiration = 60 * 60 * 24 * 30; // default expires after 30 days
52
        }
53
        $encoded_value = json_encode($value);
54
55
        return $this->getRedis()->set($key, $encoded_value, $expiration);
56
    }
57
58
    /**
59
     * {@inheritdoc}
60
     */
61
    public function getItem($key)
62
    {
63
        $response = json_decode($this->getRedis()->get($key));
64
65
        return $response;
66
    }
67
68
    /**
69
     * {@inheritdoc}
70
     */
71
    public function getAllKeys()
72
    {
73
        return $this->getRedis()->keys('*');
74
    }
75
76
    /**
77
     * {@inheritdoc}
78
     */
79
    public function deleteItem($key)
80
    {
81
        $this->getRedis()->delete($key);
82
83
        return true;
84
    }
85
86
    /**
87
     * {@inheritdoc}
88
     */
89
    public function dropCache()
90
    {
91
        return $this->getRedis()->flushAll();
92
    }
93
94
    /**
95
     * {@inheritdoc}
96
     */
97
    public function __toString()
98
    {
99
        return 'uberRedis';
100
    }
101
102
    /**
103
     * {@inheritdoc}
104
     */
105
    public function isFresh($timestamp)
106
    {
107
    }
108
109
    /**
110
     * {@inheritdoc}
111
     */
112
    public function getResource()
113
    {
114
    }
115
} 
116