1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* to do: separate into MEMORY and NON-MEMORY |
4
|
|
|
* |
5
|
|
|
*/ |
6
|
|
|
|
7
|
|
View Code Duplication |
class MysqlCache extends Object implements flushable |
|
|
|
|
8
|
|
|
{ |
9
|
|
|
private static $cache_in_mysql_tables = array(); |
|
|
|
|
10
|
|
|
|
11
|
|
|
public static function flush() |
12
|
|
|
{ |
13
|
|
|
$cache = SS_Cache::factory('any'); |
14
|
|
|
$cache->clean(Zend_Cache::CLEANING_MODE_ALL); |
15
|
|
|
$tables = Config::inst()->get('EcommerceCache', 'cache_in_mysql_tables'); |
16
|
|
|
if (is_array($tables)) { |
17
|
|
|
foreach ($tables as $table) { |
18
|
|
|
$table = self::make_mysql_table_name($table); |
19
|
|
|
DB::query( |
20
|
|
|
' |
21
|
|
|
DROP TABLE IF EXISTS "'.$table.'"; |
22
|
|
|
' |
23
|
|
|
); |
24
|
|
|
DB::query( |
25
|
|
|
' |
26
|
|
|
CREATE TABLE "'.$table.'" ( |
27
|
|
|
"PAGEID" int(11) NOT NULL, |
28
|
|
|
"CACHEKEY" CHAR(50) NOT NULL, |
29
|
|
|
"DATA" TEXT |
30
|
|
|
) ENGINE=InnoDB DEFAULT CHARSET=utf8; |
31
|
|
|
' |
32
|
|
|
); |
33
|
|
|
|
34
|
|
|
DB::query( |
35
|
|
|
' |
36
|
|
|
ALTER TABLE "'.$table.'" |
37
|
|
|
ADD PRIMARY KEY ("PAGEID","CACHEKEY"), |
38
|
|
|
ADD KEY "CACHEKEY" ("CACHEKEY"), |
39
|
|
|
ADD KEY "PAGEID" ("PAGEID"); |
40
|
|
|
' |
41
|
|
|
); |
42
|
|
|
} |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public static function clean() |
47
|
|
|
{ |
48
|
|
|
$tables = Config::inst()->get('EcommerceCache', 'cache_in_mysql_tables'); |
49
|
|
|
if (is_array($tables)) { |
50
|
|
|
foreach ($tables as $table) { |
51
|
|
|
$table = self::make_mysql_table_name($table); |
52
|
|
|
DB::query( |
53
|
|
|
' |
54
|
|
|
DELETE FROM "'.$table.'"; |
55
|
|
|
' |
56
|
|
|
); |
57
|
|
|
} |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
private static $_items = array(); |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @param string $table |
65
|
|
|
* @param int $id |
66
|
|
|
* @param string $cacheKey |
67
|
|
|
* @return mixed |
68
|
|
|
*/ |
69
|
|
|
public static function load($table, $id, $cacheKey) |
70
|
|
|
{ |
71
|
|
|
$tables = Config::inst()->get('EcommerceCache', 'cache_in_mysql_tables'); |
72
|
|
|
if (is_array($tables) && in_array($table, $tables)) { |
73
|
|
|
$table = self::make_mysql_table_name($table); |
74
|
|
|
$id = (int)$id; |
75
|
|
|
if (isset(self::$_items[$table])) { |
76
|
|
|
if (isset(self::$_items[$table][$id])) { |
77
|
|
|
if (isset(self::$_items[$table][$id][$cacheKey])) { |
78
|
|
|
if (self::$_items[$table][$id][$cacheKey] !== null) { |
79
|
|
|
return @unserialize(self::$_items[$table][$id][$cacheKey]); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
//we are now loading the data ... |
86
|
|
|
if (!isset(self::$_items[$table])) { |
87
|
|
|
self::$_items[$table] = array(); |
88
|
|
|
} |
89
|
|
|
if (! isset(self::$_items[$table][$id])) { |
90
|
|
|
self::$_items[$table][$id] = array(); |
91
|
|
|
$rows = DB::query('SELECT "CACHEKEY", "DATA" FROM "'.$table.'" WHERE "PAGEID" = '.$id.' ;'); |
92
|
|
|
foreach ($rows as $row) { |
93
|
|
|
self::$_items[$table][$id][$row['CACHEKEY']] = $row['DATA']; |
94
|
|
|
} |
95
|
|
|
//return the value, if there is one. |
96
|
|
|
if (isset(self::$_items[$table][$id])) { |
97
|
|
|
if (isset(self::$_items[$table][$id][$cacheKey])) { |
98
|
|
|
return @unserialize(self::$_items[$table][$id][$cacheKey]); |
99
|
|
|
} |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
} else { |
103
|
|
|
$cache = SS_Cache::factory($table.'_'.$id); |
104
|
|
|
$data = $cache->load($cacheKey); |
105
|
|
|
if (!$data) { |
106
|
|
|
return; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
return @unserialize($data); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* @param string $table |
115
|
|
|
* @param int $id |
116
|
|
|
* @param string $cacheKey |
117
|
|
|
* @param mixed $data |
118
|
|
|
* |
119
|
|
|
*/ |
120
|
|
|
public static function save($table, $id, $cacheKey, $data) |
121
|
|
|
{ |
122
|
|
|
$tables = Config::inst()->get('EcommerceCache', 'cache_in_mysql_tables'); |
123
|
|
|
if (is_array($tables) && in_array($table, $tables)) { |
124
|
|
|
$table = self::make_mysql_table_name($table); |
125
|
|
|
$id = (int)$id; |
126
|
|
|
if (strlen($cacheKey) > 50) { |
127
|
|
|
user_error('ERROR: CACHEKEY longer than 50 characters: '.$cacheKey); |
128
|
|
|
} |
129
|
|
|
$data = Convert::raw2sql(serialize($data)); |
130
|
|
|
if (!isset(self::$_items[$table])) { |
131
|
|
|
self::$_items[$table] = array(); |
132
|
|
|
} |
133
|
|
|
if (isset(self::$_items[$table][$id])) { |
134
|
|
|
self::$_items[$table][$id] = array(); |
135
|
|
|
} |
136
|
|
|
self::$_items[$table][$id][$cacheKey] = $data; |
137
|
|
|
DB::query(' |
138
|
|
|
INSERT INTO "'.$table.'" ("PAGEID", "CACHEKEY", "DATA") |
139
|
|
|
VALUES ('.$id.', \''.$cacheKey.'\', \''.$data.'\') |
140
|
|
|
ON DUPLICATE KEY UPDATE DATA = \''.$data.'\'; |
141
|
|
|
'); |
142
|
|
|
} else { |
143
|
|
|
$cache = SS_Cache::factory($table."_".$id); |
144
|
|
|
$data = serialize($data); |
145
|
|
|
$cache->save($data, $cacheKey); |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @param string $table |
151
|
|
|
* @param int $id |
152
|
|
|
* @param string $cacheKey |
153
|
|
|
* @param mixed $data |
154
|
|
|
*/ |
155
|
|
|
public static function touch($table, $id, $cacheKey, $data) |
|
|
|
|
156
|
|
|
{ |
157
|
|
|
$data = self::load($table, $inty, $cacheKey); |
|
|
|
|
158
|
|
|
self::save($table, $id, $cacheKey, $data); |
159
|
|
|
} |
160
|
|
|
|
161
|
|
|
private static function make_mysql_table_name($table) |
162
|
|
|
{ |
163
|
|
|
return strtoupper($table). '_CACHE'; |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.