|
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
|
|
|
* @param string $path |
|
14
|
|
|
* |
|
15
|
|
|
* @throws \Exception |
|
16
|
|
|
*/ |
|
17
|
5 |
|
public function __construct($path) |
|
18
|
|
|
{ |
|
19
|
5 |
|
if (!is_writable($path)) { |
|
20
|
1 |
|
throw new \Exception(sprintf('%s is not exist or writable', $path)); |
|
21
|
|
|
} |
|
22
|
4 |
|
$options = ['path' => $path]; |
|
23
|
4 |
|
$driver = new \Stash\Driver\FileSystem($options); |
|
24
|
4 |
|
$this->pool = new \Stash\Pool($driver); |
|
25
|
4 |
|
} |
|
26
|
|
|
|
|
27
|
1 |
|
public function save($cache_key, $value, $table, $connection_name) |
|
28
|
|
|
{ |
|
29
|
1 |
|
$item = $this->pool->getItem($connection_name.'/'.$table.'/'.$cache_key); |
|
30
|
1 |
|
$item->lock(); |
|
31
|
1 |
|
$this->pool->save($item->set($value)); |
|
32
|
1 |
|
} |
|
33
|
|
|
|
|
34
|
1 |
|
public function isHit($cache_key, $table, $connection_name) |
|
35
|
|
|
{ |
|
36
|
1 |
|
$item = $this->pool->getItem($connection_name.'/'.$table.'/'.$cache_key); |
|
37
|
1 |
|
if (!$item->isHit()) { |
|
38
|
1 |
|
return false; |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
1 |
|
return $item->get(); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
1 |
|
public function clear($table, $connection_name) |
|
45
|
|
|
{ |
|
46
|
1 |
|
if ($table == '') { |
|
47
|
1 |
|
$this->pool->deleteItem($connection_name.'/*'); |
|
48
|
1 |
|
} else { |
|
49
|
1 |
|
$this->pool->deleteItem($connection_name.'/'.$table.'/*'); |
|
50
|
|
|
} |
|
51
|
1 |
|
} |
|
52
|
|
|
|
|
53
|
1 |
|
public function genKey($query, $parameters, $table, $connection_name) |
|
54
|
|
|
{ |
|
55
|
1 |
|
$parameter_string = implode(',', $parameters); |
|
56
|
1 |
|
$key = $query.':'.$parameter_string.'-'.$table.'-'.$connection_name; |
|
57
|
|
|
|
|
58
|
1 |
|
return hash('md5', $key); |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|