cache.redis.func.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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_redis() {
  8. global $_W;
  9. static $redisobj;
  10. if (!extension_loaded('redis')) {
  11. return error(1, 'Class Redis is not found');
  12. }
  13. $config = $_W['config']['setting']['redis'];
  14. if (empty($redisobj)) {
  15. $redisobj = new Redis();
  16. try {
  17. if ($config['pconnect']) {
  18. $redisobj->pconnect($config['server'], $config['port']);
  19. } else {
  20. $redisobj->connect($config['server'], $config['port']);
  21. }
  22. if (!empty($config['auth'])) {
  23. $redisobj->auth($config['auth']);
  24. }
  25. } catch (Exception $e) {
  26. return error(-1, 'redis连接失败,错误信息:' . $e->getMessage());
  27. }
  28. }
  29. if (defined('IN_MODULE') && !empty($config['db']) && is_array($config['db']) && in_array(IN_MODULE, array_keys($config['db']))) {
  30. $redisobj->select($config['db'][IN_MODULE]);
  31. } elseif (!empty($config['db']['system'])) {
  32. $redisobj->select($config['db']['system']);
  33. }
  34. return $redisobj;
  35. }
  36. function cache_read($key) {
  37. $redis = cache_redis();
  38. if (is_error($redis)) {
  39. return $redis;
  40. }
  41. if ($redis->exists(cache_prefix($key))) {
  42. $data = $redis->get(cache_prefix($key));
  43. $data = iunserializer($data);
  44. return $data;
  45. }
  46. return '';
  47. }
  48. function cache_search($key) {
  49. $redis = cache_redis();
  50. if (is_error($redis)) {
  51. return $redis;
  52. }
  53. $search_keys = $redis->keys(cache_prefix($key) . '*');
  54. $search_data = array();
  55. if (!empty($search_keys)) {
  56. foreach ($search_keys as $search_value) {
  57. $search_data[$search_value] = iunserializer($redis->get($search_value));
  58. }
  59. }
  60. return $search_data;
  61. }
  62. function cache_write($key, $value, $ttl = 0) {
  63. $redis = cache_redis();
  64. if (is_error($redis)) {
  65. return $redis;
  66. }
  67. $value = iserializer($value);
  68. if (empty($ttl)) {
  69. $result = $redis->set(cache_prefix($key), $value);
  70. } else {
  71. $result = $redis->set(cache_prefix($key), $value, $ttl);
  72. }
  73. if ($result) {
  74. return true;
  75. }
  76. return false;
  77. }
  78. function cache_delete($key) {
  79. $redis = cache_redis();
  80. if (is_error($redis)) {
  81. return $redis;
  82. }
  83. $cache_relation_keys = cache_relation_keys($key);
  84. if (is_error($cache_relation_keys)) {
  85. return $cache_relation_keys;
  86. }
  87. if (is_array($cache_relation_keys) && !empty($cache_relation_keys)) {
  88. foreach ($cache_relation_keys as $key) {
  89. $cache_info = cache_load($key);
  90. if (!empty($cache_info)) {
  91. if (method_exists($redis, 'del')) {
  92. $result = $redis->del(cache_prefix($key));
  93. } else {
  94. $result = $redis->delete(cache_prefix($key));
  95. }
  96. if ($result) {
  97. unset($GLOBALS['_W']['cache'][$key]);
  98. } else {
  99. return error(1, '缓存:' . $key . ' 删除失败!');
  100. }
  101. }
  102. }
  103. }
  104. return true;
  105. }
  106. function cache_clean($key = '') {
  107. $redis = cache_redis();
  108. if (is_error($redis)) {
  109. return $redis;
  110. }
  111. if (!empty($key)) {
  112. $cache_relation_keys = cache_relation_keys($key);
  113. if (is_error($cache_relation_keys)) {
  114. return $cache_relation_keys;
  115. }
  116. if (is_array($cache_relation_keys) && !empty($cache_relation_keys)) {
  117. foreach ($cache_relation_keys as $key) {
  118. preg_match_all('/\:([a-zA-Z0-9\-\_]+)/', $key, $matches);
  119. if ($keys = $redis->keys(cache_prefix('we7:' . $matches[1][0]) . '*')) {
  120. unset($GLOBALS['_W']['cache']);
  121. $res = $redis->delete($keys);
  122. if (!$res) {
  123. return error(-1, '缓存 ' . $key . ' 删除失败');
  124. }
  125. }
  126. }
  127. }
  128. return true;
  129. }
  130. if ($redis->flushDB()) {
  131. unset($GLOBALS['_W']['cache']);
  132. return true;
  133. }
  134. return false;
  135. }
  136. function cache_prefix($key) {
  137. return $GLOBALS['_W']['config']['setting']['authkey'] . $key;
  138. }