Home > Database > Redis > body text

How to use PHP to record the type of redis

WBOY
Release: 2023-06-01 21:07:16
forward
687 people have browsed it

redis_hash.php

<?php
header("content-type:text/html; charset=utf-8");
include_once("config/config.php");

//$redis->hset("shop_cart_uid_1","501","2");
//$redis->hset("shop_cart_uid_1","405","1");
//$redis->hset("shop_cart_uid_1","333","1");
//$redis->hset("shop_cart_uid_1","591","1");
$list = $redis->hGet("shop_cart_uid_1","501");
echo "获取用户id=1购物车中的商品id为501商品的数量为2<br>";
var_dump($list);
$all = $redis->hGetAll("shop_cart_uid_1");
echo "<br>获取用户id=1购物车中的商品id和数量<br>";
print_r($all);
echo "<br>获取用户id=1的购物车中的某些指定商品信息<br>";
$list = $redis->hMGet("shop_cart_uid_1",array("405","501"));
print_r($list);
$redis->hDel("shop_cart_uid_1","333");
echo "<br>删除用户id=1的购物车中的商品id为333的商品记录<br>";
$all = $redis->hGetAll("shop_cart_uid_1");
print_r($all);
echo "<br>获取用户id=1的购物车中的商品总数<br>";
$num = $redis->hLen("shop_cart_uid_1");
echo $num;
echo "<br>对hashKey进行数值操作,用户id=1商品id为405的记录给405的value增加10<br>";
$redis->hIncrBy("shop_cart_uid_1","405","10");
$all = $redis->hGetAll("shop_cart_uid_1");
print_r($all);
echo "<br>查询id=1的用户购物车中商品的数量列表<br>";
$value = $redis->hvals(&#39;shop_cart_uid_1&#39;);
print_r($value);
echo "<br>查询id=1的用户购物车中商品id的列表<br>";
$list = $redis->hKeys("shop_cart_uid_1");
print_r($list);
echo "<br>向用户id=1的购物车中添加多个商品<br>";
$redis->hmset(&#39;shop_cart_uid_1&#39;,array("501"=>5,"333"=>1,"405"=>8));
$all = $redis->hGetAll("shop_cart_uid_1");
print_r($all);

?>
Copy after login

redis_set.php

<?php
header("content-type:text/html; charset=utf-8");
include_once("config/config.php");
//共同好友等,购买过这个商品的人的人群
$redis->sAdd("key","1","2","501");//向key里面添加成员
$redis->sCard("key");//查看key里面的元素个数
$redis->sIsMember(&#39;key&#39;,&#39;501&#39;);//查看501是不是key的成员
$redis->sRem(&#39;key&#39;,&#39;1&#39;,&#39;5&#39;,&#39;501&#39;);//删除key里面的1、5、501 key中不存在的5会被忽略
$redis->sMembers(&#39;key&#39;);//查看key里面所有成员
$redis->sPop(&#39;key&#39;);//随便删除key里面的一个成员
$redis->sRandMember(&#39;key&#39;);//随便返回一个key里面的成员
$redis->sInter(&#39;key1&#39;,&#39;key2&#39;,&#39;keyn&#39;);//查,返回所有给定集合的交集 [array | false]重合的部分数据集合
$redis->sUnion(&#39;key1&#39;,&#39;key2&#39;,&#39;keyn&#39;);//查,返回所有给定集合的并集 [array | false]n个数组一起返回
$redis->sDiff(&#39;key1&#39;,&#39;key2&#39;,&#39;keyn&#39;);//查,返回所有给定集合的差集 [array | false]不重合的数据集合

?>
Copy after login

 redis_list.php

<?php
header("content-type:text/html; charset=utf-8");
include_once("config/config.php");

$redis->lpush("tutorial-list", "1");//从左边推入
$redis->rpush("tutorial-list", "2");//从右变推入
$redis->lpop("tutorial-list");
$list = $redis->lrange("tutorial-list", 0 ,$redis->llen("tutorial-list"));
var_dump($list);
echo "<br>";
$redis->rpop("tutorial-list");
$list = $redis->lrange("tutorial-list", 0 ,$redis->llen("tutorial-list"));
var_dump($list);
echo "<br>";
echo $redis->llen("tutorial-list");//查看某个list数据类型的长度
// 获取存储的数据并输出
echo "<br>";
$list = $redis->lrange("tutorial-list", 0 ,$redis->llen("tutorial-list"));
var_dump($list);

?>
Copy after login

redis_string.php

<?php
header("content-type:text/html; charset=utf-8");
include_once("config/config.php");

//1、string类型  整型浮点型字符串
echo "向key里面设置一个数值100<br>";
$redis->set("key","100");
echo $redis->get("key")."<br>";
echo "让100自增1<br>";
$redis->incr(&#39;key&#39;);//自增1,如不存在key,赋值为1(只对整数有效,存储以10进制64位,redis中为str)[new_num | false]
echo $redis->get("key")."<br>";
echo "让101自增20<br>";
$redis->incrby(&#39;key&#39;,20);//自增$num,不存在为赋值,值需为整数[new_num | false]
echo $redis->get("key")."<br>";
echo "让121自减1<br>";
$redis->decr(&#39;key&#39;);//自减1,[new_num | false]
echo $redis->get("key")."<br>";
echo "让120自减20<br>";
$redis->decrby(&#39;key&#39;,20);//自减$num,[ new_num | false]
echo $redis->get("key")."<br>";

echo "如果键值不存在则为其设置一个值返回true如果键值存在就设置失败返回false<br>";
$redis->setnx(&#39;keyss&#39;,&#39;1001&#39;);
echo $redis->get("keyss")."<br>";
echo "删除已存在的键。不存在的 key 会被忽略【返回删除个数】<br>";
echo $redis->del("keyss");//删除已存在的键。不存在的 key 会被忽略【返回删除个数】

echo "设置key的过期时间如果过期了key会不可用,设置成功返回 1 。 当 key 不存在或者不能为 key 设置过期时间时(比如在低于 2.1.3 版本的 Redis 中你尝试更新 key 的过期时间)返回 0 <br>";
var_dump($redis->expire("keys","60"));

?>
Copy after login

redis_sortedset.php

<?php
header("content-type:text/html; charset=utf-8");
include_once("config/config.php");
//特点value值不能重复,按照score进行排序

$list = $redis->zrem("goods_shop_list","1","2","3");
echo "向商品销量排行榜中插入商品id=1和2、3的销量<br>";
$redis->zAdd("goods_shop_list","501","1","55","2","36","3");
echo "<br>获取当前商品销量排行榜中的商品数量<br>";
$list = $redis->zCard("goods_shop_list");
echo $list;
echo "<br>获取当前商品销量排行榜中销量在10到50之间的商品数量<br>";
$list = $redis->zCount("goods_shop_list",50,60);
echo $list;
echo "<br>获取当前商品销量排行榜中商品id=1的销量<br>";
$list = $redis->zscore("goods_shop_list","1");
echo $list;
echo "<br>根据商品销量从大到小排序前三名<br>";
$list = $redis->zrange("goods_shop_list","0","2",true);
print_r($list);
echo "<br>根据商品销量小到大排序前三名<br>";
$list = $redis->zrevrange("goods_shop_list","0","2",true);
print_r($list);
echo "<br>删除商品销量排名中的商品id=1的销量信息<br>";
$list = $redis->zrem("goods_shop_list","1");
print_r($list);
echo "<br>根据商品销量小到大排序前三名<br>";
$list = $redis->zrevrange("goods_shop_list","0","10",true);
print_r($list);
echo "<br>查询商品id=2的商品在排行榜中从大到小的排名从0开始<br>";
$list = $redis->zrank("goods_shop_list","2");
print_r($list);
echo "<br>查询商品id=2的商品在排行榜中从小到大的排名从0开始<br>";
$list = $redis->zrevrank("goods_shop_list","2");
print_r($list);
?>
Copy after login

The above is the detailed content of How to use PHP to record the type of redis. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
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