Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to analyze how to get started with Redis

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/01 Report--

How to analyze the introduction to Redis, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain in detail for you, people with this need can come to learn, I hope you can gain something.

1. Brief introduction to Redis

Redis (REmote dictionary server) remote dictionary server that stores data in a dictionary structure.

Currently supported key value types include:

Strings, hashes, lists, collection types, ordered collections

Redis all the data in memory, read fast, but after the program exits, memory data will be lost, so redis supports asynchronous data persistence.

Redis is rich in features and can also be used as a cache and queuing system.

Redis is similar to memcached in that it is an in-memory database based on key value.

Easy to use, if the sql of the query is as follows

Select title from posts where id ='1' limit 1

If you use the HGET post:1 title statement, it is as follows:

Dozens of client programming languages are supported and rich libraries are provided.

2 getting started Command Test

2.1 multiple databases

Redis provides 16 databases by default, 0-15. The tests are as follows

Redis 127.0.0.1 > pingPONGredis 127.0.0.1 > KEYS * 1) "logn" redis 127.0.0.1 > select (error) ERR invalid DB indexredis 127.0.0.1 > select 16 (error) ERR invalid DB indexredis 127.0.16379 [16] > select 15OKredis 127.0.0.16379 [15] > KEYS * (empty list or set) redis 127.0.0.1 > select 0OKredis 127.0.0.1 redis 6379 > keys * 1) "logn" redis 127.0.0.1

Switch databases through the select command

Database renaming is not supported

An empty redis instance takes up 1m of memory, which is lightweight

2.2 key-value related commands

SET

GET

KEYS

DEL

TYPE

EXISTS

Redis 127.0.0.1 redis 6379 > SET logn='helloredis' (error) ERR wrong number of arguments for 'set' commandredis 127.0.0.1 error 6379 > SET logn='helloredis' (error) ERR wrong number of arguments for' set' commandredis 127.0.0.1 error 6379 > SET logn helloredisOKredis 127.0.0.1 helloredis > GET logn "helloredis" redis 127.0.0.1 > TYPE lognstringredis 127.0.0.1 > EXIST logn (error) ERR unknown command 'EXIST'redis 127.0.0 .11redis 6379 > EXISTS logn (integer) 1redis 127.0.0.1 1redis 6379 > DEL logn (integer) 1redis 127.0.0.1 1redis 6379 > DEL logn (integer) 0redis 127.0.0.1 DEL logn 6379 > EXISTS logn (integer) 0redis 127.0.0.1 > 2.3 incr and decr

Redis 127.0.0.1 1redis 127.0.0.1 1redis 6379 > incr num (integer) 2redis 127.0.0.1 1redis 6379 > incr num (integer) 3redis 127.0.0.1 integer 6379 > incrby num 4 (integer) 7redis 127.0.0.1 integer 6379 > decrby num 2 (integer) 5redis 127.0.0.1 integer 6379 > decr num (integer) 4redis 127.0.1integer 6379 > 2.4 batch settings MGETredis 127.0.0.1 3OKredis 6379 > MSET a11 a2 2 a3 3OKredis 127.0.1 0.1 3OKredis 6379 > MGET a11) "1" redis 127.0.1 0.1 3OKredis 6379 > KEYS * 1) "a2" 2) "a3" 3) "num" 4) "A1" redis 127.0.1 1 3OKredis 6379 > MGET A21) "2" redis 127.0.1 3OKredis 6379 > MGET A31) "3" redis 127.0.0.1 3OKredis 6379 > 2.5 hash type

Car:1- > name--- > value

HSET, HGET

Redis 127.0.0.1 0redis 127.0.0.1 0redis 6379 > HSET car1 price 30w (integer) 1redis 127.0.0.1 HSET car1 price 6379 > HSET car2 name AUDIO (integer) 0redis 127.0.0.1 HSET car2 price 20w (integer) 1redis 127.0.16379 > HGET car2 name "AUDIO" redis 127.0.0.16379 > HGET car1 price "30w" redis 127.0.1 >

Set up multiple field to take advantage of HMSET

Redis 127.0.0.1 6379 > HMSET car3 name buick price 10wOK

Determine whether there is HEXISTS in field

Redis 127.0.0.1 1redis 6379 > HEXISTS car3 name (integer) 1redis 127.0.0.1 1redis 6379 > HEXISTS car3 notexist (integer) 0

Add command HINCRBY

Delete the command HDEL

Get field or value

Redis 127.0.0.1 redis 6379 > HKEYS car11) "name" 2) "price" redis 127.0.0.1 name > HKEYS car21) "name" 2) "price" redis 127.0.0.1 name 6379 > HKEYS car31) "name" 2) "price" redis 127.0.0.1redis 6379 > HVALS car11) "AUDIO" 2) "30w" redis 127.0.1 > HVALS car21) "AUDIO" 2) "20w" redis 127.0. 0.1 6379 > HVALS car31) "buick" 2) "10w" redis 127.0.0.1 redis 6379 > 2.6 list type

1, a list type key is the same as a hash, up to 2 ^ 32-1 elements

two。 Using a two-way linked list, the time complexity of adding elements to both ends is o (1), and the closer the elements are to the two ends, the faster it is to get them. This means that a list of tens of millions of elements, or ten records at the head or tail, is also fast. (the speed is the same as getting 10 records at the head or tail in a list of only 20 elements). Both ends of the insertion time complexity is also o (1) this feature is used in social networks for new things, log management, etc., can solve the relational database can not cope with the situation.

3. The disadvantage is that access through the index is relatively slow, one by one to find.

Common commands, LPUSH,LPOP,RPUSH,RPOP,LRANGE

Redis 127.0.0.1 1redis 127.0.0.1 1redis 6379 > LPUSH numbers 2 1 (error) ERR wrong number of arguments for 'lpush' commandredis 127.0.0.1 LPUSH numbers 6379 > LPUSH numbers 2 (integer) 2redis 127.0.0.1 RPUSH numbers 5 (integer) 3redis 127.0.1 > LPOP numbers "2" redis 127.0.1 > RPOO number (error) ERR unknown command' RPOO'redis 127.0.1 > RPOO numbers (error) ERR unknown command 'RPOO'redis 127.0.0.1 LRANGE numbers 6379 > RPOP numbers "5" redis 127.0.0.1 ERR unknown command 6379 > LRANGE 02 (error) ERR wrong number of arguments for' lrange' commandredis 127.0.0.1 ERR unknown command 6379 > LRANGE numbers 021) "3" redis 127.0.0.1 LRANGE numbers 6379 > 2.7Collection type redis 127.0.0.1 > SADD letters a b c (integer) 3redis 127.0.1 > SMEMBERS letters1) "c "2)" a "3)" b "redis 127.0.0.1 srem' commanredis 6379 > SREM a (error) ERR wrong number of arguments for 'srem' commanredis 127.0.1 srem' commanredis 6379 > SREM letters a (integer) 1redis 127.0.0.1 SDIFF letters letters21)" b "redis 127.0.0.1 > SDIFF letters2 letters1)" d "2)" e "redis 127.0.0. 1RV 6379 > SMEMBERS letters1) "c" 2) "b" redis 127.0.0.1 SMEMBERS letters21) "c" 2) "d" 3) "e" redis 127.0.1 RV 6379 > is it helpful for you to read the above contents? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report