SpringBoot——redis、SpringCache(二十)
依赖、配置
1 2 3 4 5 6 7 8 9
| <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-cache</artifactId> </dependency>
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis</artifactId> </dependency>
|
redis请见SpringBoot——redis(十九)
配置类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
| package com.qn.config.redisConfig;
import com.alibaba.druid.support.json.JSONUtils; import org.springframework.boot.SpringBootConfiguration; import org.springframework.cache.annotation.EnableCaching; import org.springframework.cache.interceptor.KeyGenerator; import org.springframework.context.annotation.Bean; import org.springframework.data.redis.cache.RedisCacheConfiguration; import org.springframework.data.redis.cache.RedisCacheManager; import org.springframework.data.redis.cache.RedisCacheWriter; import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer; import org.springframework.data.redis.serializer.RedisSerializationContext; import org.springframework.data.redis.serializer.StringRedisSerializer;
import java.io.Serializable; import java.time.Duration;
@SpringBootConfiguration @EnableCaching public class LettuceRedisConfig {
@Bean public RedisTemplate<String, Serializable> redisTemplate(LettuceConnectionFactory connectionFactory) {
RedisTemplate<String, Serializable> redisTemplate = new RedisTemplate<>(); redisTemplate.setConnectionFactory(connectionFactory); redisTemplate.setKeySerializer(new StringRedisSerializer()); redisTemplate.setValueSerializer(new GenericJackson2JsonRedisSerializer()); redisTemplate.setHashKeySerializer(new StringRedisSerializer()); redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer()); redisTemplate.afterPropertiesSet(); return redisTemplate; }
@Bean public RedisCacheManager cacheManager(RedisTemplate redisTemplate) { RedisCacheWriter redisCacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(redisTemplate.getConnectionFactory()); RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig() .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(redisTemplate.getValueSerializer())) .disableCachingNullValues() .entryTtl(Duration.ofHours(1)); return new RedisCacheManager(redisCacheWriter, redisCacheConfiguration); }
@Bean public KeyGenerator cacheKeyGenerator() { return (target, method, params) -> { StringBuilder sb = new StringBuilder(); sb.append(target.getClass().getName()); sb.append(method.getName()); for (Object obj : params) { sb.append(JSONUtils.toJSONString(obj).hashCode()); } return sb.toString(); }; } }
|
主要是开启缓存使用及配置自定义缓存key
业务操作类
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
| package com.qn.service;
import com.qn.eneity.User; import com.qn.dao.boot.UserMapperAnocation; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cache.annotation.Cacheable; import org.springframework.stereotype.Service;
import java.util.Date; import java.util.List;
@Service public class UserServiceAnocation { @Autowired UserMapperAnocation userMapperAnocation;
@Cacheable(value = "user", key = "#id",unless="#result == null||#result.size()<1") public List<User> getUserById(Integer id) { System.out.println(new Date().toString()); return userMapperAnocation.getUserById(id); }
@Cacheable(value = "user", keyGenerator = "cacheKeyGenerator",unless="#result == null||#result.size()<1") public List<User> getAllUser() { System.out.println(new Date().toString()); return userMapperAnocation.getAllUsers(); } }
|
先查询redis,redis不存在,查询数据库,并将值根据key存入redis库。