Completed
Push — master ( 552d53...b8eb91 )
by Jean-Christophe
02:02
created

QueryCache::getKey()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace micro\cache;
4
use mindplay\annotations\AnnotationCache;
5
use micro\utils\JArray;
6
7
class QueryCache{
8
	protected static $cache;
9
	protected static $config;
10
	public static $active;
11
12
	protected static function getKey($query){
13
		return "query".\md5($query);
14
	}
15
16
	public static function start(&$config){
17
		self::$config=$config;
18
		$cacheDirectory=ROOT.DS.CacheManager::getCacheDirectory($config).DS."queries";
19
		self::$cache=new AnnotationCache($cacheDirectory);
20
		self::$active=true;
21
	}
22
23
	public static function store($query,$result){
24
		self::$cache->store(self::getKey($query),"return ".JArray::asPhpArray($result,"array").";");
25
	}
26
27
	public static function fetch($query){
28
		$key=self::getKey($query);
29
		if(self::$cache->exists($key))
30
			return self::$cache->fetch($key);
31
		return false;
32
	}
33
34
	public function clear(){
35
		CacheManager::clearCache(self::$config,"queries");
36
	}
37
38
	public function remove($query){
39
		$file=self::$cache->getRoot(). DIRECTORY_SEPARATOR . self::getKey($query) . '.annotations.php';
40
		if(\is_file($file))
41
			unlink($file);
42
	}
43
44
	public static function setActive($value=true){
45
		self::$active=$value;
46
	}
47
}