当前位置:网站首页>How to make five kinds of data structures in redis
How to make five kinds of data structures in redis
2022-07-24 23:48:00 【Clean night mortal dust】
redis 5 A method of making data structure
redis data structure
redis yes key-value Data structure of , Every piece of data is ⼀ Key value pairs , The type of key is string , Bonds cannot be repeated .
There are five types of values :
character string string: The string type is Redis The most basic data storage type in , One key Corresponding to one value.
string Type in the Redis Is binary safe . This means that the type can accept data in any format , such as JPEG Image data or JSON Object description information, etc . stay Redis Of string type Value The maximum length of data that can be held is 512M.Hash hash: It's a key value (key=>value) The collection . Ideal for storing objects .
list list: A simple list of strings , Sort by insertion order . You can add an element to the head of the list ( On the left ) Or tail ( On the right ).
aggregate set:string Unordered collection of type . Through the hash table , add to , Delete , The complexity of searching is O (1).
Ordered set zset: and set The same is true. string Collection of type elements , And duplicate members are not allowed . The difference is that each element is associated with a double Score of type .redis It's the scores that sort the members of a collection from small to large .zset Members of are unique , But fractions (score) But it can be repeated .
string Storage
# set key value : set key value
127.0.0.1:6379> set name xiaoming
# Set key expiration time , The unit is in seconds : setex key1 seconds value
127.0.0.1:6379> setex name 3 xiaoming
# Set multiple key values mset key1 value1 key2 value2
127.0.0.1:6379> mset name xiaoming age 18
# Additional value append key value
127.0.0.1:6379> append name ge
(integer) 10
127.0.0.1:6379> get name
"xiaomingge"
# obtain : Get value from key , There is no return null get key
127.0.0.1:6379> get name
# obtain : Return multiple values according to multiple keys mget key1 key2
127.0.0.1:6379> mget name age
1) "xiaomingge"
2) "18"
# Look at all the keys
127.0.0.1:6379> keys *
1) "name"
2) "age"
# Lookup key , regular expression : keys pattern
127.0.0.1:6379> keys n*
1) "name"
# Judge whether the key exists , If there is a return 1, There is no return 0:exists key
127.0.0.1:6379> exists name
(integer) 1
127.0.0.1:6379> exists aa
(integer) 0
# View the type corresponding to the key
127.0.0.1:6379> type name
string
# Delete key and corresponding value del key1 key2
127.0.0.1:6379> del name age
(integer) 2
# Set expiration time for existing keys -1: Represents never expires -2: Represents expired deletion
127.0.0.1:6379> set name a
OK
127.0.0.1:6379> expire name 20
(integer) 1
127.0.0.1:6379> ttl name
(integer) 17 # Expiration time remains 17 second
127.0.0.1:6379> ttl name
(integer) -2 # Indicates that the key value has expired and been deleted
hash Storage
# Set individual properties : hset key field value
127.0.0.1:6379> hset user name xiaoming
(integer) 1
# Set multiple properties : hmset key field1 value1 field2 value2
127.0.0.1:6379> hset user name xiaoming age 18
(integer) 1
# Gets all the properties of the specified key : hkeys key
127.0.0.1:6379> hkeys user
1) "name"
2) "age"
# Get the value of a property : hget key field
127.0.0.1:6379> hget user name
"xiaoming"
# Get values for multiple properties : hmget key field1 field2
127.0.0.1:6379> hmget user name age
1) "xiaoming"
2) "18"
# Get the values of all properties : hvals key
127.0.0.1:6379> hvals user
1) "xiaoming"
2) "18"
# Delete the whole hash Key and its value , Use del
# Delete attribute , The values corresponding to the attributes are deleted together : hdel key diled1 dile2
127.0.0.1:6379> hdel user name age
(integer) 2
list Storage
# Insert data on the left : lpush key value1 value2 ( In reverse order )
127.0.0.1:6379> lpush name a b
(integer) 2
# Insert data on the right : rpush key value1 value2 ( positive sequence )
127.0.0.1:6379> rpush name c d
(integer) 4
# Insert a new element before and after the specified element : linsert key before or after Existing elements The new element
127.0.0.1:6379> linsert name before a xiaoming # stay a Add a new element before the element xiaoming
(integer) 5
# obtain - Returns the elements within the specified range in the list : lrange key start stop, The first element is 0,-1 Represents the last element
## obtain name List all elements
127.0.0.1:6379> lrange name 0 -1
1) "b"
2) "xiaoming"
3) "a"
4) "c"
5) "d"
# Set the element value of the specified position : lset key index value
## The index starts on the left , The first ⼀ Elements are 0. The index can be negative , Indicates that the tail starts counting , Such as - 1 Said the last ⼀ Elements
127.0.0.1:6379> lset name 4 a # This setting is equivalent to indexing wei 4 The elements of It is amended as follows a
OK
127.0.0.1:6379> lrange name 0 -1
1) "b"
2) "xiaoming"
3) "a"
4) "c"
5) "a" # It used to be d Be modified
# Delete - Deletes the specified element :lrem key count value
## Put the list before count The value of the second occurrence is value Remove the elements of ,count > 0: Remove from the beginning to the end ,count < 0: Remove from the tail to the head ,count = 0: Remove all
127.0.0.1:6379> lrem name 0 b # Remove elements b
(integer) 1
set Storage
# Additive elements :sadd key member1 member2 ...
127.0.0.1:6379> sadd name 1 2
(integer) 2
# Get all the elements : smembers key
127.0.0.1:6379> smembers name
1) "1"
2) "2"
# Deletes the specified element : srem key
127.0.0.1:6379> srem name 1
(integer) 1
127.0.0.1:6379> smembers name
1) "2"
zset Storage
# add to : zadd key score1 member1 score2 member2
127.0.0.1:6379> zadd name 2 xiaom 1 xiaoh
(integer) 2
# obtain - Returns the elements in the specified range : zrange key start stop
127.0.0.1:6379> zrange name 0 -1
1) "xiaoh"
2) "xiaom"
## return score Values in min and max Member between : zrangebyscore key min max
127.0.0.1:6379> zrangebyscore name 1 2
1) "xiaoh"
2) "xiaom"
## Return members memberd score value : zscore key member
127.0.0.1:6379> zscore name xiaom
"2"
# Delete - Deletes the specified element : zrem key member1 member2 ...
127.0.0.1:6379> zrem name xiaom
(integer) 1
## Delete the element whose weight is within the specified range : zremrangebyscore key min max
127.0.0.1:6379> zremrangebyscore name 0 2
(integer) 1
边栏推荐
- Coding builds an image, inherits the self built basic image, and reports an error unauthorized: invalid credential Please confirm that you have entered the correct user name and password.
- ShardingSphere-数据库分库分表简介
- 云计算三类巨头:IaaS、PaaS、SaaS,分别是什么意思,应用场景是什么?
- 50 places are limited to open | with the news of oceanbase's annual press conference coming!
- 中金证券新客理财产品收益可以达到百分之六?开户怎么开?
- 基于FPGA的VGA显示
- Is the income of CICC securities' new financial products 6%? I want to open an account and manage money
- Introduction to HLS programming
- Notes of Teacher Li Hongyi's 2020 in-depth learning series 8
- Technical operation
猜你喜欢

Horizontally centered element

痞子衡嵌入式:MCUXpresso IDE下将源码制作成Lib库方法及其与IAR,MDK差异

郑慧娟:基于统一大市场的数据资产应用场景与评估方法研究

c语言:深度刨析函数栈帧

Processing PDF and JPG files in VB6

云计算三类巨头:IaaS、PaaS、SaaS,分别是什么意思,应用场景是什么?

Value driven provides the core driving force for the transformation of commercial BP - BP realization under business scenarios - Commercial BP sharing

JS ------ Chapter 5 functions and events

91. (leaflet chapter) leaflet situation plotting - offensive direction drawing

Salesforce zero foundation learning (116) workflow - & gt; On flow
随机推荐
Live broadcast preview | online seminar on open source security governance models and tools
中金证券新课理财产品的收益有百分之六吗?我想要开户理财
JS ------ Chapter 3 JS cycle
必会面试题:1.浅拷贝和深拷贝_深拷贝
凸优化基础知识
Collection of common online testing tools
NVIDIA inspector detailed instructions
多线程&高并发(全网最新:面试题 + 导图 + 笔记)面试手稳心不慌
salesforce零基础学习(一百一十六)workflow -&gt; flow浅谈
First experience of flask
How to speculate on the Internet? Is it safe to speculate on mobile phones
Horizontally centered element
Notes of Teacher Li Hongyi's 2020 in-depth learning series 2
Weekly summary (*66): next five years
How to propose effective solutions for high-end products? (1 methodology + 2 cases + 1 List)
Paper time review MB2: build a behavior model for autonomous databases
Transmission download list, download file migration machine guide
Vite3.0 has been released, can you still roll it (list of new features)
50 places are limited to open | with the news of oceanbase's annual press conference coming!
codeforces round #797 ABCDEFG