|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Ubiquity\cache\dao; |
|
4
|
|
|
|
|
5
|
|
|
use Ubiquity\cache\CacheManager; |
|
6
|
|
|
use Ubiquity\cache\system\AbstractDataCache; |
|
7
|
|
|
use Ubiquity\cache\system\MemCachedDriver; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Ubiquity\cache\dao$DAOCache |
|
11
|
|
|
* This class is part of Ubiquity |
|
12
|
|
|
* |
|
13
|
|
|
* @author jc |
|
14
|
|
|
* @version 1.0.0 |
|
15
|
|
|
* |
|
16
|
|
|
*/ |
|
17
|
|
|
class DAOCache extends AbstractDAOCache { |
|
18
|
|
|
/** |
|
19
|
|
|
* |
|
20
|
|
|
* @var array |
|
21
|
|
|
*/ |
|
22
|
|
|
private $items; |
|
23
|
|
|
/** |
|
24
|
|
|
* |
|
25
|
|
|
* @var AbstractDataCache |
|
26
|
|
|
*/ |
|
27
|
|
|
protected $cache; |
|
28
|
|
|
|
|
29
|
|
|
protected function getKey($class, $key) { |
|
30
|
|
|
return \md5 ( $class . $key ); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
public function __construct($cacheSystem = MemCachedDriver::class) { |
|
34
|
|
|
if (\is_string ( $cacheSystem )) { |
|
35
|
|
|
$this->cache = new $cacheSystem ( CacheManager::getCacheSubDirectory ( 'objects' ), '.object' ); |
|
36
|
|
|
} else { |
|
37
|
|
|
$this->cache = $cacheSystem; |
|
38
|
|
|
} |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public function store($class, $key, $object) { |
|
42
|
|
|
$this->cache->store ( $this->getKey ( $class, $key ), $object ); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public function fetch($class, $key) { |
|
46
|
|
|
$k = $this->getKey ( $class, $key ); |
|
47
|
|
|
if (! isset ( $this->items [$k] )) { |
|
48
|
|
|
$this->items [$k] = $this->cache->fetch ( $k ); |
|
49
|
|
|
} |
|
50
|
|
|
return $this->items [$k]; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public function delete($class, $key) { |
|
54
|
|
|
$key = $this->getKey ( $class, $key ); |
|
55
|
|
|
if ($this->cache->exists ( $key )) { |
|
56
|
|
|
return $this->cache->remove ( $key ); |
|
57
|
|
|
} |
|
58
|
|
|
return false; |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
|