cache.func.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  1. <?php
  2. /**
  3. * [WeEngine System] Copyright (c) 2014 WE7.CC
  4. * WeEngine is NOT a free software, it under the license terms, visited http://www.we7.cc/ for more details.
  5. */
  6. defined('IN_IA') or exit('Access Denied');
  7. load()->func('cache.' . cache_type());
  8. function cache_type() {
  9. global $_W;
  10. $cacher = $connect = '';
  11. $cache_type = strtolower($_W['config']['setting']['cache']);
  12. if (extension_loaded($cache_type)) {
  13. $config = $_W['config']['setting'][$cache_type];
  14. if (!empty($config['server']) && !empty($config['port'])) {
  15. if ($cache_type == 'memcache') {
  16. $cacher = new Memcache();
  17. } elseif ($cache_type == 'redis') {
  18. $cacher = new Redis();
  19. }
  20. $connect = $cacher->connect($config['server'], $config['port']);
  21. }
  22. }
  23. if (empty($cacher) || empty($connect)) {
  24. $cache_type = 'mysql';
  25. }
  26. return $cache_type;
  27. }
  28. function cache_load($key, $unserialize = false) {
  29. global $_W;
  30. static $we7_cache;
  31. if (is_error($key)) {
  32. trigger_error($key['message'], E_USER_WARNING);
  33. return false;
  34. }
  35. if (!empty($we7_cache[$key])) {
  36. return $we7_cache[$key];
  37. }
  38. $data = $we7_cache[$key] = cache_read($key);
  39. if ($key == 'setting') {
  40. $_W['setting'] = $data;
  41. return $_W['setting'];
  42. } elseif ($key == 'modules') {
  43. $_W['modules'] = $data;
  44. return $_W['modules'];
  45. } elseif ($key == 'module_receive_enable' && empty($data)) {
  46. cache_build_module_subscribe_type();
  47. return cache_read($key);
  48. } else {
  49. return $unserialize ? iunserializer($data) : $data;
  50. }
  51. }
  52. function &cache_global($key) {
  53. }
  54. function cache_system_key($cache_key) {
  55. $cache_key_all = cache_key_all();
  56. $params = array();
  57. $args = func_get_args();
  58. if (empty($args[1])) {
  59. $args[1] = '';
  60. }
  61. if (!is_array($args[1])) {
  62. $cache_key = $cache_key_all['caches'][$cache_key]['key'];
  63. preg_match_all('/\%([a-zA-Z\_\-0-9]+)/', $cache_key, $matches);
  64. for ($i = 0; $i < func_num_args()-1; $i++) {
  65. $cache_key = str_replace($matches[0][$i], $args[$i+1], $cache_key);
  66. }
  67. return 'we7:' . $cache_key;
  68. } else {
  69. $params = $args[1];
  70. }
  71. if (empty($params)) {
  72. $res = preg_match_all('/([a-zA-Z\_\-0-9]+):/', $cache_key, $matches);
  73. if ($res) {
  74. $key = count($matches[1]) > 0 ? $matches[1][0] : $matches[1];
  75. } else {
  76. $key = $cache_key;
  77. }
  78. if (empty($cache_key_all['caches'][$key])) {
  79. return error(1, '缓存' . $key . ' 不存在!');
  80. } else {
  81. $cache_info_key = $cache_key_all['caches'][$key]['key'];
  82. preg_match_all('/\%([a-zA-Z\_\-0-9]+)/', $cache_info_key, $key_params);
  83. preg_match_all('/\:([a-zA-Z\_\-0-9]+)/', $cache_key, $val_params);
  84. if (count($key_params[1]) != count($val_params[1])) {
  85. foreach ($key_params[1] as $key => $val) {
  86. if (in_array($val, array_keys($cache_key_all['common_params']))) {
  87. $cache_info_key = str_replace('%' . $val, $cache_key_all['common_params'][$val], $cache_info_key);
  88. unset($key_params[1][$key]);
  89. }
  90. }
  91. if (count($key_params[1]) == count($val_params[1])) {
  92. $arr = array_combine($key_params[1], $val_params[1]);
  93. foreach ($arr as $key => $val) {
  94. if (preg_match('/\%' . $key . '/', $cache_info_key)) {
  95. $cache_info_key = str_replace('%' . $key, $val, $cache_info_key);
  96. }
  97. }
  98. }
  99. if (strexists($cache_info_key, '%')) {
  100. return error(1, '缺少缓存参数或参数不正确!');
  101. } else {
  102. return 'we7:' . $cache_info_key;
  103. }
  104. } else {
  105. return 'we7:' . $cache_key;
  106. }
  107. }
  108. }
  109. $cache_info = $cache_key_all['caches'][$cache_key];
  110. $cache_common_params = $cache_key_all['common_params'];
  111. if (empty($cache_info)) {
  112. return error(2, '缓存 ' . $cache_key . ' 不存在!');
  113. } else {
  114. $cache_key = $cache_info['key'];
  115. }
  116. foreach ($cache_common_params as $param_name => $param_val) {
  117. preg_match_all('/\%([a-zA-Z\_\-0-9]+)/', $cache_key, $matches);
  118. if (in_array($param_name, $matches[1]) && !in_array($param_name, array_keys($params))) {
  119. $params[$param_name] = $cache_common_params[$param_name];
  120. }
  121. }
  122. if (is_array($params) && !empty($params)) {
  123. foreach ($params as $key => $param) {
  124. $cache_key = str_replace('%' . $key, $param, $cache_key);
  125. }
  126. if (strexists($cache_key, '%')) {
  127. return error(1, '缺少缓存参数或参数不正确!');
  128. }
  129. }
  130. $cache_key = 'we7:' . $cache_key;
  131. if (strlen($cache_key) > CACHE_KEY_LENGTH) {
  132. trigger_error('Cache name is over the maximum length');
  133. }
  134. return $cache_key;
  135. }
  136. function cache_relation_keys($key) {
  137. if (!is_string($key)) {
  138. return $key;
  139. }
  140. if (!strexists($key, 'we7:')) {
  141. return array($key);
  142. }
  143. $cache_param_values = explode(':', $key);
  144. $cache_name = $cache_param_values[1];
  145. unset($cache_param_values[0]);
  146. unset($cache_param_values[1]);
  147. if (empty($cache_param_values)) {
  148. preg_match_all('/\:([a-zA-Z\_\-0-9]+)/', $key, $matches);
  149. $cache_name = $matches[1][0];
  150. }
  151. $cache_key_all = cache_key_all();
  152. $cache_relations = $cache_key_all['groups'];
  153. $cache_common_params = $cache_key_all['common_params'];
  154. $cache_info = $cache_key_all['caches'][$cache_name];
  155. if (empty($cache_info)) {
  156. return error(2, '缓存 : ' . $key . '不存在');
  157. }
  158. if (!empty($cache_info['group'])) {
  159. if (empty($cache_relations[$cache_info['group']])) {
  160. return error(1, '关联关系未定义');
  161. }
  162. $relation_keys = $cache_relations[$cache_info['group']]['relations'];
  163. $cache_keys = array();
  164. foreach ($relation_keys as $key => $val) {
  165. if ($val == $cache_name) {
  166. $relation_cache_key = $cache_key_all['caches'][$val]['key'];
  167. } else {
  168. $relation_cache_key = $cache_key_all['caches'][$cache_name]['key'];
  169. }
  170. foreach ($cache_common_params as $param_name => $param_val) {
  171. preg_match_all('/\%([a-zA-Z\_\-0-9]+)/', $relation_cache_key, $matches);
  172. if (in_array($param_name, $matches[1])) {
  173. $cache_key_params[$param_name] = $cache_common_params[$param_name];
  174. }
  175. if (!empty($cache_prams_values) || count($matches[1]) == count($cache_param_values)) {
  176. $cache_key_params = array_combine($matches[1], $cache_param_values);
  177. } else {
  178. $cache_key_params = array();
  179. }
  180. }
  181. $cache_key = cache_system_key($val, $cache_key_params);
  182. if (!is_error($cache_key)) {
  183. $cache_keys[] = $cache_key;
  184. } else {
  185. return error(1, $cache_key['message']);
  186. }
  187. }
  188. } else {
  189. $cache_keys[] = $key;
  190. }
  191. return $cache_keys;
  192. }
  193. function cache_key_all() {
  194. global $_W;
  195. $caches_all = array(
  196. 'common_params' => array(
  197. 'uniacid' => $_W['uniacid'],
  198. 'uid' => $_W['uid'],
  199. ),
  200. 'caches' => array(
  201. 'module_info' => array(
  202. 'key' => 'module_info:%module_name',
  203. 'group' => 'module',
  204. ),
  205. 'module_setting' => array(
  206. 'key' => 'module_setting:%module_name:%uniacid',
  207. 'group' => 'module',
  208. ),
  209. 'last_account' => array(
  210. 'key' => 'last_account:%switch:%uid',
  211. 'group' => '',
  212. ),
  213. 'last_account_type' => array(
  214. 'key' => 'last_account_type',
  215. 'group' => '',
  216. ),
  217. 'user_modules' => array(
  218. 'key' => 'user_modules:%uid',
  219. 'group' => '',
  220. ),
  221. 'user_accounts' => array(
  222. 'key' => 'user_accounts:%type:%uid',
  223. 'group' => '',
  224. ),
  225. 'unimodules' => array(
  226. 'key' => 'unimodules:%uniacid',
  227. 'group' => '',
  228. ),
  229. 'unimodules_binding' => array(
  230. 'key' => 'unimodules_binding:%uniacid',
  231. 'group' => '',
  232. ),
  233. 'uni_groups' => array(
  234. 'key' => 'uni_groups:%groupids',
  235. 'group' => '',
  236. ),
  237. 'permission' => array(
  238. 'key' => 'permission:%uniacid:%uid',
  239. 'group' => '',
  240. ),
  241. 'memberinfo' => array(
  242. 'key' => 'memberinfo:%uid',
  243. 'group' => '',
  244. ),
  245. 'statistics' => array(
  246. 'key' => 'statistics:%uniacid',
  247. 'group' => '',
  248. ),
  249. 'uniacid_visit' => array(
  250. 'key' => 'uniacid_visit:%uniacid:%today',
  251. 'group' => '',
  252. ),
  253. 'material_reply' => array(
  254. 'key' => 'material_reply:%attach_id',
  255. 'group' => '',
  256. ),
  257. 'keyword' => array(
  258. 'key' => 'keyword:%content:%uniacid',
  259. 'group' => '',
  260. ),
  261. 'back_days' => array(
  262. 'key' => 'back_days',
  263. 'group' => '',
  264. ),
  265. 'miniapp_version' => array(
  266. 'key' => 'miniapp_version:%version_id',
  267. 'group' => '',
  268. ),
  269. 'site_store_buy' => array(
  270. 'key' => 'site_store_buy:%type:%uniacid',
  271. 'group' => '',
  272. ),
  273. 'proxy_wechatpay_account' => array(
  274. 'key' => 'proxy_wechatpay_account',
  275. 'group' => '',
  276. ),
  277. 'recycle_module' => array(
  278. 'key' => 'recycle_module',
  279. 'group' => '',
  280. ),
  281. 'sync_fans_pindex' => array(
  282. 'key' => 'sync_fans_pindex:%uniacid',
  283. 'group' => '',
  284. ),
  285. 'uniaccount' => array(
  286. 'key' => "uniaccount:%uniacid",
  287. 'group' => 'uniaccount',
  288. ),
  289. 'unisetting' => array(
  290. 'key' => "unisetting:%uniacid",
  291. 'group' => 'uniaccount',
  292. ),
  293. 'defaultgroupid' => array(
  294. 'key' => 'defaultgroupid:%uniacid',
  295. 'group' => 'uniaccount',
  296. ),
  297. 'uniaccount_type' => array(
  298. 'key' => "uniaccount_type:%account_type",
  299. 'group' => '',
  300. ),
  301. 'accesstoken' => array(
  302. 'key' => 'accesstoken:%uniacid',
  303. 'group' => 'accesstoken',
  304. ),
  305. 'jsticket' => array(
  306. 'key' => 'jsticket:%acid',
  307. 'group' => 'accesstoken',
  308. ),
  309. 'cardticket' => array(
  310. 'key' => 'cardticket:%acid',
  311. 'group' => 'accesstoken',
  312. ),
  313. 'accesstoken_key' => array(
  314. 'key' => 'accesstoken_key:%key',
  315. 'group' => '',
  316. ),
  317. 'account_oauth_refreshtoken' => array(
  318. 'key' => 'account_oauth_refreshtoken:%acid',
  319. 'group' => '',
  320. ),
  321. 'account_auth_refreshtoken' => array(
  322. 'key' => 'account_auth_refreshtoken:%uniacid',
  323. 'group' => '',
  324. ),
  325. 'unicount' => array(
  326. 'key' => 'unicount:%uniacid',
  327. 'group' => '',
  328. ),
  329. 'checkupgrade' => array(
  330. 'key' => 'checkupgrade',
  331. 'group' => '',
  332. ),
  333. 'cloud_transtoken' => array(
  334. 'key' => 'cloud_transtoken',
  335. 'group' => '',
  336. ),
  337. 'upgrade' => array(
  338. 'key' => 'upgrade',
  339. 'group' => '',
  340. ),
  341. 'account_ticket' => array(
  342. 'key' => 'account_ticket',
  343. 'group' => '',
  344. ),
  345. 'oauthaccesstoken' => array(
  346. 'key' => 'oauthaccesstoken:%acid',
  347. 'group' => '',
  348. ),
  349. 'account_component_assesstoken' => array(
  350. 'key' => 'account_component_assesstoken',
  351. 'group' => '',
  352. ),
  353. 'cloud_ad_uniaccount' => array(
  354. 'key' => 'cloud_ad_uniaccount:%uniacid',
  355. 'group' => '',
  356. ),
  357. 'cloud_ad_uniaccount_list' => array(
  358. 'key' => 'cloud_ad_uniaccount_list',
  359. 'group' => '',
  360. ),
  361. 'cloud_flow_master' => array(
  362. 'key' => 'cloud_flow_master',
  363. 'group' => '',
  364. ),
  365. 'cloud_ad_tags' => array(
  366. 'key' => 'cloud_ad_tags',
  367. 'group' => '',
  368. ),
  369. 'cloud_ad_type_list' => array(
  370. 'key' => 'cloud_ad_type_list',
  371. 'group' => '',
  372. ),
  373. 'cloud_ad_app_list' => array(
  374. 'key' => 'cloud_ad_app_list:%uniacid',
  375. 'group' => '',
  376. ),
  377. 'cloud_ad_app_support_list' => array(
  378. 'key' => 'cloud_ad_app_support_list',
  379. 'group' => '',
  380. ),
  381. 'cloud_ad_site_finance' => array(
  382. 'key' => 'cloud_ad_site_finance',
  383. 'group' => '',
  384. ),
  385. 'cloud_ad_store_notice' => array(
  386. 'key' => 'cloud_ad_store_notice',
  387. 'group' => '',
  388. ),
  389. 'couponsync' => array(
  390. 'key' => 'couponsync:%uniacid',
  391. 'group' => '',
  392. ),
  393. 'storesync' => array(
  394. 'key' => 'storesync:%uniacid',
  395. 'group' => '',
  396. ),
  397. 'cloud_auth_transfer' => array(
  398. 'key' => 'cloud_auth_transfer',
  399. 'group' => '',
  400. ),
  401. 'modulesetting' => array(
  402. 'key' => 'modulesetting:%module:%acid',
  403. 'group' => '',
  404. ),
  405. 'scan_config' => array(
  406. 'key' => 'scan_config',
  407. 'group' => 'scan_file',
  408. ),
  409. 'scan_file' => array(
  410. 'key' => 'scan_file',
  411. 'group' => 'scan_file',
  412. ),
  413. 'scan_badfile' => array(
  414. 'key' => 'scan_badfile',
  415. 'group' => 'scan_file',
  416. ),
  417. 'bomtree' => array(
  418. 'key' => 'bomtree',
  419. 'group' => '',
  420. ),
  421. 'setting' => array(
  422. 'key' => 'setting',
  423. 'group' => '',
  424. ),
  425. 'stat_todaylock' => array(
  426. 'key' => 'stat_todaylock:%uniacid',
  427. 'group' => '',
  428. ),
  429. 'account_preauthcode' => array(
  430. 'key' => 'account_preauthcode',
  431. 'group' => '',
  432. ),
  433. 'account_auth_accesstoken' => array(
  434. 'key' => 'account_auth_accesstoken:%key',
  435. 'group' => '',
  436. ),
  437. 'usersfields' => array(
  438. 'key' => 'usersfields',
  439. 'group' => '',
  440. ),
  441. 'userbasefields' => array(
  442. 'key' => 'userbasefields',
  443. 'group' => '',
  444. ),
  445. 'system_frame' => array(
  446. 'key' => 'system_frame:%uniacid',
  447. 'group' => '',
  448. ),
  449. 'module_receive_enable' => array(
  450. 'key' => 'module_receive_enable',
  451. 'group' => '',
  452. ),
  453. 'module_entry_call' => array(
  454. 'key' => 'module_entry_call:%module_name',
  455. 'group' => '',
  456. ),
  457. 'system_check' => array(
  458. 'key' => 'system_check',
  459. 'group' => '',
  460. ),
  461. 'delete_visit_ip' => array(
  462. 'key' => 'delete_visit_ip:%date',
  463. 'group' => '',
  464. ),
  465. ),
  466. 'groups' => array(
  467. 'uniaccount' => array(
  468. 'relations' => array('uniaccount', 'unisetting', 'defaultgroupid'),
  469. ),
  470. 'accesstoken' => array(
  471. 'relations' => array('accesstoken', 'jsticket', 'cardticket'),
  472. ),
  473. 'scan_file' => array(
  474. 'relations' => array('scan_file', 'scan_config', 'scan_badfile'),
  475. ),
  476. 'module' => array(
  477. 'relations' => array('module_info', 'module_setting'),
  478. ),
  479. ),
  480. );
  481. return $caches_all;
  482. }