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

QueryCache   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 41
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 9
c 1
b 0
f 0
lcom 1
cbo 2
dl 0
loc 41
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getKey() 0 3 1
A start() 0 6 1
A store() 0 3 1
A fetch() 0 6 2
A clear() 0 3 1
A remove() 0 5 2
A setActive() 0 3 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
}