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

AI Hentai Generator
Generate AI Hentai for free.

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 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

Causes and solutions for errors when using PECL to install extensions in Docker environment When using Docker environment, we often encounter some headaches...

CMS stands for Content Management System. It is a software application or platform that enables users to create, manage, and modify digital content without requiring advanced technical knowledge. CMS allows users to easily create and organize content

Arrays are linear data structures used to process data in programming. Sometimes when we are processing arrays we need to add new elements to the existing array. In this article, we will discuss several ways to add elements to the end of an array in PHP, with code examples, output, and time and space complexity analysis for each method. Here are the different ways to add elements to an array: Use square brackets [] In PHP, the way to add elements to the end of an array is to use square brackets []. This syntax only works in cases where we want to add only a single element. The following is the syntax: $array[] = value; Example
