Completed
Push — develop ( f5214a...d7714e )
by Novikov
02:25
created

UberRedis::getResource()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 1
nc 1
nop 0
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
    public function __construct(\Redis $redis)
22
    {
23
        $this->redis = $redis;
24
    }
25
26
    /**
27
     * {@inheritdoc}
28
     */
29
    public function setConnection($host, $port)
30
    {
31
        $this->getRedis()->connect($host, $port);
32
    }
33
34
    /**
35
     * @return Redis
36
     */
37
    public function getRedis()
38
    {
39
        return $this->redis;
40
    }
41
42
    /**
43
     * {@inheritdoc}
44
     */
45
    public function addItem($key, $value, $expiration = null)
46
    {
47
        if ($expiration === null) {
48
            $expiration = 60 * 60 * 24 * 30; // default expires after 30 days
49
        }
50
        $encoded_value = json_encode($value);
51
52
        return $this->getRedis()->set($key, $encoded_value, $expiration);
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    public function getItem($key)
59
    {
60
        $response = json_decode($this->getRedis()->get($key));
61
62
        return $response;
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68
    public function getAllKeys()
69
    {
70
        return $this->getRedis()->keys('*');
71
    }
72
73
    /**
74
     * {@inheritdoc}
75
     */
76
    public function deleteItem($key)
77
    {
78
        $this->getRedis()->delete($key);
79
80
        return true;
81
    }
82
83
    /**
84
     * {@inheritdoc}
85
     */
86
    public function dropCache()
87
    {
88
        return $this->getRedis()->flushAll();
89
    }
90
91
    /**
92
     * {@inheritdoc}
93
     */
94
    public function __toString()
95
    {
96
        return 'uberRedis';
97
    }
98
99
    /**
100
     * {@inheritdoc}
101
     */
102
    public function isFresh($timestamp)
103
    {
104
    }
105
106
    /**
107
     * {@inheritdoc}
108
     */
109
    public function getResource()
110
    {
111
    }
112
} 
1 ignored issue
show
Coding Style introduced by
The closing brace of a class hould be on a line by itself.
Loading history...
113