4d84a934
曹明
初始代码提交
|
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
|
<?php
/**
* [WeEngine System] Copyright (c) 2014 WE7.CC
* WeEngine is NOT a free software, it under the license terms, visited http://www.we7.cc/ for more details.
*/
defined('IN_IA') or exit('Access Denied');
function cache_memcache() {
global $_W;
static $memcacheobj;
if (!extension_loaded('memcache')) {
return error(1, 'Class Memcache is not found');
}
if (empty($memcacheobj)) {
$config = $_W['config']['setting']['memcache'];
$memcacheobj = new Memcache();
if($config['pconnect']) {
$connect = $memcacheobj->pconnect($config['server'], $config['port']);
} else {
$connect = $memcacheobj->connect($config['server'], $config['port']);
}
if(!$connect) {
return error(-1, 'Memcache is not in work');
}
}
return $memcacheobj;
}
function cache_read($key, $forcecache = true) {
$key = cache_namespace($key);
$memcache = cache_memcache();
if (is_error($memcache)) {
return $memcache;
}
$result = $memcache->get(cache_prefix($key));
if (empty($result) && empty($forcecache)) {
$dbcache = pdo_get('core_cache', array('key' => $key), array('value'));
if (!empty($dbcache['value'])) {
$result = iunserializer($dbcache['value']);
$memcache->set(cache_prefix($key), $result);
}
}
return $result;
}
function cache_search($key) {
return cache_read(cache_prefix($key));
}
function cache_write($key, $value, $ttl = 0, $forcecache = true) {
$key = cache_namespace($key);
$memcache = cache_memcache();
if (is_error($memcache)) {
return $memcache;
}
if (empty($forcecache)) {
$record = array();
$record['key'] = $key;
$record['value'] = iserializer($value);
pdo_insert('core_cache', $record, true);
}
if ($memcache->set(cache_prefix($key), $value, MEMCACHE_COMPRESSED, $ttl)) {
return true;
} else {
return false;
}
}
function cache_delete($key, $forcecache = true) {
$origins_cache_key = $key;
$key = cache_namespace($key);
$memcache = cache_memcache();
if (is_error($memcache)) {
return $memcache;
}
if (empty($forcecache)) {
pdo_delete('core_cache', array('key' => $key));
}
if ($memcache->delete(cache_prefix($key))) {
unset($GLOBALS['_W']['cache'][$origins_cache_key]);
return true;
} else {
unset($GLOBALS['_W']['cache'][$origins_cache_key]);
return false;
}
}
function cache_clean($prefix = '') {
if (!empty($prefix)) {
$cache_namespace = cache_namespace($prefix, true);
unset($GLOBALS['_W']['cache']);
pdo_delete('core_cache', array('key LIKE' => $cache_namespace . '%'));
return true;
}
$memcache = cache_memcache();
if (is_error($memcache)) {
return $memcache;
}
if ($memcache->flush()) {
unset($GLOBALS['_W']['cache']);
pdo_delete('core_cache');
return true;
} else {
return false;
}
}
function cache_namespace($key, $forcenew = false) {
if (!strexists($key, ':')) {
$namespace_cache_key = $key;
} else {
list($key1, $key2) = explode(':', $key);
if ($key1 == 'we7') {
$namespace_cache_key = $key2;
} else {
$namespace_cache_key = $key1;
}
}
if (!in_array($namespace_cache_key, array('unimodules', 'user_modules'))) {
return $key;
}
$namespace_cache_key = 'cachensl:' . $namespace_cache_key;
$memcache = cache_memcache();
if (is_error($memcache)) {
return $memcache;
}
$namespace = $memcache->get(cache_prefix($namespace_cache_key));
if (empty($namespace) || $forcenew) {
$namespace = random(5);
$memcache->set(cache_prefix($namespace_cache_key), $namespace, MEMCACHE_COMPRESSED, 0);
}
return $namespace . ':' . $key;
}
function cache_prefix($key) {
return $GLOBALS['_W']['config']['setting']['authkey'] . $key;
}
|