This is the 19th day of my participation in the August More Text Challenge

define

  • Redis is an open source (BSD licensed), memory-stored data structure server that can be used as a database, cache, and message queue agent. It supports string, hash, list, set, ordered set, bitmap, Hyperloglogs and other data types. Built-in replication, Lua scripting, LRU recall, transactions, and different levels of disk persistence, as well as high availability through Redis Sentinel and automatic partitioning through Redis Cluster.

Redis general usage scenarios

1. Cache: Use different cache data types according to requirements to reduce the pressure on the database

2, leaderboards: Ordered set ZSET can achieve various types of leaderboards. Almost all leaderboards are implemented through ordered collections

3, counter: increment() by Redis

4. Social network: “like”, “follow”, “be followed” and other functions (” like “and other high-frequency operations will affect the performance of the database if every operation has to deal with the database)

5, distributed lock: redisTemplate opsForValue () setIfAbsent can realize distributed lock (key, value)

Message queues: There are several ways to implement message queues, such as subscribing/publishing

6 data type storage (listing some commonly used expressions)

1, the String (String) can keep writing: String, integer, or floating-point Numbers redisTemplate. OpsForValue (). The set (key, value) (get (key) to read)

Replacement: redisTemplate. OpsForValue (.) getAndSet cover (key, value) and returns the old value

Delete: redistemplate. delete(key) Universal delete. Returns a bool

2, list (redis is two-way chain table) linked list and array: an array of storage is adjacent storage, so the query is very quickly, and the chain table query is slow, in order to query a position data must be checked to pour a – 1 position But insert more quickly in the list, you just need to move the cursor, and an array of query slower, need to be backward position behind the location of the data

The set in Redis is a hash table structure. The data in the set is unordered and non-repeatable, and each element is of String type

Add: redistemplate.opsForset ().add(key, values) Returns the number of successful additions

Difference: redistemplate.opsForset ().difference(key, key2)

Union: redistemplate.opsforset ().union(key, otherKey)

All elements: redistemplate.opsForset ().members(key) Returns all elements of the collection

4. Ordered sets are similar to sets except that they are ordered. The main difference from unordered sets is that each element has an extra fraction in addition to its value. A fraction is a floating point number, represented in Java as a double precision, and Redis supports sorting the fraction from smallest to largest or from largest to smallest. Each element is unique, but the score can be the same for different elements

Add: redistemplate.opsforzset ().add(key, value, score) returns the number of successful additions

Remove: redistemplate.opsForzSet ().remove(key, values) returns the difference set

Redistemplate.opsforzset ().rank(key, value)

Interval sort: redistemplate.opsForzSet ().range(key, start, end) interval sort set

5. Hash A key can store multiple key-value pairs. In fact, hash algorithm is used at the bottom of the hash table

Add: redistemplate.opsForHash ().put(key, hashKey, value)

Delete: redistemplate.opsForhash ().delete(key, hashKeys)

Get: redistemplate.opsForhash ().get(key, hashKey) Gets the hashKey under the specified key

Validation: redistemplate.opsForHash ().haskey (key, hashKey) Verifies that the key has a hashKey

There’s also the cardinality that you can figure out for yourself

conclusion

Today is mainly to share some basic knowledge of Redis first to know what Redis is, why to use it (application scenario), in such a background to understand how it is to store data. Redis has a lot of content such as distributed locks that will be shared later