当前位置:网站首页>Basic knowledge of redis database learning - basic, commonly used
Basic knowledge of redis database learning - basic, commonly used
2022-07-29 11:50:00 【Learning is endless】
目录
NoSQL简介
NoSQL泛指非关系型的数据库,It is to solve the problems caused by multiple data types in large-scale data sets,特别是大数据应用难题,与SQLlanguage does not conflict.
主要特点:易拓展、大数据量、高性能、灵活的数据模型、高可用.
redis简介与安装
redis是一个免费开源的NoSQL产品.
redis使用ANSI C语言编写、是一个key-value 存储系统.There are many storage types it supports,包括string(字符串)、hash(哈希)、list(列表)、set(集合)和zset(sorted set--有序集合).
redis会周期性的把更新的数据写入磁盘或者把修改操作写入追加的记录文件.
redis是一个高性能的key-value NoSQL 产品,Provides many language development interfaces,使用方便.
ubuntu下安装redis:
--切换到root账号进行操作
sudo apt update
sudo apt install redis-server
redis基本操作
1、启动服务端:redis-server
2、启动客户端:redis-cli(默认不支持中文)、redis-cli --raw(支持中文显示)
3、运行测试命令:ping
- The function is to test withredis服务器是否连通
4、切换数据库:select 1(选择编号为1的数据库)
- 数据库没有名称,默认16个,通过0-15来标识
- By default after starting the client0号数据库
5、redis键值对说明
- redis是key-value 的数据结构
- 每条数据都是一个“键值对”
- 键(key)的类型是字符串(string)
- 值(value)的类型分为5种:字符串string、哈希hash、列表list、无序集合set、有序集合zset
键命令
1、查找键
- 参数支持正则表达式,找到键,返回键名,找不到返回空
--格式
keys pattern(键名)
--例:查找所有键
keys *2、判断键是否存在
- 如果存在返回1,不存在返回0
--格式
exists key3、查看键对应的 value 类型
--格式
type key4、设置键过期时间
- 以秒为单位
- If no time is specified, it will always exist,直到使用del 删除
--格式
expire key seconds5、查看键有效时间
- 返回大于0,代表有效时间,单位:秒
- 返回-1为永久有效
- 返回-2为键不存在
--格式
ttl key字符串string
1、增加、修改
- 如果设置的键不存在则为添加,Modify if it already exists
- redis 中的字符串(string)可以用单引号、双引号、Omit the quotation marks
--设置键值
set key value
--设置键值及过期时间,以秒为单位
setex key seconds value
--设置多个键值对
mset key1 value2 key2 value2 ......
--追加值(Concatenate with the original value)
append key value2、获取
--获取单个值,不存在返回null
get key
--获取多个值
mget key1 key2 ......3、删除
--Delete keys and their corresponding values
del key1 key2 ......哈希hash
1、hash介绍
- hash用于存储“键值对”集合
- 每个哈希中的键可以理解为字段(field),一个字段(field)对应一个值(value)
- hash中的值(value)类型为字符串(string)
- 同一个哈希中字段名(field)不可以重复
2、增加、修改
- 如果设置的键不存在则为添加,Modify if it already exists
--设置单个字段
hset key field value
--设置多个字段
hmset key field1 value1 field2 value2 ......3、获取
--Get all fields of the specified key
hkeys key
--获取一个字段的值
hget key field
--获取多个字段的值
hmget key field1 field2 ......
--获取所有字段的值
hvals key
--Get all field names including their corresponding values
hgetall key4、删除
--删除hash中指定字段,The values corresponding to the fields are deleted together
hdel key field1 field2 ......
--删除整个hashKeys and Fields and Values,使用del命令
del key1 key2 ......列表list
1、list介绍
- 列表中的值(value)类型为字符串(string)
- Each value in the list is sorted in the order in which it was added
2、添加
--从左侧插入值
lpush key value1 value2 ......
--从右侧插入值
rpush key value1 value2 ......
--Inserts a value before or after the specified value
linsert key before 或 after 值 插入的值3、获取
- Returns the values in the specified range in the list
- 索引从左侧开始,第一个值的索引为0
- 索引可以是负数,表示从尾部开始计数,如:-1表示最后一个值
- start,stop 为要获取值的索引
--格式
lrange key start stop
--例:获取键keyA list of all values
lrange key 0 -14、修改
- 设置指定索引位置的值
- 索引从左侧开始,第一个值的索引为0
- 索引可以是负数,表示从尾部开始计数,如:-1表示最后一个值
--格式
lset key index value5、删除
- 删除指定值
- 将列表中前count次出现的值移除
- count > 0,从头往尾删除
- count < 0,从尾往头删除
- count = 0,删除所有值
--格式
lrem key count value无序集合 set
1、介绍
- The value of an unordered collection(value)类型为字符串(string)
- Duplicate values are not allowed in the collection
- 对于集合里的值只能添加与删除,不能修改
2、添加
--集合中添加值
sadd key value1 value2 ......3、获取
--返回所有值
smembers key4、删除
--删除指定值
srem key value有序集合 zset
1、介绍
- The median value of an ordered set(value)类型为字符串(string)
- Duplicate values are not allowed in the collection
- Each value is associated with a score(score),分数(score)可以为负数,通过分数(score)将值从小到大排序
- The values in the sorted set can only be added and deleted,不能修改
2、添加
--集合中添加值
zadd key score1 value1 score2 value2 ......3、获取
- Returns a value in the specified range
- start,stop is the subscript index of the value
- 索引从左侧开始,第一个值的索引为0
- 索引可以是负数,表示从尾部开始计数,如:-1表示最后一个值
- withscores:At the same time, the corresponding score of the value is obtained(score)
--格式
zrange key start stop [withscores]
--返回分数(score)在 min 和 max 之间的值
zrangebyscore key min max
--返回值value的分数(score)
zscore key value4、删除
--删除指定值
zrem key value1 value2 ......
--删除分数(score)A value between the specified range
zremrangebyscore key min max边栏推荐
- QML(一):自定义圆角按钮的处理
- INVALID_ ARGUMENT : Invalid rank for input: modelInput Got: 3 Expected: 4 Please fix either the input
- HMS Core Discovery第16期回顾|与虎墩一起,玩转AI新“声”态
- 精通音视频开发是真的可以为所欲为
- 365天挑战LeetCode1000题——Day 043 有效的正方形 数学
- 报表查询字段集sql摘记
- SQL clock 】 【 daily DAY 23 丨 reporting to the CEO job difficulty moderate 】 【
- 【每日SQL打卡】DAY 26丨餐馆营业额变化增长【难度中等】
- MyCat中间件高可用、读写分离、分片、主从切换、ER分片
- It is recommended to collect a thousand ways to write sql row to column!!
猜你喜欢

Out-of-the-box problem-solving thinking, putting a "rearview mirror" on the unconscious life

Starrocks technology insider: how to have both real-time update and fast query

365天挑战LeetCode1000题——Day 043 有效的正方形 数学

QT's user-defined interface (borderless and movable)

【Unity3D】角色控制器(CharacterController)

Std:: vector copy, append, nested access

即学即用的问题解决思维,给无意识的生活装上“后视镜”

ECCV 2022 | ssp: a new idea of small sample tasks with self-supporting matching

ECCV 2022 | 基于关系查询的时序动作检测方法

AI model risk assessment Part 2: core content
随机推荐
TCP and UDP
Learning with Recoverable Forgetting阅读心得
HMS Core Discovery 16 review | with tiger mound, embracing new AI "voice" state
【每日SQL打卡】DAY 20丨查询结果的质量和占比【难度简单】
QT's user-defined interface (borderless and movable)
Codeforces Round #797 (Div. 3)个人题解
Why should kubernetes be used in development environments
DAY 27 daily SQL clock 】 【 丨 within a specified period of time all order products [difficult simple]
Recursion - Eight Queens Problem
mysql single-line, multi-line subquery
Use anyio instead of asyncio
HMS Core Discovery第16期回顾|与虎墩一起,玩转AI新“声”态
HMS Core音频编辑服务音源分离与空间音频渲染,助力快速进入3D音频的世界
ASN.1接口描述语言详解
Starrocks technology insider: how to have both real-time update and fast query
【每日SQL打卡】DAY 26丨餐馆营业额变化增长【难度中等】
深入理解C# 可空类型
【每日SQL打卡】DAY 20丨查询球队积分【难度中等】
"100 Interview Knowledge Collections" 1. Interview Skills丨Do you really understand HR's careful thinking?
使用anyio替代asyncio