Maison > Article > base de données > Comment utiliser PHP pour enregistrer le type de redis
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('shop_cart_uid_1'); 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('shop_cart_uid_1',array("501"=>5,"333"=>1,"405"=>8)); $all = $redis->hGetAll("shop_cart_uid_1"); print_r($all); ?>
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('key','501');//查看501是不是key的成员 $redis->sRem('key','1','5','501');//删除key里面的1、5、501 key中不存在的5会被忽略 $redis->sMembers('key');//查看key里面所有成员 $redis->sPop('key');//随便删除key里面的一个成员 $redis->sRandMember('key');//随便返回一个key里面的成员 $redis->sInter('key1','key2','keyn');//查,返回所有给定集合的交集 [array | false]重合的部分数据集合 $redis->sUnion('key1','key2','keyn');//查,返回所有给定集合的并集 [array | false]n个数组一起返回 $redis->sDiff('key1','key2','keyn');//查,返回所有给定集合的差集 [array | false]不重合的数据集合 ?>
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); ?>
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('key');//自增1,如不存在key,赋值为1(只对整数有效,存储以10进制64位,redis中为str)[new_num | false] echo $redis->get("key")."<br>"; echo "让101自增20<br>"; $redis->incrby('key',20);//自增$num,不存在为赋值,值需为整数[new_num | false] echo $redis->get("key")."<br>"; echo "让121自减1<br>"; $redis->decr('key');//自减1,[new_num | false] echo $redis->get("key")."<br>"; echo "让120自减20<br>"; $redis->decrby('key',20);//自减$num,[ new_num | false] echo $redis->get("key")."<br>"; echo "如果键值不存在则为其设置一个值返回true如果键值存在就设置失败返回false<br>"; $redis->setnx('keyss','1001'); 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")); ?>
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); ?>
Ce qui précède est le contenu détaillé de. pour plus d'informations, suivez d'autres articles connexes sur le site Web de PHP en chinois!