Redis
Redis is a cache, Message Queuing, and multiple types of key-value storage services.
Spring Boot
Spring boot provides automatic injection configuration for lettcue and Jedis clients, and provides abstract interfaces through Spring-data-redis
Configure the Connection Redis service and interface call 1. Join the dependency
Dependencies are pom.xml added to the dependency collection org.springframework.boot:spring-boot-starter-data-reids , which is configured as follows
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <!-- 里面依赖了spring-data-redis --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency> </dependencies>
Default use Lettuce as Client
2. Modify the configuration file
Add Redis-related configuration to the spring boot configuration file as application.yaml An example (other format profiles, self-converting)
spring: redis: # 其他配置信息有缺省 host: localhost port: 6379 timeout: 500 pool: min-idle: 1 max-idle: 8 max-active: 8
3. Bean Injection using
Spring Boot automatically injects management as soon as the configuration is complete. RedisTemplate you can manipulate Redis with this object.
In accordance with my previous concise approach, I 在RedisTemple was encapsulated into a straightforward operation. Manage below
RedisManager.java
package info.chiwm.boot.manager;import Org.springframework.beans.factory.annotation.autowired;import Org.springframework.data.redis.core.stringredistemplate;import Org.springframework.stereotype.component;import javax.annotation.postconstruct;/** * @author [email protected] * @ClassName: Redismanager * @Description: * @date 20 18/1/10 pm 3:40 */@Componentpublic class Redismanager {@Autowired private stringredistemplate redistemplate; private static Redismanager Redismanager; @PostConstruct public void, init () {Redismanager = this; /** * Redis Set String Ops * * @param key * @param value */public static void Set (String key, String value) {RedisManager.redisTemplate.opsForValue (). Set (key, value); /** * Redis Get string Ops * @param key * @return */public static string get (String key) { Return RedisManager.redisTemplate.opsForValue (). get (key); }}
Call the static method directly, convenient to call the Redis corresponding set key command. If additional storage types and operations are required. RedisManagerstatic methods can be added on.
Spring Boot + Spring-data-redis