memory_driver_redis.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. <?php
  2. /**
  3. * [Discuz!] (C)2001-2099 Comsenz Inc.
  4. * This is NOT a freeware, use is subject to license terms
  5. *
  6. * $Id: memory_driver_redis.php 33336 2013-05-29 02:05:10Z andyzheng $
  7. */
  8. if (!defined('IN_DISCUZ')) {
  9. exit('Access Denied');
  10. }
  11. class memory_driver_redis {
  12. public $cacheName = 'Redis';
  13. var $enable;
  14. var $obj;
  15. public function env() {
  16. return extension_loaded('redis');
  17. }
  18. function init($config) {
  19. if(!$this->env()) {
  20. $this->enable = false;
  21. return;
  22. }
  23. if (!empty($config['server'])) {
  24. try {
  25. $this->obj = new Redis();
  26. if ($config['pconnect']) {
  27. $connect = @$this->obj->pconnect($config['server'], $config['port']);
  28. } else {
  29. $connect = @$this->obj->connect($config['server'], $config['port']);
  30. }
  31. } catch (RedisException $e) {
  32. }
  33. $this->enable = $connect ? true : false;
  34. if ($this->enable) {
  35. if ($config['requirepass']) {
  36. $this->obj->auth($config['requirepass']);
  37. }
  38. @$this->obj->setOption(Redis::OPT_SERIALIZER, $config['serializer']);
  39. }
  40. }
  41. }
  42. function &instance() {
  43. static $object;
  44. if (empty($object)) {
  45. $object = new memory_driver_redis();
  46. $object->init(getglobal('config/memory/redis'));
  47. }
  48. return $object;
  49. }
  50. function get($key) {
  51. if (is_array($key)) {
  52. return $this->getMulti($key);
  53. }
  54. return $this->obj->get($key);
  55. }
  56. function getMulti($keys) {
  57. $result = $this->obj->getMultiple($keys);
  58. $newresult = array();
  59. $index = 0;
  60. foreach ($keys as $key) {
  61. if ($result[$index] !== false) {
  62. $newresult[$key] = $result[$index];
  63. }
  64. $index++;
  65. }
  66. unset($result);
  67. return $newresult;
  68. }
  69. function select($db = 0) {
  70. return $this->obj->select($db);
  71. }
  72. function set($key, $value, $ttl = 0) {
  73. if ($ttl) {
  74. return $this->obj->setex($key, $ttl, $value);
  75. } else {
  76. return $this->obj->set($key, $value);
  77. }
  78. }
  79. function rm($key) {
  80. return $this->obj->delete($key);
  81. }
  82. function setMulti($arr, $ttl = 0) {
  83. if (!is_array($arr)) {
  84. return FALSE;
  85. }
  86. foreach ($arr as $key => $v) {
  87. $this->set($key, $v, $ttl);
  88. }
  89. return TRUE;
  90. }
  91. function inc($key, $step = 1) {
  92. return $this->obj->incr($key, $step);
  93. }
  94. function dec($key, $step = 1) {
  95. return $this->obj->decr($key, $step);
  96. }
  97. function getSet($key, $value) {
  98. return $this->obj->getSet($key, $value);
  99. }
  100. function sADD($key, $value) {
  101. return $this->obj->sADD($key, $value);
  102. }
  103. function sRemove($key, $value) {
  104. return $this->obj->sRemove($key, $value);
  105. }
  106. function sMembers($key) {
  107. return $this->obj->sMembers($key);
  108. }
  109. function sIsMember($key, $member) {
  110. return $this->obj->sismember($key, $member);
  111. }
  112. function keys($key) {
  113. return $this->obj->keys($key);
  114. }
  115. function expire($key, $second) {
  116. return $this->obj->expire($key, $second);
  117. }
  118. function sCard($key) {
  119. return $this->obj->sCard($key);
  120. }
  121. function hSet($key, $field, $value) {
  122. return $this->obj->hSet($key, $field, $value);
  123. }
  124. function hDel($key, $field) {
  125. return $this->obj->hDel($key, $field);
  126. }
  127. function hLen($key) {
  128. return $this->obj->hLen($key);
  129. }
  130. function hVals($key) {
  131. return $this->obj->hVals($key);
  132. }
  133. function hIncrBy($key, $field, $incr) {
  134. return $this->obj->hIncrBy($key, $field, $incr);
  135. }
  136. function hGetAll($key) {
  137. return $this->obj->hGetAll($key);
  138. }
  139. function sort($key, $opt) {
  140. return $this->obj->sort($key, $opt);
  141. }
  142. function exists($key) {
  143. return $this->obj->exists($key);
  144. }
  145. function clear() {
  146. return $this->obj->flushAll();
  147. }
  148. }
  149. ?>