日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > php >内容正文

php

PHP操作使用Redis

發(fā)布時間:2024/9/19 php 21 豆豆
生活随笔 收集整理的這篇文章主要介紹了 PHP操作使用Redis 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

連接

//實例化redis $redis = new Redis(); //連接 $redis->connect('127.0.0.1', 6379); //檢測是否連接成功 echo "Server is running: " . $redis->ping(); // 輸出結果 Server is running: +PONG

string

// 設置一個字符串的值 $redis->set('cat', 111); //獲取一個字符串的值 echo $redis->get('cat'); // 111 // 重復set $redis->set('cat', 222); echo $redis->get('cat'); // 222

hash

<?php//實例化redis$redis = new Redis();//連接$redis->connect('127.0.0.1', 6379);//字典//批量設置多個key的值$arr = [1=>1, 2=>2, 3=>3, 4=>4, 5=>5];$redis->hmset('hash', $arr);print_r($redis->hgetall('hash'));echo '<br>';// 批量獲得額多個key的值$arr = [1, 2, 3, 5];$hash = $redis->hmget('hash', $arr);print_r($hash);echo '<br>';//檢測hash中某個key知否存在echo $redis->hexists('hash', '1');echo '<br>';var_dump($redis->hexists('hash', 'cat'));echo '<br>';print_r($redis->hgetall('hash'));echo '<br>';//給hash表中key增加一個整數(shù)值$redis->hincrby('hash', '1', 1);print_r($redis->hgetall('hash'));echo '<br>';//給hash中的某個key增加一個浮點值$redis->hincrbyfloat('hash', 2, 1.3);print_r($redis->hgetall('hash'));echo '<br>';//結果// Array ( [1] => 1 [2] => 2 [3] => 3 [4] => 4 [5] => 5 )// Array ( [1] => 1 [2] => 2 [3] => 3 [5] => 5 )// 1// bool(false)// Array ( [1] => 1 [2] => 2 [3] => 3 [4] => 4 [5] => 5 )// Array ( [1] => 2 [2] => 2 [3] => 3 [4] => 4 [5] => 5 )// Array ( [1] => 2 [2] => 3.3 [3] => 3 [4] => 4 [5] => 5 )

list

//列表 //存儲數(shù)據(jù)到列表中 $redis->lpush('list', 'html'); $redis->lpush('list', 'css'); $redis->lpush('list', 'php'); //獲取列表中所有的值 $list = $redis->lrange('list', 0, -1); print_r($list);echo '<br>'; //從右側加入一個 $redis->rpush('list', 'mysql'); $list = $redis->lrange('list', 0, -1); print_r($list);echo '<br>'; //從左側彈出一個 $redis->lpop('list'); $list = $redis->lrange('list', 0, -1); print_r($list);echo '<br>'; //從右側彈出一個 $redis->rpop('list'); $list = $redis->lrange('list', 0, -1); print_r($list);echo '<br>'; // 結果 // Array ( [0] => php [1] => css [2] => html ) // Array ( [0] => php [1] => css [2] => html [3] => mysql ) // Array ( [0] => css [1] => html [2] => mysql ) // Array ( [0] => css [1] => html )

set

<?php//實例化redis$redis = new Redis();//連接$redis->connect('127.0.0.1', 6379);//集合$redis->sadd('set', 'horse');$redis->sadd('set', 'cat');$redis->sadd('set', 'dog');$redis->sadd('set', 'bird');$redis->sadd('set2', 'fish');$redis->sadd('set2', 'dog');$redis->sadd('set2', 'bird');print_r($redis->smembers('set'));echo '<br>';print_r($redis->smembers('set2'));echo '<br>';//返回集合的交集print_r($redis->sinter('set', 'set2'));echo '<br>';//執(zhí)行交集操作 并結果放到一個集合中$redis->sinterstore('output', 'set', 'set2');print_r($redis->smembers('output'));echo '<br>';//返回集合的并集print_r($redis->sunion('set', 'set2'));echo '<br>';//執(zhí)行并集操作 并結果放到一個集合中$redis->sunionstore('output', 'set', 'set2');print_r($redis->smembers('output'));echo '<br>';//返回集合的差集print_r($redis->sdiff('set', 'set2'));echo '<br>';//執(zhí)行差集操作 并結果放到一個集合中$redis->sdiffstore('output', 'set', 'set2');print_r($redis->smembers('output'));echo '<br>';// 結果// Array ( [0] => cat [1] => dog [2] => bird [3] => horse )// Array ( [0] => bird [1] => dog [2] => fish )// Array ( [0] => bird [1] => dog )// Array ( [0] => dog [1] => bird )// Array ( [0] => cat [1] => dog [2] => bird [3] => horse [4] => fish )// Array ( [0] => cat [1] => dog [2] => bird [3] => horse [4] => fish )// Array ( [0] => horse [1] => cat )// Array ( [0] => horse [1] => cat )

zset

<?php//實例化redis$redis = new Redis();//連接$redis->connect('127.0.0.1', 6379);//有序集合//添加元素echo $redis->zadd('set', 1, 'cat');echo '<br>';echo $redis->zadd('set', 2, 'dog');echo '<br>';echo $redis->zadd('set', 3, 'fish');echo '<br>';echo $redis->zadd('set', 4, 'dog');echo '<br>';echo $redis->zadd('set', 4, 'bird');echo '<br>';//返回集合中的所有元素print_r($redis->zrange('set', 0, -1));echo '<br>';print_r($redis->zrange('set', 0, -1, true));echo '<br>';//返回元素的score值echo $redis->zscore('set', 'dog');echo '<br>';//返回存儲的個數(shù)echo $redis->zcard('set');echo '<br>';//刪除指定成員$redis->zrem('set', 'cat');print_r($redis->zrange('set', 0, -1));echo '<br>';//返回集合中介于min和max之間的值的個數(shù)print_r($redis->zcount('set', 3, 5));echo '<br>';//返回有序集合中score介于min和max之間的值print_r($redis->zrangebyscore('set', 3, 5));echo '<br>';print_r($redis->zrangebyscore('set', 3, 5, ['withscores'=>true]));echo '<br>';//返回集合中指定區(qū)間內所有的值print_r($redis->zrevrange('set', 1, 2));echo '<br>';print_r($redis->zrevrange('set', 1, 2, true));echo '<br>';//有序集合中指定值的socre增加echo $redis->zscore('set', 'dog');echo '<br>';$redis->zincrby('set', 2, 'dog');echo $redis->zscore('set', 'dog');echo '<br>';//移除score值介于min和max之間的元素print_r($redis->zrange('set', 0, -1, true));echo '<br>';print_r($redis->zremrangebyscore('set', 3, 4));echo '<br>';print_r($redis->zrange('set', 0, -1, true));echo '<br>';//結果// 1// 0// 0// 0// 0// Array ( [0] => cat [1] => fish [2] => bird [3] => dog )// Array ( [cat] => 1 [fish] => 3 [bird] => 4 [dog] => 4 )// 4// 4// Array ( [0] => fish [1] => bird [2] => dog )// 3// Array ( [0] => fish [1] => bird [2] => dog )// Array ( [fish] => 3 [bird] => 4 [dog] => 4 )// Array ( [0] => bird [1] => fish )// Array ( [bird] => 4 [fish] => 3 )// 4// 6// Array ( [fish] => 3 [bird] => 4 [dog] => 6 )// 2// Array ( [dog] => 6 )

?

總結

以上是生活随笔為你收集整理的PHP操作使用Redis的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。