|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Inspired by (c) Rasmus Schultz <[email protected]> |
|
4
|
|
|
* <https://github.com/mindplay-dk/php-annotations> |
|
5
|
|
|
*/ |
|
6
|
|
|
namespace micro\cache; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* This class is responsible for storing Arrays in PHP files. |
|
10
|
|
|
*/ |
|
11
|
|
|
abstract class AbstractDataCache { |
|
12
|
|
|
/** |
|
13
|
|
|
* |
|
14
|
|
|
* @var string The PHP opening tag (used when writing cache files) |
|
15
|
|
|
*/ |
|
16
|
|
|
const PHP_TAG="<?php\n"; |
|
17
|
|
|
|
|
18
|
|
|
|
|
19
|
|
|
protected $_root; |
|
20
|
|
|
|
|
21
|
|
|
protected $postfix; |
|
22
|
|
|
|
|
23
|
|
|
public function __construct($root, $postfix=""){ |
|
24
|
|
|
$this->_root=$root; |
|
25
|
|
|
$this->postfix=$postfix; |
|
26
|
|
|
} |
|
27
|
|
|
/** |
|
28
|
|
|
* Check if annotation-data for the key has been stored. |
|
29
|
|
|
* @param string $key cache key |
|
30
|
|
|
* @return bool true if data with the given key has been stored; otherwise false |
|
31
|
|
|
*/ |
|
32
|
|
|
abstract public function exists($key); |
|
33
|
|
|
|
|
34
|
|
|
public function expired($key, $duration) { |
|
35
|
|
|
if ($this->exists($key)) { |
|
36
|
|
|
if (\is_int($duration)) { |
|
37
|
|
|
return \time() - $this->getTimestamp($key) > $duration; |
|
38
|
|
|
} else { |
|
39
|
|
|
return false; |
|
40
|
|
|
} |
|
41
|
|
|
} else { |
|
42
|
|
|
return true; |
|
43
|
|
|
} |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Caches the given data with the given key. |
|
48
|
|
|
* @param string $key cache key |
|
49
|
|
|
* @param string $code the source-code to be cached |
|
50
|
|
|
* @throws AnnotationException if file could not be written |
|
51
|
|
|
*/ |
|
52
|
|
|
public function store($key, $code, $php=true) { |
|
53
|
|
|
$content=""; |
|
54
|
|
|
if ($php) |
|
55
|
|
|
$content=self::PHP_TAG; |
|
56
|
|
|
$content.=$code . "\n"; |
|
57
|
|
|
$this->storeContent($key, $content); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
public function getRoot() { |
|
61
|
|
|
return $this->_root; |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
abstract protected function storeContent($key,$content); |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Fetches data stored for the given key. |
|
68
|
|
|
* @param string $key cache key |
|
69
|
|
|
* @return mixed the cached data |
|
70
|
|
|
*/ |
|
71
|
|
|
abstract public function fetch($key); |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* return data stored for the given key. |
|
75
|
|
|
* @param string $key cache key |
|
76
|
|
|
* @return mixed the cached data |
|
77
|
|
|
*/ |
|
78
|
|
|
abstract public function file_get_contents($key); |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Returns the timestamp of the last cache update for the given key. |
|
82
|
|
|
* |
|
83
|
|
|
* @param string $key cache key |
|
84
|
|
|
* @return int unix timestamp |
|
85
|
|
|
*/ |
|
86
|
|
|
abstract public function getTimestamp($key); |
|
87
|
|
|
|
|
88
|
|
|
abstract public function remove($key); |
|
89
|
|
|
|
|
90
|
|
|
abstract public function clear(); |
|
91
|
|
|
|
|
92
|
|
|
} |
|
93
|
|
|
|