欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程语言 > php >内容正文

php

PHP操作使用Redis

发布时间:2024/9/19 php 30 豆豆
生活随笔 收集整理的这篇文章主要介绍了 PHP操作使用Redis 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

连接

//实例化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增加一个整数值$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

//列表 //存储数据到列表中 $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>';//执行交集操作 并结果放到一个集合中$redis->sinterstore('output', 'set', 'set2');print_r($redis->smembers('output'));echo '<br>';//返回集合的并集print_r($redis->sunion('set', 'set2'));echo '<br>';//执行并集操作 并结果放到一个集合中$redis->sunionstore('output', 'set', 'set2');print_r($redis->smembers('output'));echo '<br>';//返回集合的差集print_r($redis->sdiff('set', 'set2'));echo '<br>';//执行差集操作 并结果放到一个集合中$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>';//返回存储的个数echo $redis->zcard('set');echo '<br>';//删除指定成员$redis->zrem('set', 'cat');print_r($redis->zrange('set', 0, -1));echo '<br>';//返回集合中介于min和max之间的值的个数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>';//返回集合中指定区间内所有的值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的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。