Issues (45)

src/Components/Cache/RedisCache.php (12 issues)

1
<?php
2
3
namespace Matecat\SimpleS3\Components\Cache;
4
5
use Predis\Client as Redis;
6
use Matecat\SimpleS3\Helpers\File;
7
8
class RedisCache implements CacheInterface
9
{
10
    /**
11
     * @var string
12
     */
13
    private $prefixSeparator = DIRECTORY_SEPARATOR;
14
15
    /**
16
     * @var Redis
17
     */
18
    private $redisClient;
19
20
    /**
21
     * RedisCache constructor.
22
     *
23
     * @param Redis $redisClient
24
     */
25 12
    public function __construct(Redis $redisClient)
26
    {
27 12
        $this->redisClient = $redisClient;
28 12
    }
29
30
    /**
31
     * @return bool
32
     */
33
    public function flushAll()
34
    {
35
        $flush = $this->redisClient->flushall();
36
37
        if ($flush->getPayload() === 'OK') {
38
            return true;
39
        }
40
41
        return false;
42
    }
43
44
    /**
45
     * @param string $bucket
46
     * @param string $keyname
47
     * @param null $version
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $version is correct as it would always require null to be passed?
Loading history...
48
     *
49
     * @return array|mixed
50
     */
51 2
    public function get($bucket, $keyname, $version = null)
52
    {
53 2
        if (null != $version) {
0 ignored issues
show
The condition null != $version is always false.
Loading history...
54
            $keyname .= '<VERSION_ID:'.$version.'>';
55
        }
56
57 2
        return unserialize($this->redisClient->hget($this->getHashPrefix($bucket, $keyname), $keyname));
58
    }
59
60
    /**
61
     * @param string $bucket
62
     * @param string $keyname
63
     * @param null $version
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $version is correct as it would always require null to be passed?
Loading history...
64
     *
65
     * @return bool
66
     */
67
    public function has($bucket, $keyname, $version = null)
68
    {
69
        if (null != $version) {
0 ignored issues
show
The condition null != $version is always false.
Loading history...
70
            $keyname .= '<VERSION_ID:'.$version.'>';
71
        }
72
73
        return (1 === $this->redisClient->hexists($this->getHashPrefix($bucket, $keyname), $keyname)) ? true : false;
74
    }
75
76
    /**
77
     * @param string $bucket
78
     * @param string $keyname
79
     * @param null $version
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $version is correct as it would always require null to be passed?
Loading history...
80
     */
81 1
    public function remove($bucket, $keyname, $version = null)
82
    {
83 1
        if (null != $version) {
0 ignored issues
show
The condition null != $version is always false.
Loading history...
84
            $keyname .= '<VERSION_ID:'.$version.'>';
85
        }
86
87 1
        $this->redisClient->hdel($this->getHashPrefix($bucket, $keyname), [$keyname]);
88 1
    }
89
90
    /**
91
     * @param string $bucket
92
     * @param string $keyname
93
     *
94
     * @return array
95
     */
96 3
    public function search($bucket, $keyname)
97
    {
98 3
        return $this->redisClient->hkeys($this->getHashPrefix($bucket, $keyname));
99
    }
100
101
    /**
102
     * @param string $bucket
103
     * @param string $keyname
104
     * @param mixed  $content
105
     * @param null   $version
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $version is correct as it would always require null to be passed?
Loading history...
106
     * @param null   $ttl
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $ttl is correct as it would always require null to be passed?
Loading history...
107
     */
108 2
    public function set($bucket, $keyname, $content, $version = null, $ttl = null)
109
    {
110 2
        if (null != $version) {
0 ignored issues
show
The condition null != $version is always false.
Loading history...
111
            $keyname .= '<VERSION_ID:'.$version.'>';
112
        }
113
114 2
        $this->redisClient->hset($this->getHashPrefix($bucket, $keyname), $keyname, serialize($content));
115
116 2
        if ($this->ttl($bucket, $keyname) === -1) {
117 2
            $this->redisClient->expire($this->getHashPrefix($bucket, $keyname), (null != $ttl) ? $ttl * 60 : self::TTL_STANDARD);
0 ignored issues
show
The condition null != $ttl is always false.
Loading history...
118
        }
119 2
    }
120
121
    /**
122
     * @param string $separator
123
     */
124 9
    public function setPrefixSeparator($separator)
125
    {
126 9
        $this->prefixSeparator = $separator;
127 9
    }
128
129
    /**
130
     * @param string $bucket
131
     * @param string $keyname
132
     * @param null $version
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $version is correct as it would always require null to be passed?
Loading history...
133
     *
134
     * @return int
135
     */
136 2
    public function ttl($bucket, $keyname, $version = null)
137
    {
138 2
        if (null != $version) {
0 ignored issues
show
The condition null != $version is always false.
Loading history...
139
            $keyname .= '<VERSION_ID:'.$version.'>';
140
        }
141
142 2
        return $this->redisClient->ttl($this->getHashPrefix($bucket, $keyname));
143
    }
144
145
    /**
146
     * @param string $bucketName
147
     * @param string $keyName
148
     *
149
     * @return string
150
     */
151 3
    private function getHashPrefix($bucketName, $keyName)
152
    {
153 3
        return hash(self::HASH_ALGORITHM, $bucketName . self::HASH_SAFE_SEPARATOR . $this->getDirName($keyName));
154
    }
155
156
    /**
157
     * @param string $item
158
     *
159
     * @return string
160
     */
161 3
    private function getDirName($item)
162
    {
163 3
        if (File::endsWith($item, $this->prefixSeparator)) {
164 3
            return $item;
165
        }
166
167 3
        $fileInfo = File::getPathInfo($item);
168
169 3
        return $fileInfo['dirname'] . $this->prefixSeparator;
170
    }
171
}
172