Redis.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. namespace PhpLife\Frame\Package\Cache;
  3. /**
  4. * 使用Redis做缓存
  5. */
  6. class Redis extends \PhpLife\Frame\Package\Cache
  7. {
  8. const CONFIG_CLASS = 'Redis\\Core';
  9. const PREFIX = '';
  10. const EXPIRE = 0; // 3 天
  11. /**
  12. * 单个读取
  13. * @param $id
  14. * @return array
  15. */
  16. public static function get($id)
  17. {
  18. $key = self::getKey($id);
  19. try {
  20. $result = self::getConnection()->get($key);
  21. } catch (\Exception $ex) {
  22. $result = null;
  23. }
  24. return $result;
  25. }
  26. /**
  27. * 批量读取,如果没有数据,则对应的value为空
  28. * @param array $idList
  29. * @return array
  30. */
  31. public static function getBatch(Array $idList)
  32. {
  33. $keyList = array();
  34. foreach ($idList as $id) {
  35. $keyList[$id] = self::getKey($id);
  36. }
  37. try {
  38. $result = self::getConnection()->mget($keyList);
  39. } catch (\Exception $ex) {
  40. $result = null;
  41. }
  42. $data = array();
  43. if ($result) {
  44. $i = 0;
  45. foreach ($keyList as $k => $v) {
  46. $data[$k] = $result[$i];
  47. $i++;
  48. }
  49. }
  50. unset($result);
  51. unset($idList);
  52. unset($keyList);
  53. return $data;
  54. }
  55. /**
  56. * @param $id
  57. * @param int $data
  58. * @return bool
  59. */
  60. public static function set($id, $data)
  61. {
  62. if (is_array($data)) {
  63. $data = serialize($data);
  64. }
  65. $key = self::getKey($id);
  66. $class = get_called_class();
  67. try {
  68. if ($class::EXPIRE) {
  69. $result = self::getConnection(true)->setex($key, $class::EXPIRE, $data);
  70. } else {
  71. $result = self::getConnection(true)->set($key, $data);
  72. }
  73. } catch (\Exception $ex) {
  74. $result = false;
  75. }
  76. return $result;
  77. }
  78. /**
  79. * 批量设置
  80. * @param array $data
  81. * @return bool
  82. */
  83. public static function setBatch($data)
  84. {
  85. $dataList = array();
  86. foreach ($data as $k => $v) {
  87. $k = self::getKey($k);
  88. $v = is_array($v) ? serialize($v) : $v;
  89. $dataList[$k] = $v;
  90. }
  91. $class = get_called_class();
  92. try {
  93. $result = self::getConnection(true)->mset($dataList);
  94. // 设置过期时间
  95. if ($class::EXPIRE) {
  96. foreach ($dataList as $k => $v) {
  97. self::getConnection()->expire($k, $class::EXPIRE);
  98. }
  99. }
  100. } catch (\Exception $ex) {
  101. $result = false;
  102. }
  103. unset($data);
  104. unset($dataList);
  105. return $result;
  106. }
  107. /**
  108. * 加1
  109. * @param $id
  110. * @param int $value
  111. * @return int
  112. */
  113. public static function increase($id, $value = 1)
  114. {
  115. $key = self::getKey($id);
  116. $value = abs($value);
  117. try {
  118. $result = self::getConnection(true)->incr($key, $value);
  119. } catch (\Exception $ex) {
  120. $result = false;
  121. }
  122. return $result;
  123. }
  124. /**
  125. * @param $id
  126. * @param int $value
  127. * @return int
  128. */
  129. public static function decrease($id, $value = 1)
  130. {
  131. $key = self::getKey($id);
  132. $value = abs($value);
  133. try {
  134. $result = self::getConnection(true)->decr($key, $value);
  135. } catch (\Exception $ex) {
  136. $result = false;
  137. }
  138. return $result;
  139. }
  140. /**
  141. * 删除
  142. * @param $id
  143. * @return bool
  144. */
  145. public static function delete($id)
  146. {
  147. $key = self::getKey($id);
  148. try {
  149. $result = self::getConnection(true)->delete($key);
  150. } catch (\Exception $ex) {
  151. $result = false;
  152. }
  153. return $result;
  154. }
  155. /**
  156. * 获取连接池
  157. * @param bool $isMaster
  158. * @return \Redis
  159. */
  160. protected static function getConnection($isMaster = false)
  161. {
  162. $class = get_called_class();
  163. $configClass = $class::CONFIG_CLASS;
  164. $redis = new \PhpLife\Frame\Library\Redis($configClass);
  165. $connection = $redis->getConnection($isMaster);
  166. return $connection;
  167. }
  168. }