Redis development and use guide
Redis
Redis is an open source (BSD licensed) in memory data structure storage, which is used as database, cache and message agent. Redis provides data structures, such as strings, hashes, lists, sets, sorted sets with range queries, bitmaps, hyperlogs, geospatial indexes, and streams. Redis has built-in replication, Lua scripts, LRU instances, transactions, and different levels of disk persistence through redis sentinel and Redis cluster's automatic partition provides high availability.
1、 Redis basic data structure
1. String
String type is the most basic data storage type in redis. It is binary safe in redis, which means that this type can accept data in any format, such as JPEG image data or JSON object description information. In redis, the maximum data length that can be accommodated by string type value is 512M.
Common commands:
be careful:
2. Hash
The hash type in redis can be regarded as a map container of string key and string value. Therefore, this type is very suitable for storing object information.
Common commands:
3. List
In redis, the list type is a string linked list sorted by insertion order. Like the ordinary linked list in the database structure, new elements can be added at the head and tail. If the key does not exist during insertion, redis will create a new linked list for the key. On the contrary, if all the elements in the linked list are removed, the key will also be deleted from the database.
Common commands:
4. Set
Redis set is an unordered set of string type. Collection members are unique, which means that duplicate data cannot appear in the collection. Collections in redis are implemented through hash tables, so the complexity of adding, deleting and searching is O (1). The maximum number of members in the collection is 2 ^ 32 - 1
Common commands:
5. Sorted set
Redis ordered collections, like collections, are collections of string elements, and duplicate members are not allowed. The difference is that each element is associated with a score of type double. Redis sorts the members of the collection from small to large through scores.
Common commands:
2、 Advanced data structure of redis
1. HyperLogLog
Redis on 2.8 Version 9 adds the hyperlog structure. Redis hyperloglog is an algorithm for cardinality statistics. The advantage of hyperloglog is that when the number or volume of input elements is very, very large, the space required to calculate the cardinality is always fixed and very small. However, this is an estimate, with a certain error.
Common commands:
2. GEO
Redis geo is mainly used to store and operate geographic location information. This function is available in redis3 2 version added
Common commands:
3. BitMap
The principle of bitmap has been mentioned in the previous article. It can be used as a storage of large amounts of data, but the stored content can only be 0 or 1 It can be used in the online status of 1 billion users. 1 represents online and 0 represents offline.
Value value can only be 0, 1
3、 Redis advanced features
1. Redis transaction
The transaction concept of redis is different from that of the database. Redis will serialize all commands in a transaction and then execute them in order. Redis cannot insert and execute the request sent by another client during the execution of a redis transaction. The failure of any command in the transaction will not affect the execution of other commands or roll back.
2. Publish and subscribe
Publish subscribe is a communication mode in which the sender sends a message and the subscriber accepts the message. The client can subscribe to multiple channels, and then send new messages to the channel. The client subscribing to the channel can receive messages.
Common commands:
3. Script
Redis script executes the script through Lua interpreter. Redis version 2.6 supports Lua environment through embedding
The basic syntax is as follows: Eval script numkeys key [key...] arg [arg ...]
example:
EVAL "return {KEYS[1],KEYS[2],ARGV[1],ARGV[2]}" 2 key1 key2 first second
4. Redis Stream
Redis stream is a new data structure in version 5.0. Redis stream is mainly used for message queuing. Redis itself has a publish / subscribe function, but it has a disadvantage that messages are not persistent. If the network is interrupted or down, data will be lost.
Redis stream provides the functions of message persistence and active / standby replication. It has a message linked list to string all added messages. Each message has a unique ID and content.
Common commands:
Start from scratch:
Start consuming from the tail and only accept new news
第二个group :消费组名
consumer: 消费者名
count :读取数量
milliseconds : 阻塞毫秒数
key :队列名
ID:消息id
4、 Redis usage scenario
1. Business data cache
2. Business data processing
3. Global consistent count
4. Efficient statistical counting
5. Publish, subscribe and stream
Subscription mode for message publishing
6. Distributed lock
set key my_random_value NX PX 30000
if redis.call("get",KEYS[1])==ARGV[1] then
return redis.call("del",KEYS[1])
else
return 0
end
5、 Redis java client
1. Jedis
Based on bio and thread insecurity, connection pool management needs to be configured
2. Lettuce
At present, the mainstream recommended driver is based on netty NiO and API thread safety
3. Redission
Based on netty NiO, API thread safety. A large number of rich distributed functions, such as distributed basic data types and locks.
6、 Project integration
1. Spring data redis can be introduced into the springmvc project
Maven dependency
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-redis</artifactId>
<version>2.1.2.RELEASE</version>
</dependency>
The core is redistemplate (which can be configured based on jedis, lattice and redisson), which encapsulates the basic redis commands.
2. Springboot access (lettuce is used by default)
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
Configure spring redis
For example: spring redis. host=127.0. zero point one
3. Spring cache integrates redis
@EnableCaching
@Override
@Cacheable(value = "userCache")
public User getUser(Integer id) {
return userMapper.getUser(id);
}
@Configuration
public class CacheConfig {
@Bean
public RedisCacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) {
return RedisCacheManager.create(redisConnectionFactory);
}
}
Fan Wai:
There is a hole in this problem. First, redis is multi-threaded as a process. For example, redis deletes objects in the background through multithreading, and blocking commands implemented through redis module The place of single thread is to detect which has received the request data - > Data Processing - > return data. Other time-consuming operations use other threads.
To detect which client has accepted the request, the IO multiplexing model is used. "Multiplexing" refers to multiple network connections, "multiplexing" refers to multiplexing the same thread.
Because redis is a memory based operation, CPU is not the bottleneck. The bottleneck lies in the size of machine memory or network bandwidth. 3. What is multithreading after redis6?
The IO model uses the multi-threaded NiO model, and the memory processing thread is also a single thread.