ホームページ  >  記事  >  バックエンド開発  >  PHP Redis クラスの操作

PHP Redis クラスの操作

WBOY
WBOYオリジナル
2016-07-25 08:46:28876ブラウズ
  1. /******************************************** *** *************************************
  2. * InitPHP 2.0 国産 PHP 開発フレームワーク Dao-Nosql -Redis
  3. *---------------------------------------------- -- -----------------------------
  4. * Copyright: 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. $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. $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 flashAll() {
  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. $を返してください? $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. 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. * キーが存在するかどうか、存在する場合は true を返します
  95. * @param string $key KEY 名
  96. */
  97. public function contains($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 までご連絡ください。