-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathLudoDBCache.php
More file actions
151 lines (141 loc) · 4.32 KB
/
LudoDBCache.php
File metadata and controls
151 lines (141 loc) · 4.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
<?php
/**
* Caching of serialized LudoDBObjects
* User: Alf Magne
* Date: 28.01.13
* @package LudoDB
* @author Alf Magne Kalleland <post@dhtmlgoodies.com>
*/
/**
* Cache class used by LudoDBRequestHandler. This is a LudoDBModel storing data as serialized
* strings in the database. When caching is enabled for a service, LudoDBRequestHandler will
* attempt to retrieve data from cache before calling the service method. This may save time
* for services requiring many SQL queries to complete.
* @package LudoDB
*/
class LudoDBCache extends LudoDBModel
{
/**
* JSONConfig set to false
* @var bool
*/
protected $JSONConfig = false;
/**
* Config of ludo_db_cache
* @var array
*/
protected $config = array(
'table' => 'ludo_db_cache',
'sql' => "select class_name, cache_key, cache_value from ludo_db_cache where cache_key=?",
'idField' => 'JSON_key',
'columns' => array(
'id' => array(
'db' => 'int auto_increment not null primary key'
),
'cache_key' => array(
'db' => 'varchar(512)',
'access' => 'rw'
),
'class_name' => array(
'db' => 'varchar(512)',
'access' => 'rw'
),
'cache_value' => array(
'db' => 'mediumtext',
'access' => 'rw'
)
),
'indexes' => array('cache_key','class_name')
);
/**
* Internal JSON cache
* @var null
*/
private $JSON = null;
/**
* Creates new LudoDBCache instance. An empty LudoDBService class is sent to the constructor along
* with constructor arguments. The cache class will search for records where "class_name" equals
* class of given service and arguments compiled to a key matches cache_key in the database table.
* @param LudoDBService $resource
* @param array $arguments
*/
public function __construct(LudoDBService $resource = null, array $arguments = null){
if(isset($resource)){
$resourceName = get_class($resource);
$cacheKey = $this->getCacheKey($resourceName, $arguments);
parent::__construct($cacheKey);
if(isset($cacheKey)){
$this->setKey($cacheKey);
$this->setClassName($resourceName);
$this->JSON = $this->getValue('cache_value');
}
}else{
parent::__construct();
}
}
/**
* Return cache key based on class name and arguments.
* @param $resourceName
* @param $arguments
* @return string
*/
private function getCacheKey($resourceName, $arguments){
return implode("_", array_merge(array($resourceName), $arguments));
}
/**
* Returns true if cache data exists.
* @return bool
*/
public function hasData(){
return isset($this->JSON) && strlen($this->JSON)>0;
}
/**
* Set class name for new cache record
* @param $name
*/
private function setClassName($name){
$this->setValue('class_name', $name);
}
/**
* Return cache data
* @return array
*/
public function getCache(){
$ret = $this->getValue('cache_value');
return isset($ret) && strlen($ret)> 0 ? unserialize($ret) : '';
}
/**
* Set cache_key of new cache record
* @param $key
* @return LudoDBCache
*/
public function setKey($key){
$this->setValue('cache_key', $key);
return $this;
}
/**
* Store service data to cache
* @param array $json
* @return LudoDBCache
*/
public function setCache(array $json){
$this->setValue('cache_value', serialize($json));
return $this;
}
/**
* Clear cache records from db where cache_key equals key
* @param string $key
*/
public static function clearBy($key){
if(isset($key) && strlen($key)){
LudoDb::getInstance()->query("delete from ludo_db_cache where cache_key=?", array($key));
}
}
/**
* Clear all cache record for given class name / resource.
* @param $className
*/
public static function clearByClass($className){
LudoDb::getInstance()->query("delete from ludo_db_cache where class_name=?", array($className));
}
}