redis.init.php 3.2 KB

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