PHP Redis類操作

來源:互聯網
上載者:User
  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 移動
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.