PHP Redis class operations

WBOY
Release: 2016-07-25 08:46:28
Original
874 people have browsed it
  1. /************************************************ **********************************
  2. * InitPHP 2.0 Domestic PHP development framework Dao-Nosql-Redis
  3. *------------------------------------------------ -------------------------------
  4. * Copyright: CopyRight By initphp.com
  5. * You can use the source code freely, but During use, please retain the author information.尊重他人劳动成果就是尊重自己
  6. *-------------------------------------------------------------------------------
  7. * $Author:zhuli
  8. * $Dtime:2011-10-09
  9. ***********************************************************************************/
  10. class redisInit {
  11. private $redis; //redis对象
  12. /**
  13. * Initialize Redis
  14. * $config = array(
  15. * 'server' => '127.0.0.1' server
  16. * 'port' => '6379' port number
  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. * Set value
  29. * @param string $key KEY name
  30. * @param string|array $value Get the data
  31. * @param int $timeOut time
  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. * Get data through KEY
  41. * @param string $key KEY name
  42. */
  43. public function get($key) {
  44. $result = $this->redis->get($key);
  45. return json_decode($result, TRUE);
  46. }
  47. /**
  48. * Delete a piece of data
  49. * @param string $key KEY name
  50. */
  51. public function delete($key) {
  52. return $this->redis->delete($key);
  53. }
  54. /**
  55. * Clear data
  56. */
  57. public function flushAll() {
  58. return $this->redis->flushAll();
  59. }
  60. /**
  61. * Data is put into the queue
  62. * @param string $key KEY name
  63. * @param string|array $value Get the obtained data
  64. * @param bool $right Whether to start from the 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. * Dequeue data
  72. * @param string $key KEY name
  73. * @param bool $left Whether to start dequeuing data from the 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. * Data auto-increment
  81. * @param string $key KEY name
  82. */
  83. public function increment($key) {
  84. return $this->redis->incr($key);
  85. }
  86. /**
  87. * Data decrement
  88. * @param string $key KEY name
  89. */
  90. public function decrement($key) {
  91. return $this->redis->decr($key);
  92. }
  93. /**
  94. * Whether the key exists, true is returned if it exists
  95. * @param string $key KEY name
  96. */
  97. public function exists($key) {
  98. return $this->redis->exists($key);
  99. }
  100. /**
  101. * Return the redis object
  102. * Redis has many operation methods, we only encapsulate some of them
  103. * With this object, you can directly call redis's own methods
  104. */
  105. public function redis() {
  106. return $this->redis;
  107. }
  108. }
复制代码

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


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!