当前位置:网站首页>Redis basic data type (string/list/set/hash/zset)
Redis basic data type (string/list/set/hash/zset)
2022-07-25 23:37:00 【csdn_ kiki:)】
3、 character string (String)
redis The most basic data type in ,String Type is binary safe , Can contain any data , such as jpg Picture or serialized object . A string can be at most 512M.
Bottom : Simple dynamic string , String that can be modified . similar ArrayList, Reduce the frequent allocation of memory by pre allocating redundant space .
in the light of value
set key value # value You can override jedis.set(key, value);
get key jedis.get("k1")
append key value # Yes key Corresponding value Perform value splicing
strlen key # Get the length of the value
setnx key value # Only in key When there is no , You can set key Value
incr key # value When it's a numeric type ++
decr key # value When it's a numeric type --
incrby key increment # value += increment
decrby key decremment # value -= decrement
Atomicity : Will not be interrupted by the thread scheduling mechanism , Once you start , It won't stop , Until the end .
In a single thread , Operations that can be performed in a single instruction can be considered as " Atomic manipulation ", Because interrupts can only occur between instructions .
In multithreading , Can't be used by other processes ( Threads ) The interrupted operation is called atomic operation .
Redis The atomicity of a single command is mainly due to Redis The single thread .
mset <key1><value1><key2><value2> # Set one or more... At the same time key-value Yes jedis.mset(k1,v1,k2,v2)
mget <key1> <key2> # According to the multiple key Get multiple value jedis.mget("k1","k2","k3")
msetnx <key1><value1><key2><value2> # All when setting key Can't exist ( Atomicity )
getrange <key>< The starting position >< End position > # Get the range of values , similar java Medium substring, Front bag , Back bag
setrange <key>< The starting position ><value> # use <value> overwrite <key> String value stored , from < The starting position > Start ( Index from 0 Start ).
setex <key>< Expiration time (s)><value>
getset key value # New value for old value
4、 list (List)
in the light of value, Single key multiple values use List
Redis List is 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 ).
Its bottom layer is actually a two-way linked list , High performance on both ends , The performance of the nodes in the middle of the index subscript operation will be poor .
List The data structure of is fast linked list quickList.
First, in the case of fewer list elements will use a block of continuous memory storage , This structure is ziplist, That is, compressed list .
It stores all the elements next to each other , Allocated is a continuous block of memory .
When there is a large amount of data, it will be changed to quicklist.
Because ordinary linked list needs too much additional pointer space , It's a waste of space . For example, what's in this list is just int Data of type , Two additional pointers are required for the structure prev and next.
Redis Linking lists and ziplist Combined to form quicklist. That is to say, multiple ziplist Use two-way pointer string to use . This not only satisfies the fast insertion and deletion performance , There will not be too much space redundancy .
lpush/rpush <key><value1><value2><value3> # From the left / Insert one or more values... To the right .
lpop/rpop <key> # From the left / Spit out a value on the right . Value at key at , The value of the light key .
rpoplpush <key1><key2> # from <key1> Spit a value on the right side of the list , insert <key2> To the left of the list .
lrange <key><start><stop> # Get elements according to index subscript ( From left to right ) 0:-1 jedis.lrange(key, start, stop)
lindex <key><index> Get elements according to index subscript ( From left to right )
llen <key> Get the list length
linsert <key> before/after <value><newvalue> # stay <value> In front of / Insert... At the back <newvalue> Insert value
lrem <key><n><value> Delete from the left n individual value( From left to right )
lset<key><index><value> Will list key Subscript to be index Replace the value of with value
5、 aggregate (Set)
Redis Of Set yes string Unordered collection of type . The bottom of it is actually a value by null Of hash surface , So add the , Delete , The complexity of searching is O(1).
sadd <key><value1><value2> # Put one or more member Elements are added to the collection key in , What already exists member Elements will be ignored
smembers <key> # Take all the values of the set .
sismember <key><value> # Judgment set <key> Whether it contains the <value> value , Yes 1, No, 0
scard<key> # Returns the number of elements in the collection .
srem <key><value1><value2> .... Delete an element in the collection .
spop <key> Spit a value out of the set at random .
srandmember <key> <n> Randomly take... From the set n It's worth . Will not be removed from the collection .
smove <source> <destination> value # Move a value in a set from one set to another
sinter <key1><key2> # Returns the intersection element of two sets .
sunion <key1><key2> # Returns the union element of two sets .
sdiff <key1><key2> # Returns the difference set element of two sets (key1 Medium , It doesn't contain key2 Medium )
Set The data structure is dict Dictionaries , Dictionaries are implemented with hash tables .
Java in HashSet The internal implementation of HashMap, It's just all value They all point to the same object .Redis Of set The structure is the same , It's also used internally hash structure , be-all value All point to the same internal value .
6、 Hash (Hash)
Redis hash It's a collection of key-value pairs .
Redis hash It's a string Type of field and value Mapping table ,hash Ideal for storing objects .
similar Java Inside Map<String, Object>
user ID For searching key, Stored value User object contains name , Age , Birthday and other information , If you use ordinary key/value Structure to store
hset <key><field><value> # to <key> In the collection <field> Key assignment <value>
hget <key1><field> # from <key1> aggregate <field> Take out value
hmset <key1><field1><value1><field2><value2> # Batch settings hash Value
hexists<key1><field> # Look at the hash table key in , Given domain field Whether there is .
hkeys <key> # List the hash All of the collection field
hvals <key> # List the hash All of the collection value
hincrby <key><field><increment> # Hash table key In the domain field Plus the increment 1 -1
hsetnx <key><field><value> # Hash table key In the domain field Is set to value , If and only if domain field non-existent .
Hash There are two kinds of data structures corresponding to types :ziplist( Compressed list ),hashtable( Hashtable ). When field-value When the length is short and the number is small , Use ziplist, Otherwise use hashtable.
7、Zset( Ordered set )
Redis Ordered set zset With the common set set Very similar , Is a collection of strings without repeating elements .
The difference is that each member of the ordered set is associated with a score , This score (score) Used to sort the members of a set from the lowest score to the highest score . Members of a collection are unique , But the score can be repeated .
Because the elements are ordered , So you can also quickly rate (score) Or order (position) To get a range of elements .
Accessing intermediate elements of ordered collections is also very fast , So you can use ordered sets as a smart list without duplicate members .
key : value|score
zadd <key><score1><value1><score2><value2> # Put one or more member Elements and score Value added to ordered set key among .
zrange <key><start><stop> [WITHSCORES] # Return to ordered set key in , The subscript is <start><stop> Between the elements , belt WITHSCORES, You can return scores and values together to the result set . zrevrange key start stop
zrangebyscore key minmax [withscores] [limit offset count] # Return to ordered set key in , all score The value is between min and max Between ( Including equal to min or max ) Members of . Members of the ordered set press score Value increment ( From small to large ) Order .
zrevrangebyscore key maxmin [withscores] [limit offset count] # ditto , Change to big to small .
zincrby <key> <increment> <value> For the elements value Of score Plus the increment
zrem <key><value> # Delete... Under this collection , The element that specifies the value
zcount <key><min><max> # Count the set , The number of elements in the fraction range
zrank <key><value> # Returns the rank of the value in the collection , from 0 Start . # zrevrank key value
SortedSet(zset) yes Redis A very special data structure provided , On the one hand, it is equivalent to Java Data structure of Map<String, Double>, You can give each element value Give a weight score, On the other hand, it is similar to TreeSet, Internal elements are weighted score Sort , You can get the rank of each element , You can also use score To get a list of elements .
zset The bottom layer uses two data structures
(1)hash,hash The role of is to associate elements value And weight score, Guarantee elements value Uniqueness , You can use the element value Find the appropriate score value .
(2) Skip list , The purpose of jump tables is to give elements value Sort , according to score Scope get element list for .
边栏推荐
- Source code of YY music wechat applet imitating Netease cloud music
- WebMvcConfigurationSupport
- Swap, move, forward, exchange of utility component learning
- utility实用组件学习之swap,move,forward,exchange
- [QNX hypervisor 2.2 user manual]9.7 generate
- Duplicate numbers in array
- 2022 Niuke multi School Game 2
- MVVM model
- initializer_ List tool library learning
- Programmer interview Golden Classic 4.12 summation path
猜你喜欢

WebMvcConfigurationSupport

Unexpected dubug tricks
![[Database Foundation] summary of MySQL Foundation](/img/89/e22c232b0183eaae35a9f45a40ff36.jpg)
[Database Foundation] summary of MySQL Foundation

Data broker understanding

Docker 安装 Redis-5.0.12(远程访问)

Solution of phpstudy service environment 80 port occupied by process system under Windows

chown: changing ownership of ‘/var/lib/mysql/‘: Operation not permitted

What is the difference between'1 and'b1 when assigning values

【JUC】并发需要了解的关键字volatile

POI特效 市场调研
随机推荐
Grain Academy p98 trample pit e.globalexceptionhandler: null
redis-基本数据类型(String/list/Set/Hash/Zset)
E-commerce RPA, a magic weapon to promote easy entry
@Import
Solution of phpstudy service environment 80 port occupied by process system under Windows
Unexpected dubug tricks
[Muduo] thread package
152. 乘积最大子数组-动态规划
Mongodb update operator (modifier)
762. 二进制表示中质数个计算置位
Qt风格(QSS)应用之QProgressBar
Implementation of mesh parameterized least squares conformal maps (3D mesh mapping to 2D plane)
[wechat applet] page navigation
R language installation tutorial | graphic introduction is super detailed
Which securities company should a novice choose to open an account? Is it safe?
How does Navicat modify the language (Chinese or English)?
TS basic data type
Why are there many snapshot tables in the BI system?
Simulate and implement common interfaces of string class
Docker 安装 Redis-5.0.12(远程访问)


