Redis之sql緩存的具體使用
1.環(huán)境搭建
<!-- RedisTemplate -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
spring:
redis:
host: 192.168.8.128
port: 6380
password: 1234
database: 0
timeout: 3000
jedis:
pool:
max-wait: -1
max-active: -1
max-idle: 20
min-idle: 10

2.Redis配置
package com.yzm.redis01.config;
import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonTypeInfo;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
@Configuration
public class ObjectMapperConfig {
private static final String PATTERN = "yyyy-MM-dd HH:mm:ss";
@Bean(name = "myObjectMapper")
public ObjectMapper objectMapper() {
JavaTimeModule javaTimeModule = new JavaTimeModule();
javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer());
javaTimeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer());
return new ObjectMapper()
// 轉(zhuǎn)換為格式化的json(控制臺(tái)打印時(shí),自動(dòng)格式化規(guī)范)
//.enable(SerializationFeature.INDENT_OUTPUT)
// Include.ALWAYS 是序列化對(duì)像所有屬性(默認(rèn))
// Include.NON_NULL 只有不為null的字段才被序列化,屬性為NULL 不序列化
// Include.NON_EMPTY 如果為null或者 空字符串和空集合都不會(huì)被序列化
// Include.NON_DEFAULT 屬性為默認(rèn)值不序列化
.setSerializationInclusion(JsonInclude.Include.NON_NULL)
// 如果是空對(duì)象的時(shí)候,不拋異常
.configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false)
// 反序列化的時(shí)候如果多了其他屬性,不拋出異常
.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
// 取消時(shí)間的轉(zhuǎn)化格式,默認(rèn)是時(shí)間戳,可以取消,同時(shí)需要設(shè)置要表現(xiàn)的時(shí)間格式
.configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false)
.setDateFormat(new SimpleDateFormat(PATTERN))
// 對(duì)LocalDateTime序列化跟反序列化
.registerModule(javaTimeModule)
.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY)
// 此項(xiàng)必須配置,否則會(huì)報(bào)java.lang.ClassCastException: java.util.LinkedHashMap cannot be cast to XXX
.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.PROPERTY)
;
}
static class LocalDateTimeSerializer extends JsonSerializer<LocalDateTime> {
@Override
public void serialize(LocalDateTime value, JsonGenerator gen, SerializerProvider serializers) throws IOException {
gen.writeString(value.format(DateTimeFormatter.ofPattern(PATTERN)));
}
}
static class LocalDateTimeDeserializer extends JsonDeserializer<LocalDateTime> {
@Override
public LocalDateTime deserialize(JsonParser p, DeserializationContext deserializationContext) throws IOException {
return LocalDateTime.parse(p.getValueAsString(), DateTimeFormatter.ofPattern(PATTERN));
}
}
}
package com.yzm.redis01.config;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.cache.interceptor.SimpleKey;
import java.lang.reflect.Method;
import java.util.Arrays;
/**
* key生成器
*/
@Slf4j
public class MyKeyGenerator implements KeyGenerator {
private static final String NO_PARAM = "[]";
private static final String NULL_PARAM = "_";
@Override
public Object generate(Object target, Method method, Object... params) {
StringBuilder key = new StringBuilder();
key.append(target.getClass().getSimpleName()).append(".").append(method.getName()).append(":");
if (params.length == 0) {
return new SimpleKey(key.append(NO_PARAM).toString());
}
return new SimpleKey(key.append(Arrays.toString(params).replace("null", NULL_PARAM)).toString());
}
}
package com.yzm.redis01.config;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
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.RedisConnectionFactory;
import org.springframework.data.redis.core.*;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import javax.annotation.Resource;
import java.time.Duration;
@Configuration
@EnableCaching // 啟動(dòng)緩存
public class RedisConfig {
@Bean(name = "myKeyGenerator")
public MyKeyGenerator myKeyGenerator() {
return new MyKeyGenerator();
}
@Resource(name = "myObjectMapper")
private ObjectMapper objectMapper;
/**
* 選擇redis作為默認(rèn)緩存工具
*/
@Bean
public CacheManager cacheManager(RedisConnectionFactory connectionFactory) {
RedisCacheWriter redisCacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(connectionFactory);
RedisCacheConfiguration cacheConfiguration = RedisCacheConfiguration
.defaultCacheConfig()
// 默認(rèn)緩存時(shí)間(秒)
.entryTtl(Duration.ofSeconds(300L))
// 序列化key、value
.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer()))
.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer()))
// 禁用緩存空值
.disableCachingNullValues();
return new RedisCacheManager(redisCacheWriter, cacheConfiguration);
}
/**
* redisTemplate配置
*/
@Bean
public RedisTemplate<String, Object> redisTemplate(RedisConnectionFactory factory) {
RedisTemplate<String, Object> template = new RedisTemplate<>();
// 配置連接工廠
template.setConnectionFactory(factory);
Jackson2JsonRedisSerializer<Object> jacksonSerializer = jackson2JsonRedisSerializer();
StringRedisSerializer stringRedisSerializer = new StringRedisSerializer();
// 使用StringRedisSerializer來(lái)序列化和反序列化redis的key,value采用json序列化
template.setKeySerializer(stringRedisSerializer);
template.setValueSerializer(jacksonSerializer);
// 設(shè)置hash key 和value序列化模式
template.setHashKeySerializer(stringRedisSerializer);
template.setHashValueSerializer(jacksonSerializer);
//支持事務(wù)
template.setEnableTransactionSupport(true);
template.afterPropertiesSet();
return template;
}
private Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer() {
//使用Jackson2JsonRedisSerializer來(lái)序列化和反序列化redis的value值(默認(rèn)使用JDK的序列化方式)
Jackson2JsonRedisSerializer<Object> jacksonSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
jacksonSerializer.setObjectMapper(objectMapper);
return jacksonSerializer;
}
}
3.功能實(shí)現(xiàn)
新增、更新、刪除、查詢數(shù)據(jù)時(shí),對(duì)緩存執(zhí)行對(duì)應(yīng)相同的操作
package com.yzm.redis01.entity;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.Date;
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Accessors(chain = true)
public class User implements Serializable {
private static final long serialVersionUID = -2468903864827432779L;
private Integer id;
private String username;
private String password;
private Date createDate;
private LocalDateTime updateDate;
}
package com.yzm.redis01.service;
import com.yzm.redis01.entity.User;
import java.util.List;
public interface UserService {
User saveUser(User user);
User updateUser(User user);
int deleteUser(Integer id);
void deleteAllCache();
User getUserById(Integer id);
List<User> selectAll();
List<User> findAll(Object... params);
}
為了簡(jiǎn)便,數(shù)據(jù)不從數(shù)據(jù)庫(kù)獲取,這里是創(chuàng)建Map存儲(chǔ)數(shù)據(jù)實(shí)現(xiàn)
package com.yzm.redis01.service.impl;
import com.yzm.redis01.entity.User;
import com.yzm.redis01.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.*;
@Slf4j
@Service
@CacheConfig(cacheNames = "users")
public class UserServiceImpl implements UserService {
private static final Map<Integer, User> userMap;
static {
userMap = new HashMap<>();
userMap.put(userMap.size() + 1, User.builder()
.id(userMap.size() + 1).username("root").password("root").createDate(new Date()).updateDate(LocalDateTime.now()).build());
userMap.put(userMap.size() + 1, User.builder()
.id(userMap.size() + 1).username("admin").password("admin").createDate(new Date()).updateDate(LocalDateTime.now()).build());
}
@Override
@CachePut(key = "#result.id", condition = "#result.id gt 0")
public User saveUser(User user) {
log.info("保存數(shù)據(jù)");
int id = userMap.size() + 1;
User build = User.builder()
.id(id)
.username(user.getUsername())
.password(user.getPassword())
.createDate(new Date())
.updateDate(LocalDateTime.now())
.build();
userMap.put(id, build);
return build;
}
@Override
@CachePut(key = "#user.id", unless = "#result eq null")
public User updateUser(User user) {
log.info("更新數(shù)據(jù)");
if (userMap.containsKey(user.getId())) {
User update = userMap.get(user.getId());
update.setUsername(user.getUsername())
.setPassword(user.getPassword())
.setUpdateDate(LocalDateTime.now());
userMap.replace(user.getId(), update);
return update;
}
return null;
}
@Override
@CacheEvict(key = "#id", condition = "#result gt 0")
public int deleteUser(Integer id) {
log.info("刪除數(shù)據(jù)");
if (userMap.containsKey(id)) {
userMap.remove(id);
return 1;
}
return 0;
}
@Override
@CacheEvict(allEntries = true)
public void deleteAllCache() {
log.info("清空緩存");
}
@Override
@Cacheable(key = "#id", condition = "#id gt 1")
public User getUserById(Integer id) {
log.info("查詢用戶");
return userMap.get(id);
}
@Override
@Cacheable(key = "#root.methodName")
public List<User> selectAll() {
log.info("查詢所有");
return new ArrayList<>(userMap.values());
}
@Override
@Cacheable(keyGenerator = "myKeyGenerator")
public List<User> findAll(Object... params) {
log.info("查詢所有");
return new ArrayList<>(userMap.values());
}
}
package com.yzm.redis01.controller;
import com.yzm.redis01.entity.User;
import com.yzm.redis01.service.UserService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;
@RestController
@RequestMapping("/user")
public class UserController {
private final UserService userService;
public UserController(UserService userService) {
this.userService = userService;
}
@GetMapping("/saveUser")
public void saveUser() {
User user = new User();
user.setUsername("yzm");
user.setPassword("yzm");
System.out.println(userService.saveUser(user));
}
@GetMapping("/updateUser")
public void updateUser(Integer id) {
User user = new User();
user.setId(id);
user.setUsername("yzm");
user.setPassword("123");
System.out.println(userService.updateUser(user));
}
@GetMapping("/deleteUser")
public void deleteUser(@RequestParam Integer id) {
System.out.println(userService.deleteUser(id));
}
@GetMapping("/deleteAllCache")
public void deleteAllCache() {
userService.deleteAllCache();
}
@GetMapping("/getUserById")
public void getUserById(@RequestParam Integer id) {
System.out.println(userService.getUserById(id));
}
@GetMapping("/selectAll")
public void selectAll() {
List<User> users = userService.selectAll();
users.forEach(System.out::println);
}
}
4.緩存注解的使用說(shuō)明
@CacheConfig:注解在類上,表示該類所有緩存方法使用統(tǒng)一指定的緩存區(qū),也可以作用在方法上

@CacheAble:注解在方法上,應(yīng)用到讀數(shù)據(jù)的方法上,如查找方法:調(diào)用方法之前根據(jù)條件判斷是否從緩存獲取相應(yīng)的數(shù)據(jù),緩存沒(méi)有數(shù)據(jù),方法執(zhí)行后添加到緩存

#id 直接使用參數(shù)名
#p0 p0對(duì)應(yīng)參數(shù)列表的第一個(gè)參數(shù),以此類推
#user.id 參數(shù)是對(duì)象時(shí),使用對(duì)象屬性
#root. 可以點(diǎn)出很多方法
#root.methodName
#result 返回值
http://localhost:8080/user/getUserById?id=1

http://localhost:8080/user/getUserById?id=2

http://localhost:8080/user/selectAll

@Cacheable運(yùn)行流程:在調(diào)用方法之前判斷condition,如果為true,則查緩存;沒(méi)有緩存就調(diào)用方法并將數(shù)據(jù)添加到緩存;condition=false就與緩存無(wú)關(guān)了
@CachePut:注解在方法上,應(yīng)用到寫數(shù)據(jù)的方法上,如新增/修改方法,調(diào)用方法之后根據(jù)條件判斷是否添加/更新相應(yīng)的數(shù)據(jù)到緩存:

http://localhost:8080/user/saveUser

condition條件為true,添加到緩存,根據(jù)id查詢直接從緩存獲取
http://localhost:8080/user/getUserById?id=3

http://localhost:8080/user/updateUser?id=3
http://localhost:8080/user/getUserById?id=3

條件condition=true,執(zhí)行緩存操作
條件unless=false,執(zhí)行緩存操作;跟condition相反
@CacheEvict 注解在方法上,應(yīng)用到刪除數(shù)據(jù)的方法上,如刪除方法,調(diào)用方法之后根據(jù)條件判斷是否從緩存中移除相應(yīng)的數(shù)據(jù)

http://localhost:8080/user/saveUser
http://localhost:8080/user/getUserById?id=3
http://localhost:8080/user/deleteUser?id=3

自定義緩存key自動(dòng)生成器
@Override
@Cacheable(keyGenerator = "myKeyGenerator")
public List<User> findAll(Object... params) {
log.info("查詢所有");
return new ArrayList<>(userMap.values());
}
@Slf4j
public class MyKeyGenerator implements KeyGenerator {
private static final String NO_PARAM = "[]";
private static final String NULL_PARAM = "_";
@Override
public Object generate(Object target, Method method, Object... params) {
StringBuilder key = new StringBuilder();
key.append(target.getClass().getSimpleName()).append(".").append(method.getName()).append(":");
if (params.length == 0) {
return new SimpleKey(key.append(NO_PARAM).toString());
}
return new SimpleKey(key.append(Arrays.toString(params).replace("null", NULL_PARAM)).toString());
}
}
http://localhost:8080/user/findAll

http://localhost:8080/user/findAll?id=123

http://localhost:8080/user/findAll?username=yzm

@Caching
有時(shí)候我們可能組合多個(gè)Cache注解使用;比如用戶新增成功后,我們要添加id–>user;username—>user;email—>user的緩存;
此時(shí)就需要@Caching組合多個(gè)注解標(biāo)簽了。
@Caching(
put = {
@CachePut(value = "users", key = "#user.id"),
@CachePut(value = "users", key = "#user.username"),
@CachePut(value = "users", key = "#user.email")
}
)
public User save(User user) {}
到此這篇關(guān)于Redis之sql緩存的具體使用的文章就介紹到這了,更多相關(guān)Redis sql緩存 內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
在Centos?8.0中安裝Redis服務(wù)器的教程詳解
redis redistemplate序列化對(duì)象配置方式
YII2框架手動(dòng)安裝Redis擴(kuò)展的過(guò)程
使用高斯Redis實(shí)現(xiàn)二級(jí)索引的方法
redis的key出現(xiàn)的\xac\xed\x00\x05t\x00亂碼問(wèn)題及解決

