The dependencies are as follows:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId></dependency>
The configuration file is as follows:
spring:redis: open: true # Whether to enable redis cache true to enable false to turn off database: 0 host: 47.104.208.124 port: 6378 password: lf.1228 # Password (default is empty) timeout: 6000 # Connection timeout (milliseconds) pool: max-active: 1000 # Maximum number of connection pool connection pool (using negative values means no limit) max-wait: -1 # Maximum blocking waiting time for connection pool (using negative values means no limit) max-idle: 10 # Maximum idle connection in connection pool min-idle: 5 # Minimum idle connection in connection pool
RedisConfig class:
@Configurationpublic class RedisConfig { @Autowired private RedisConnectionFactory factory; @Bean public RedisTemplate<String, Object> redisTemplate() { RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>(); redisTemplate.setKeySerializer(new StringRedisSerializer()); redisTemplate.setHashKeySerializer(new StringRedisSerializer()); redisTemplate.setHashValueSerializer(new StringRedisSerializer()); redisTemplate.setValueSerializer(new StringRedisSerializer()); redisTemplate.setConnectionFactory(factory); return redisTemplate; } @Bean public HashOperations<String, String, Object> hashOperations(RedisTemplate<String, Object> redisTemplate) { return redisTemplate.opsForHash(); } @Bean public ValueOperations<String, String> valueOperations(RedisTemplate<String, String> redisTemplate) { return redisTemplate.opsForValue(); } @Bean public ListOperations<String, Object> listOperations(RedisTemplate<String, Object> redisTemplate) { return redisTemplate.opsForList(); } @Bean public SetOperations<String, Object> setOperations(RedisTemplate<String, Object> redisTemplate) { return redisTemplate.opsForSet(); } @Bean public ZSetOperations<String, Object> zSetOperations(RedisTemplate<String, Object> redisTemplate) { return redisTemplate.opsForZSet(); }}RedisUtils are as follows:
@Componentpublic class RedisUtils { @Autowired private RedisTemplate<String, Object> redisTemplate; @Autowired private ValueOperations<String, String> valueOperations; @Autowired private HashOperations<String, String, Object> hashOperations; @Autowired private ListOperations<String, Object> listOperations; @Autowired private SetOperations<String, Object> setOperations; @Autowired private ZSetOperations<String, Object> zSetOperations; /** Default expiration time, unit: seconds*/ public final static long DEFAULT_EXPIRE = 60 * 60 * 24; /** Do not set expiration time*/ public final static long NOT_EXPIRE = -1; private final static Gson gson = new Gson(); public void set(String key, Object value, long expire){ valueOperations.set(key, toJson(value)); if(expire != NOT_EXPIRE){ redisTemplate.expire(key, expire, TimeUnit.SECONDS); } } public void set(String key, Object value){ set(key, value, DEFAULT_EXPIRE); } public <T> T get(String key, Class<T> clazz, long expire) { String value = valueOperations.get(key); if(expire != NOT_EXPIRE){ redisTemplate.expire(key, expire, TimeUnit.SECONDS); } return value == null ? null : fromJson(value, clazz); } public <T> T get(String key, Class<T> clazz) { return get(key, clazz, NOT_EXPIRE); } public String get(String key, long expire) { String value = valueOperations.get(key); if(expire != NOT_EXPIRE){ redisTemplate.expire(key, expire, TimeUnit.SECONDS); } return value; } public String get(String key) { return get(key, NOT_EXPIRE); } public void delete(String key) { redisTemplate.delete(key); } /** * Object converted to JSON data*/ private String toJson(Object object){ if(object instanceof Integer || object instanceof Long || object instanceof Float || object instanceof Double || object instanceof Boolean || object instanceof String){ return String.valueOf(object); } return gson.toJson(object); } /** * JSON data, convert to Object */ private <T> T fromJson(String json, Class<T> clazz){ return gson.fromJson(json, clazz); }}How to encapsulate redis in springboot:
RedisTemplate
Package: org.springframework.data.redis.core
Function: redis template, redis transaction, serialization support, operation of redis methods
JedisConnectionFactory
Package: org.springframework.data.redis.connection.jedis
Function: Redis connection factory class, create redis connection pool, etc.
RedisAutoConfiguration
Package: org.springframework.boot.autoconfigure.data.redis
Function: Inject redis configuration file related information into the factory class
RedisProperties
Package: org.springframework.boot.autoconfigure.data.redis
Function: Redis connects the basic class to inject configuration information into properties through the @ConfigurationProperties annotation
Summarize
The above is the tutorial on using Redis in springboot introduced to you by the editor. I hope it will be helpful to you. If you have any questions, please leave me a message and the editor will reply to you in time. Thank you very much for your support to Wulin.com website!