Dependencies
= { = "<version>" }
Configuration items
[]
= "redis://127.0.0.1/" # redis database address
# The following are all optional configurations
= 10000 # Connection timeout, in milliseconds
= 1000 # Response timeout, in milliseconds
= 6 # Retry times, interval time increases exponentially
= 2 # Interval time exponential base, unit milliseconds
= 100 # Interval time growth factor, default 100 times growth
= 60000 # Maximum interval time
Component
After configuring the above configuration items, the plugin will automatically register a Redis connection management object. This object is an alias of redis::aio::ConnectionManager.
pub type Redis = ConnectionManager;
Extract the Component registered by the plugin
The RedisPlugin plugin automatically registers a connection management object for us. We can use Component to extract this connection pool from AppState. Component is an axum extractor.
async
cache macro
spring-redis provides a transparent cache for asynchronous functions based on Redis. Add the cache macro to the async method to cache the function result.
The example is as follows:
async
The cache macro supports three optional parameters: expire, condition, and unless. For details, please refer to the cache document.
The function wrapped by cache must meet the following requirements:
- Must be
async fn - Can return
Result<T, E>or a normal valueT - The return type must implement
serde::Serializeandserde::Deserialize, and the underlyingserde_jsonis used for serialization
Complete code reference redis-example