-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmemcache.php
More file actions
60 lines (55 loc) · 1.59 KB
/
memcache.php
File metadata and controls
60 lines (55 loc) · 1.59 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
<?php
global $memcache;
$memcache = new Memcache;
$memcache->addServer("127.0.0.1");
function getCache($key) {
global $memcache;
return ($memcache) ? $memcache->get($key) : false;
}
function setCache($key,$object,$timeout = 60) {
global $memcache;
return ($memcache) ? $memcache->set($key,$object,0,$timeout) : false;
}
function mysql_query_cache($sql,$linkIdentifier = false,$timeout = 180) {
$cache = getCache(md5("mysql_query" . $sql));
if ($cache === false) {
$r = ($linkIdentifier !== false) ? mysql_query($sql,$linkIdentifier) : mysql_query($sql);
if (!is_resource($r) && !is_object($r)) return false;
if (($rows = mysql_num_rows($r)) !== 0) {
for ($i=0;$i<$rows;$i++) {
$fields = mysql_num_fields($r);
$row = mysql_fetch_array($r);
for ($j=0;$j<$fields;$j++) {
if ($i === 0) {
$columns[$j] = mysql_field_name($r,$j);
}
$cache[$i][$columns[$j]] = $row[$j];
}
}
if (!setCache(md5("mysql_query" . $sql),$cache,$timeout)) {
return $cache;
}
} else {
return array();
}
}
return $cache;
}
function GetMCacheFromQuery($r) {
$cache = array();
if (($rows = mysql_num_rows($r)) !== 0) {
for ($i=0;$i<$rows;$i++) {
$fields = mysql_num_fields($r);
$row = mysql_fetch_array($r);
for ($j=0;$j<$fields;$j++) {
if ($i === 0) {
$columns[$j] = mysql_field_name($r,$j);
}
$cache[$i][$columns[$j]] = $row[$j];
}
}
return $cache;
}
return array();
}
?>