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
|
|
|
|