cache.redis.func.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. if($redis->delete(cache_prefix($key))) {
  74. unset($GLOBALS['_W']['cache'][$key]);
  75. return true;
  76. }
  77. return false;
  78. }
  79. function cache_clean($key = '') {
  80. $redis = cache_redis();
  81. if (is_error($redis)) {
  82. return $redis;
  83. }
  84. if (!empty($key)) {
  85. if ($keys = $redis->keys(cache_prefix($key) . "*")) {
  86. unset($GLOBALS['_W']['cache']);
  87. return $redis->delete($keys) ? true : false;
  88. }
  89. }
  90. if ($redis->flushAll()) {
  91. unset($GLOBALS['_W']['cache']);
  92. return true;
  93. }
  94. return false;
  95. }
  96. function cache_prefix($key) {
  97. return $GLOBALS['_W']['config']['setting']['authkey'] . $key;
  98. }