Completed
Push — master ( df07f9...c6bcad )
by Christoffer
08:53 queued 06:33
created

CacheAwareTrait::setInCache()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
rs 10
c 0
b 0
f 0
cc 1
eloc 1
nc 1
nop 3
1
<?php
2
3
namespace Digia\GraphQL\Cache;
4
5
use Psr\SimpleCache\CacheInterface;
6
use Psr\SimpleCache\InvalidArgumentException;
7
8
trait CacheAwareTrait
9
{
10
    /**
11
     * @var CacheInterface
12
     */
13
    protected $cache;
14
15
    /**
16
     * @return string
17
     */
18
    abstract protected function getCachePrefix(): string;
19
20
    /**
21
     * @param mixed      $key
22
     * @param mixed|null $default
23
     * @return mixed
24
     * @throws InvalidArgumentException
25
     */
26
    public function getFromCache($key, $default = null)
27
    {
28
        return $this->cache->get($key, $default);
29
    }
30
31
    /**
32
     * @param mixed $key
33
     * @return bool
34
     * @throws InvalidArgumentException
35
     */
36
    public function isInCache($key): bool
37
    {
38
        return $this->cache->has($key);
39
    }
40
41
    /**
42
     * @param mixed $key
43
     * @return bool
44
     * @throws InvalidArgumentException
45
     */
46
    public function deleteFromCache($key): bool
47
    {
48
        return $this->cache->delete($key);
49
    }
50
51
    /**
52
     * @param mixed $key
53
     * @param mixed $value
54
     * @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...
55
     * @return bool
56
     * @throws InvalidArgumentException
57
     */
58
    public function setInCache($key, $value, $ttl = null): bool
59
    {
60
        return $this->cache->set($key, $value, $ttl);
61
    }
62
63
    /**
64
     *
65
     */
66
    public function clearCache()
67
    {
68
        $this->cache->clear();
69
    }
70
71
    /**
72
     * @param mixed $key
73
     * @return string
74
     */
75
    protected function getCacheKey($key): string
76
    {
77
        return $this->getCachePrefix() . $key;
78
    }
79
}
80