PHP Redis类操作

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值