IdiormCache   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 11
Bugs 6 Features 1
Metric Value
wmc 7
c 11
b 6
f 1
lcom 1
cbo 3
dl 0
loc 59
ccs 27
cts 27
cp 1
rs 10

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 8 1
A save() 0 7 1
A isHit() 0 9 2
A clear() 0 8 2
A genKey() 0 7 1
1
<?php
2
3
namespace Ilex\Cache;
4
5
use Ilex\Cache\Interfaces\IdiormCacheInterface;
6
7
class IdiormCache implements IdiormCacheInterface
8
{
9
    /* @var $pool \Stash\Pool */
10
    private $pool;
11
12
    /**
13
     * expires after in sec, default 1hour = 3600s.
14
     *
15
     * @var int
16
     */
17
    private $expiresAfter;
18
19
    /**
20
     * @param string $path
21
     */
22 5
    public function __construct($path = '', $expiresAfter = 3600)
23
    {
24 5
        $options = ['path' => $path];
25 5
        $driver = new \Stash\Driver\FileSystem($options);
26 5
        $this->pool = new \Stash\Pool($driver);
27
28 5
        $this->expiresAfter = $expiresAfter;
29 5
    }
30
31 2
    public function save($cache_key, $value, $table, $connection_name)
32
    {
33 2
        $item = $this->pool->getItem($connection_name.'/'.$table.'/'.$cache_key);
34 2
        $item->lock();
35 2
        $item->expiresAfter($this->expiresAfter);
36 2
        $this->pool->save($item->set($value));
37 2
    }
38
39 2
    public function isHit($cache_key, $table, $connection_name)
40
    {
41 2
        $item = $this->pool->getItem($connection_name.'/'.$table.'/'.$cache_key);
42 2
        if (!$item->isHit()) {
43 2
            return false;
44
        }
45
46 1
        return $item->get();
47
    }
48
49 1
    public function clear($table, $connection_name)
50
    {
51 1
        if ($table == '') {
52 1
            $this->pool->deleteItem($connection_name.'/*');
53 1
        } else {
54 1
            $this->pool->deleteItem($connection_name.'/'.$table.'/*');
55
        }
56 1
    }
57
58 1
    public function genKey($query, $parameters, $table, $connection_name)
59
    {
60 1
        $parameter_string = implode(',', $parameters);
61 1
        $key = $query.':'.$parameter_string.'-'.$table.'-'.$connection_name;
62
63 1
        return hash('md5', $key);
64
    }
65
}
66