cache.redis.func.php 3.2 KB

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