cache.memcache.func.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. function cache_memcache() {
  8. global $_W;
  9. static $memcacheobj;
  10. if (!extension_loaded('memcache')) {
  11. return error(1, 'Class Memcache is not found');
  12. }
  13. if (empty($memcacheobj)) {
  14. $config = $_W['config']['setting']['memcache'];
  15. $memcacheobj = new Memcache();
  16. if($config['pconnect']) {
  17. $connect = $memcacheobj->pconnect($config['server'], $config['port']);
  18. } else {
  19. $connect = $memcacheobj->connect($config['server'], $config['port']);
  20. }
  21. if(!$connect) {
  22. return error(-1, 'Memcache is not in work');
  23. }
  24. }
  25. return $memcacheobj;
  26. }
  27. function cache_read($key, $forcecache = true) {
  28. $key = cache_namespace($key);
  29. $memcache = cache_memcache();
  30. if (is_error($memcache)) {
  31. return $memcache;
  32. }
  33. $result = $memcache->get(cache_prefix($key));
  34. if (empty($result) && empty($forcecache)) {
  35. $dbcache = pdo_get('core_cache', array('key' => $key), array('value'));
  36. if (!empty($dbcache['value'])) {
  37. $result = iunserializer($dbcache['value']);
  38. $memcache->set(cache_prefix($key), $result);
  39. }
  40. }
  41. return $result;
  42. }
  43. function cache_search($key) {
  44. return cache_read(cache_prefix($key));
  45. }
  46. function cache_write($key, $value, $ttl = 0, $forcecache = true) {
  47. $key = cache_namespace($key);
  48. $memcache = cache_memcache();
  49. if (is_error($memcache)) {
  50. return $memcache;
  51. }
  52. if (empty($forcecache)) {
  53. $record = array();
  54. $record['key'] = $key;
  55. $record['value'] = iserializer($value);
  56. pdo_insert('core_cache', $record, true);
  57. }
  58. if ($memcache->set(cache_prefix($key), $value, MEMCACHE_COMPRESSED, $ttl)) {
  59. return true;
  60. } else {
  61. return false;
  62. }
  63. }
  64. function cache_delete($key, $forcecache = true) {
  65. $memcache = cache_memcache();
  66. if (is_error($memcache)) {
  67. return $memcache;
  68. }
  69. $cache_relation_keys = cache_relation_keys($key);
  70. if (is_error($cache_relation_keys)) {
  71. return $cache_relation_keys;
  72. }
  73. if (is_array($cache_relation_keys) && !empty($cache_relation_keys)) {
  74. foreach ($cache_relation_keys as $key) {
  75. $cache_info = cache_load($key);
  76. if (!empty($cache_info)) {
  77. $origins_cache_key = $key;
  78. $key = cache_namespace($key);
  79. if (empty($forcecache)) {
  80. pdo_delete('core_cache', array('key' => $key));
  81. }
  82. $result = $memcache->delete(cache_prefix($key));
  83. unset($GLOBALS['_W']['cache'][$origins_cache_key]);
  84. if (!$result) {
  85. return error(1, '缓存: ' . $key . ' 删除失败!');
  86. }
  87. }
  88. }
  89. }
  90. return true;
  91. }
  92. function cache_clean($prefix = '') {
  93. if (!empty($prefix)) {
  94. $cache_relation_keys = cache_relation_keys($prefix);
  95. if (is_error($cache_relation_keys)) {
  96. return $cache_relation_keys;
  97. }
  98. if (is_array($cache_relation_keys) && !empty($cache_relation_keys)) {
  99. foreach ($cache_relation_keys as $key) {
  100. preg_match_all('/\:([a-zA-Z0-9\-\_]+)/', $key, $matches);
  101. $cache_namespace = cache_namespace('we7:' . $matches[1][0]);
  102. pdo_delete('core_cache', array('key LIKE' => $cache_namespace . '%'));
  103. unset($GLOBALS['_W']['cache']);
  104. cache_namespace('we7:' . $matches[1][0], true);
  105. }
  106. }
  107. return true;
  108. }
  109. $memcache = cache_memcache();
  110. if (is_error($memcache)) {
  111. return $memcache;
  112. }
  113. if ($memcache->flush()) {
  114. unset($GLOBALS['_W']['cache']);
  115. pdo_delete('core_cache');
  116. return true;
  117. } else {
  118. return false;
  119. }
  120. }
  121. function cache_namespace($key, $forcenew = false) {
  122. if (!strexists($key, ':')) {
  123. $namespace_cache_key = $key;
  124. } else {
  125. list($key1, $key2) = explode(':', $key);
  126. if ($key1 == 'we7') {
  127. $namespace_cache_key = $key2;
  128. } else {
  129. $namespace_cache_key = $key1;
  130. }
  131. }
  132. if (!in_array($namespace_cache_key, array('unimodules', 'user_modules', 'system_frame'))) {
  133. return $key;
  134. }
  135. $namespace_cache_key = 'cachensl:' . $namespace_cache_key;
  136. $memcache = cache_memcache();
  137. if (is_error($memcache)) {
  138. return $memcache;
  139. }
  140. $namespace = $memcache->get(cache_prefix($namespace_cache_key));
  141. if (empty($namespace) || $forcenew) {
  142. $namespace = random(5);
  143. $memcache->set(cache_prefix($namespace_cache_key), $namespace, MEMCACHE_COMPRESSED, 0);
  144. }
  145. return $namespace . ':' . $key;
  146. }
  147. function cache_prefix($key) {
  148. return $GLOBALS['_W']['config']['setting']['authkey'] . $key;
  149. }