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); ?>
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!

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

方法:1、用“str_replace(" ","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\ \;||\xc2\xa0)/","其他字符",$str)”语句。

查找方法:1、用strpos(),语法“strpos("字符串值","查找子串")+1”;2、用stripos(),语法“strpos("字符串值","查找子串")+1”。因为字符串是从0开始计数的,因此两个函数获取的位置需要进行加1处理。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

Dreamweaver Mac version
Visual web development tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool