Briefly talk about redis (on)

1. What is it?

Redis is an excellent nosql (non-relational), key-vlaue, memory-level database! Redis is single-threaded, and the operation is safe!
Relational: This type of database uses a two-dimensional table to represent and store real-life objects. oracle, mysql
non-relational (nosql): no two-dimensional tables, such as key-value, documents and other data. Redis, MongoDB, Hadoop

2. Features

1. Redis supports data persistence and will persist to disk according to a certain strategy, and will not lose data even if the power is cut off.
2. Redis not only supports simple key-value type data, but also provides list, set, Storage of data structures such as zset, hash, etc.
3. Redis supports data backup, that is, data backup in master-slave mode.

3. Data and application scenarios

1.String: String is the most basic type of redis, a key corresponds to a value. The string type is binary safe. Means redis string can contain any data. Such as images or serialized objects. The String type is the most basic data type of Redis, and a key can store a maximum of 512MB.
Common commands:
① get, get the value stored in the specified key
② set, set the value stored in the specified key
③ del, delete the value stored in the specified key (this command can be used for all types)
Usage scenarios: use incr generates id, decr reduces inventory, cache - expiration time setting, simulate session

-

2.list: Redis lists are simple lists of strings, sorted by insertion order. You can add an element to the head (left) or tail (right) of the list.
Common commands:
① rpush, push the given value to the right end of the list
② lrange, get all the values ​​of the list in the specified range
③ lindex, get a single element of the list in the specified range
Usage scenario: multitask scheduling queue

-

3.set: Redis's Set is an unordered collection of string type. Sets are implemented through hash tables, so adding, deleting, and searching are all O(1).
Common commands:
①sadd, add a given element to the set
②smembers, return all elements contained in the set
③sismember, check whether the specified element exists in the set
Usage scenario: number of followers on Weibo

-

4.hash : Redis hash is a set of key-value (key=>value) pairs. Redis hash is a mapping table of field and value of string type, and hash is especially suitable for storing objects.
Common commands:
①hset, associate the specified key-value pair in the hash
②hget, get the value of the specified hash key
③hgetall, get all the key-value pairs contained in the
hash ④hdel, if the given key exists in the hash, then move Except this key
usage scenario: shopping cart

-

5.zset: Redis zset, like set, is also a collection of string type elements, and does not allow duplicate members. The difference is that each element is associated with a fraction of type double. Redis sorts the members of the set from small to large through scores. The members of zset are unique, but the score can be repeated.
Common commands:
① zadd, add a member with a given score to the ordered set
② zrange, according to the position of the element in the ordered arrangement, get multiple elements from the ordered set
③ zrangebyscore, get the ordered set All elements in the given score range ④zrem, if the specified member exists in the ordered set, then remove this member
. Usage scenario: leaderboard

4: Endurance

  1. rdb snapshot: By default redis will persist data to disk in the form of snapshots (a binary file, dump.rdb, the file name can be specified), the format in the configuration file (redis.conf) is: save
    NM means Within N seconds, if redis is modified at least M times, then redis takes snapshots to disk. Of course, we can also manually execute save or bgsave (asynchronously) to take snapshots.
################################ SNAPSHOTTING  #################################
#
# Save the DB on disk:
#
#   save <seconds> <changes>
#
#   Will save the DB if both the given number of seconds and the given
#   number of write operations against the DB occurred.
#
#   In the example below the behaviour will be to save:
#   after 900 sec (15 min) if at least 1 key changed
#   after 300 sec (5 min) if at least 10 keys changed
#   after 60 sec if at least 10000 keys changed
#
#   Note: you can disable saving at all commenting all the "save" lines.

save 900 1
save 300 10
save 60 10000
  1. aof command copy: AOF persistently records all write commands performed by the server and restores the dataset by re-executing these commands when the server starts. The commands in the AOF file are all saved in the format of the Redis protocol, and new commands will be appended to the end of the file. Redis can also rewrite the AOF file in the background, so that the size of the AOF file does not exceed the actual size required to save the state of the dataset.

5. Advantages

1. Extremely high performance – Redis can read 110,000 times/s and write 81,000 times/s.
2. Rich data types – Redis supports Strings, Lists, Hashes, Sets and Ordered Sets data type operations for binary cases.
3. Atomic – All operations in Redis are atomic, meaning that they either execute successfully or fail to execute at all. A single operation is atomic. Transactions are also supported for multiple operations, i.e. atomicity, wrapped by MULTI and EXEC instructions.
4. Rich features - Redis also supports publish/subscribe, notification, key expiration and other features.

6. Access

Java access redis:

1. Use the jedisjava client to access the redis server, which has the same advantages as jdbc to access the db.
2. If spring is integrated, you can use springDataRedis to access redis. springDataRedis is just a secondary encapsulation of jedis.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325927181&siteId=291194637
Recommended