>  기사  >  백엔드 개발  >  PHP Redis类操作

PHP Redis类操作

WBOY
WBOY원래의
2016-07-25 08:46:28876검색
  1. /*********************************************************************************
  2. * InitPHP 2.0 国产php开发框架 Dao-Nosql-Redis
  3. *-------------------------------------------------------------------------------
  4. * 版权所有: CopyRight By initphp.com
  5. * 您可以自由使用该源码,但是在使用过程中,请保留作者信息。尊重他人劳动成果就是尊重自己
  6. *-------------------------------------------------------------------------------
  7. * $Author:zhuli
  8. * $Dtime:2011-10-09
  9. ***********************************************************************************/
  10. class redisInit {
  11. private $redis; //redis对象
  12. /**
  13. * 初始化Redis
  14. * $config = array(
  15. * 'server' => '127.0.0.1' 服务器
  16. * 'port' => '6379' 端口号
  17. * )
  18. * @param array $config
  19. */
  20. public function init($config = array()) {
  21. if ($config['server'] == '') $config['server'] = '127.0.0.1';
  22. if ($config['port'] == '') $config['port'] = '6379';
  23. $this->redis = new Redis();
  24. $this->redis->connect($config['server'], $config['port']);
  25. return $this->redis;
  26. }
  27. /**
  28. * 设置值
  29. * @param string $key KEY名称
  30. * @param string|array $value 获取得到的数据
  31. * @param int $timeOut 时间
  32. */
  33. public function set($key, $value, $timeOut = 0) {
  34. $value = json_encode($value, TRUE);
  35. $retRes = $this->redis->set($key, $value);
  36. if ($timeOut > 0) $this->redis->setTimeout($key, $timeOut);
  37. return $retRes;
  38. }
  39. /**
  40. * 通过KEY获取数据
  41. * @param string $key KEY名称
  42. */
  43. public function get($key) {
  44. $result = $this->redis->get($key);
  45. return json_decode($result, TRUE);
  46. }
  47. /**
  48. * 删除一条数据
  49. * @param string $key KEY名称
  50. */
  51. public function delete($key) {
  52. return $this->redis->delete($key);
  53. }
  54. /**
  55. * 清空数据
  56. */
  57. public function flushAll() {
  58. return $this->redis->flushAll();
  59. }
  60. /**
  61. * 数据入队列
  62. * @param string $key KEY名称
  63. * @param string|array $value 获取得到的数据
  64. * @param bool $right 是否从右边开始入
  65. */
  66. public function push($key, $value ,$right = true) {
  67. $value = json_encode($value);
  68. return $right ? $this->redis->rPush($key, $value) : $this->redis->lPush($key, $value);
  69. }
  70. /**
  71. * 数据出队列
  72. * @param string $key KEY名称
  73. * @param bool $left 是否从左边开始出数据
  74. */
  75. public function pop($key , $left = true) {
  76. $val = $left ? $this->redis->lPop($key) : $this->redis->rPop($key);
  77. return json_decode($val);
  78. }
  79. /**
  80. * 数据自增
  81. * @param string $key KEY名称
  82. */
  83. public function increment($key) {
  84. return $this->redis->incr($key);
  85. }
  86. /**
  87. * 数据自减
  88. * @param string $key KEY名称
  89. */
  90. public function decrement($key) {
  91. return $this->redis->decr($key);
  92. }
  93. /**
  94. * key是否存在,存在返回ture
  95. * @param string $key KEY名称
  96. */
  97. public function exists($key) {
  98. return $this->redis->exists($key);
  99. }
  100. /**
  101. * 返回redis对象
  102. * redis有非常多的操作方法,我们只封装了一部分
  103. * 拿着这个对象就可以直接调用redis自身方法
  104. */
  105. public function redis() {
  106. return $this->redis;
  107. }
  108. }
复制代码

PHP, Redis
本主题由 小贝 于 2015-11-12 08:43 移动


성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.