Completed
Push — master ( 4304d8...a737ee )
by Jean-Christophe
01:37
created

AbstractDataCache::file_get_contents()

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 1
nc 1
1
<?php
2
/**
3
 * Inspired by (c) Rasmus Schultz <[email protected]>
4
 * <https://github.com/mindplay-dk/php-annotations>
5
 */
6
namespace micro\cache\system;
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 boolean 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
	 * @param string tag the item tag
51
	 * @param boolean $php
52
	 * @throws AnnotationException if file could not be written
53
	 */
54
	public function store($key, $code,$tag=null, $php=true) {
55
		$content="";
56
		if ($php)
57
			$content=self::PHP_TAG;
58
		$content.=$code . "\n";
59
		$this->storeContent($key, $content,$tag);
60
	}
61
62
	public function getRoot() {
63
		return $this->_root;
64
	}
65
66
	abstract protected function storeContent($key,$content,$tag);
67
68
	/**
69
	 * Fetches data stored for the given key.
70
	 * @param string $key cache key
71
	 * @return mixed the cached data
72
	 */
73
	abstract public function fetch($key);
74
75
	/**
76
	 * return data stored for the given key.
77
	 * @param string $key cache key
78
	 * @return mixed the cached data
79
	 */
80
	abstract public function file_get_contents($key);
81
82
	/**
83
	 * Returns the timestamp of the last cache update for the given key.
84
	 *
85
	 * @param string $key cache key
86
	 * @return int unix timestamp
87
	 */
88
	abstract public function getTimestamp($key);
89
90
	abstract public function remove($key);
91
92
	abstract public function clear();
93
94
	abstract public function getCacheFiles($type);
95
96
	abstract public function clearCache($type);
97
98
	public function getCacheInfo(){
99
		return "Cache system is an instance of <b>".\get_class($this)."</b>.";
100
	}
101
102
}
103