Completed
Push — master ( c86f07...f64f9c )
by Pierre
02:47
created

TRedisCache::getRedisCacheFilename()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 5
ccs 4
cts 4
cp 1
crap 1
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
namespace App\Reuse\Controllers\Api;
6
7
use App\Component\Cache\Redis\Adapter as RedisAdapter;
8
use Nymfonya\Component\Config;
9
use Nymfonya\Component\Container;
10
11
trait TRedisCache
12
{
13
14
    protected $cacheTtl = 60 * 5;
15
    protected $cacheFilename = '';
16
17
    /**
18
     * Redis client
19
     *
20
     * @var \Redis
21
     */
22
    protected $redisClient;
23
24
    /**
25
     * init redis client
26
     *
27
     * @param Container $container
28
     * @return void
29
     */
30 2
    protected function initRedisClient(Container $container): void
31
    {
32 2
        if (is_null($this->redisClient)) {
33 2
            $redisAdapter = new RedisAdapter(
34 2
                $container->getService(Config::class)
35
            );
36 2
            $this->redisClient = $redisAdapter->getClient();
37
        }
38
    }
39
40
    /**
41
     * return true if cache is expired
42
     *
43
     * @param string $key
44
     * @return boolean
45
     */
46 1
    protected function cacheRedisExpired(string $key = ''): bool
47
    {
48 1
        $cfn = (empty($key))
49 1
            ? $this->getRedisCacheFilename()
50 1
            : $key;
51 1
        $exist = $this->redisClient->exists($cfn);
52 1
        return !$exist;
53
    }
54
55
    /**
56
     * set cache ttl
57
     *
58
     * @param integer $ttl
59
     * @return void
60
     */
61 2
    protected function setRedisCacheTtl(int $ttl): void
62
    {
63 2
        $this->cacheTtl = $ttl;
64
    }
65
66
    /**
67
     * returns cache content
68
     *
69
     * @param string $key
70
     * @return mixed
71
     */
72 2
    protected function getRedisCache(string $key = '')
73
    {
74 2
        $cfn = (empty($key))
75 2
            ? $this->getRedisCacheFilename()
76 2
            : $key;
77 2
        $cache = $this->redisClient->get($cfn);
78 2
        return (\is_bool($cache))
0 ignored issues
show
introduced by
The condition is_bool($cache) is always false.
Loading history...
79 1
            ? null
80 2
            : json_decode(
81 2
                $cache,
82 2
                true,
83 2
                512,
84 2
                JSON_OBJECT_AS_ARRAY
85
            );
86
    }
87
88
    /**
89
     * set cache content
90
     *
91
     * @param mixed $content
92
     * @param string $key
93
     * @return void
94
     */
95 2
    protected function setRedisCache($content, $key = ''): void
96
    {
97 2
        $cfn = (empty($key))
98 2
            ? $this->getRedisCacheFilename()
99 2
            : $key;
100 2
        $this->redisClient->set($cfn, json_encode($content));
101 2
        $this->redisClient->expire($cfn, $this->cacheTtl);
102
    }
103
104
    /**
105
     * clear cache entry
106
     *
107
     * @param string $key
108
     * @return void
109
     */
110 1
    protected function clearRedisCache(string $key = '')
111
    {
112 1
        $key = \strval($key);
113 1
        if (empty($key)) {
114 1
            $this->redisClient->del($this->getRedisCacheFilename());
115 1
        } elseif ($key == '*') {
116 1
            $this->redisClient->flushAll();
117
        } else {
118 1
            $this->redisClient->del($key);
119
        }
120
    }
121
122
    /**
123
     * returns cache filename from request uri
124
     *
125
     * @return string
126
     */
127 1
    protected function getRedisCacheFilename(): string
128
    {
129 1
        $path = $this->getRedisCachePath();
130 1
        $filename = md5($this->request->getUri());
131 1
        return $path . $filename;
132
    }
133
134
    /**
135
     * returns cache path from request script filename
136
     *
137
     * @return string
138
     */
139 1
    protected function getRedisCachePath(): string
140
    {
141 1
        return 'API_REQUEST_REDIS_CACHE_';
142
    }
143
}
144