Completed
Push — master ( a48513...213d8d )
by Jean-Christophe
01:39
created

TableCache::delete()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 9
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 9
rs 9.6666
c 0
b 0
f 0
cc 3
eloc 6
nc 3
nop 2
1
<?php
2
3
namespace Ubiquity\cache\database;
4
5
use Ubiquity\utils\JArray;
6
7
class TableCache extends DbCache {
8
	protected $arrayCache;
9
10
	public function store($tableName, $condition, $result) {
11
		$exists=$this->getCache($tableName);
12
		$exists[$this->getKey($condition)]=$result;
13
		$this->cache->store($tableName, "return " . JArray::asPhpArray($exists, "array") . ";");
14
	}
15
16
	public function getCache($tableName) {
17
		if ($this->cache->exists($tableName))
18
			return $this->cache->fetch($tableName);
19
		return [ ];
20
	}
21
22
	protected function getArrayCache($tableName) {
23
		if (isset($this->arrayCache[$tableName]))
24
			return $this->arrayCache[$tableName];
25
		if ($this->cache->exists($tableName)) {
26
			return $this->arrayCache[$tableName]=$this->cache->fetch($tableName);
27
		}
28
		return false;
29
	}
30
31
	public function fetch($tableName, $condition) {
32
		if ($cache=$this->getArrayCache($tableName)) {
33
			$key=$this->getKey($condition);
34
			if (isset($cache[$key]))
35
				return $cache[$key];
36
		}
37
		return false;
38
	}
39
40
	public function delete($tableName, $condition){
41
		if ($cache=$this->getArrayCache($tableName)) {
42
			$key=$this->getKey($condition);
43
			if (isset($cache[$key])){
44
				unset($cache[$key]);
45
				$this->cache->store($tableName, "return " . JArray::asPhpArray($cache, "array") . ";");
46
			}
47
		}
48
	}
49
}
50