How to use PHP to record the type of 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); ?>
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!

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

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP is used to build dynamic websites, and its core functions include: 1. Generate dynamic content and generate web pages in real time by connecting with the database; 2. Process user interaction and form submissions, verify inputs and respond to operations; 3. Manage sessions and user authentication to provide a personalized experience; 4. Optimize performance and follow best practices to improve website efficiency and security.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP uses MySQLi and PDO extensions to interact in database operations and server-side logic processing, and processes server-side logic through functions such as session management. 1) Use MySQLi or PDO to connect to the database and execute SQL queries. 2) Handle HTTP requests and user status through session management and other functions. 3) Use transactions to ensure the atomicity of database operations. 4) Prevent SQL injection, use exception handling and closing connections for debugging. 5) Optimize performance through indexing and cache, write highly readable code and perform error handling.

The core benefits of PHP include ease of learning, strong web development support, rich libraries and frameworks, high performance and scalability, cross-platform compatibility, and cost-effectiveness. 1) Easy to learn and use, suitable for beginners; 2) Good integration with web servers and supports multiple databases; 3) Have powerful frameworks such as Laravel; 4) High performance can be achieved through optimization; 5) Support multiple operating systems; 6) Open source to reduce development costs.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.
