广告
返回顶部
首页 > 资讯 > 后端开发 > 其他教程 >Redis之sql缓存的具体使用
  • 564
分享到

Redis之sql缓存的具体使用

2024-04-02 19:04:59 564人浏览 八月长安
摘要

目录1.环境搭建2.Redis配置3.功能实现4.缓存注解的使用说明1.环境搭建 <!-- RedisTemplate --> <dependency>

1.环境搭建


 <!-- 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()
                // 转换为格式化的json(控制台打印时,自动格式化规范)
                //.enable(SerializationFeature.INDENT_OUTPUT)
                // Include.ALWAYS  是序列化对像所有属性(默认)
                // Include.NON_NULL 只有不为null的字段才被序列化,属性为NULL 不序列化
                // Include.NON_EMPTY 如果为null或者 空字符串和空集合都不会被序列化
                // Include.NON_DEFAULT 属性为默认值不序列化
                .setSerializationInclusion(JsonInclude.Include.NON_NULL)
                // 如果是空对象的时候,不抛异常
                .configure(SerializationFeature.FAIL_ON_EMPTY_BEANS, false)
                // 反序列化的时候如果多了其他属性,不抛出异常
                .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)
                // 取消时间的转化格式,默认是时间戳,可以取消,同时需要设置要表现的时间格式
                .configure(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS, false)
                .setDateFormat(new SimpleDateFormat(PATTERN))
                // 对LocalDateTime序列化跟反序列化
                .reGISterModule(javaTimeModule)

                .setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY)
                // 此项必须配置,否则会报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;

@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 // 启动缓存
public class RedisConfig {

	@Bean(name = "myKeyGenerator")
    public MyKeyGenerator myKeyGenerator() {
        return new MyKeyGenerator();
    }

    @Resource(name = "myObjectMapper")
    private ObjectMapper objectMapper;

    
    @Bean
    public CacheManager cacheManager(RedisConnectionFactory connectionFactory) {
        RedisCacheWriter redisCacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(connectionFactory);
        RedisCacheConfiguration cacheConfiguration = RedisCacheConfiguration
                .defaultCacheConfig()
                // 默认缓存时间(秒)
                .entryTtl(Duration.ofSeconds(300L))
                // 序列化key、value
                .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer()))
                .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer()))
                // 禁用缓存空值
                .disableCachingNullValues();
        return new RedisCacheManager(redisCacheWriter, cacheConfiguration);
    }

    
    @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来序列化和反序列化redis的key,value采用json序列化
        template.seTKEySerializer(stringRedisSerializer);
        template.setValueSerializer(jacksonSerializer);

        // 设置hash key 和value序列化模式
        template.setHashKeySerializer(stringRedisSerializer);
        template.setHashValueSerializer(jacksonSerializer);

        //支持事务
        template.setEnableTransactionSupport(true);
        template.afterPropertiesSet();

        return template;
    }

    private Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer() {
        //使用Jackson2JsonRedisSerializer来序列化和反序列化redis的value值(默认使用jdk的序列化方式)
        Jackson2JsonRedisSerializer<Object> jacksonSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
        jacksonSerializer.setObjectMapper(objectMapper);
        return jacksonSerializer;
    }
}

3.功能实现

新增、更新、删除、查询数据时,对缓存执行对应相同的操作


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);
}

为了简便,数据不从数据库获取,这里是创建Map存储数据实现


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("保存数据");
        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("更新数据");
        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("删除数据");
        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.缓存注解的使用说明

@CacheConfig:注解在类上,表示该类所有缓存方法使用统一指定的缓存区,也可以作用在方法上

在这里插入图片描述

@CacheAble:注解在方法上,应用到读数据的方法上,如查找方法:调用方法之前根据条件判断是否从缓存获取相应的数据,缓存没有数据,方法执行后添加到缓存

在这里插入图片描述

#id 直接使用参数名
#p0 p0对应参数列表的第一个参数,以此类推
#user.id 参数是对象时,使用对象属性
#root. 可以点出很多方法
#root.methodName
#result 返回值

Http://localhost:8080/user/getUserById?id=1

在这里插入图片描述

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

在这里插入图片描述

http://localhost:8080/user/selectAll

在这里插入图片描述

@Cacheable运行流程:在调用方法之前判断condition,如果为true,则查缓存;没有缓存就调用方法并将数据添加到缓存;condition=false就与缓存无关了

@CachePut:注解在方法上,应用到写数据的方法上,如新增/修改方法,调用方法之后根据条件判断是否添加/更新相应的数据到缓存:

在这里插入图片描述

http://localhost:8080/user/saveUser

在这里插入图片描述

condition条件为true,添加到缓存,根据id查询直接从缓存获取
http://localhost:8080/user/getUserById?id=3

在这里插入图片描述

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

在这里插入图片描述

条件condition=true,执行缓存操作
条件unless=false,执行缓存操作;跟condition相反

@CacheEvict 注解在方法上,应用到删除数据的方法上,如删除方法,调用方法之后根据条件判断是否从缓存中移除相应的数据

在这里插入图片描述

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

在这里插入图片描述

自定义缓存key自动生成器


    @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
有时候我们可能组合多个Cache注解使用;比如用户新增成功后,我们要添加id–>user;username—>user;email—>user的缓存;
此时就需要@Caching组合多个注解标签了。


@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) {}

到此这篇关于Redis之sql缓存的具体使用的文章就介绍到这了,更多相关Redis sql缓存 内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: Redis之sql缓存的具体使用

本文链接: https://www.lsjlt.com/news/159280.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
  • Redis之sql缓存的具体使用
    目录1.环境搭建2.Redis配置3.功能实现4.缓存注解的使用说明1.环境搭建 <!-- RedisTemplate --> <dependency>...
    99+
    2022-11-12
  • Redis中ZSet的具体使用
    目录一、题目二、ZSet 简单使用三、ZSet 结构四、跳跃表五、场景案例一、题目 ZSet能用在哪些场景?跳表查找的过程,时间复杂度 二、ZSet 简单使用 举个例子,fruit-price 是一个有序集合键,这个有序...
    99+
    2022-07-18
    RedisZSet使用
  • Redis可视化工具Redis Desktop Manager的具体使用
    项目中用到了redis,想查询redis中的数据,一直想找一个可视化工具,今天发现了Redis Desktop Manager,试用了一下,很好用。 1.下载 Redis Des...
    99+
    2022-11-12
  • Django项目缓存优化的介绍及具体使用
    这篇文章主要讲解了“Django项目缓存优化的介绍及具体使用”,文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习“Django项目缓存优化的介绍及具体使用”吧!一、为什么要使用缓存大家可以想一下Dj...
    99+
    2023-06-02
  • 使用redis缓存的优势
    今天就跟大家聊聊有关使用redis缓存的优势,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。redis(全称:Remote Dictionary Se...
    99+
    2022-10-18
  • python3之Splash的具体使用
    目录1、Scrapy-Splash的安装2、Splash Lua脚本3、Splash对象的属性与方法4、响应对象5、元素对象6、Splash HTTP API调用7、实例Splash...
    99+
    2022-11-12
  • nginx之queue的具体使用
    目录一、简介二、数据结构三、相关API3.1 初始化一个队列3.2 判断队列是否为空3.3 队头插入节点3.4 队尾插入节点3.5 从队列中移除某个节点3.6 将队列从某个节点拆分成...
    99+
    2022-11-13
  • Java本地缓存工具之LoadingCache的使用详解
    目录前言环境依赖代码演示一下总结前言 在工作总常常需要用到缓存,而redis往往是首选,但是短期的数据缓存一般我们还是会用到本地缓存。本文提供一个我在工作中用到的缓存工具,该工具代码...
    99+
    2022-11-12
  • 图文详解Windows下使用Redis缓存工具的方法
    一、简介 redis是一个key-value存储系统。和Memcached类似,它支持存储的value类型相对更多,包括string(字符串)、list(链表)、set(集合)和zset(有序集合)。 这些...
    99+
    2022-06-04
    缓存 详解 图文
  • SQL 嵌套查询的具体使用
    目录插入数据  select子查询(外语句先执行,内语句后执行)总结嵌套查询又称子查询,有select子查询,where子查询,from子查询,exists子查询。 插入数据&nb...
    99+
    2023-01-28
    SQL嵌套查询
  • SQL嵌套查询的具体使用
    目录插入数据  select子查询(外语句先执行,内语句后执行)总结嵌套查询又称子查询,有select子查询,where子查询,from子查询...
    99+
    2023-01-28
    SQL 嵌套查询
  • Redis 整数集合的具体使用(intset)
    目录一、集合概述二、Redis 整数集合(intset)1、intset 结构定义2、编码方式3、编码升级三、整数集合常用操作1、创建集合2、元素设置3、元素获取 4、元素...
    99+
    2022-11-13
  • SQL Server索引结构的具体使用
    目录名词介绍索引表数据页索引是数据库的基础,只有先搞明白索引的结构,才能搞明白索引运行的逻辑 本文通过 索引表、数据页、执行计划、IO统计、B+Tree 来尽可能的介绍 SQL 语句...
    99+
    2022-11-13
  • pytest之assert断言的具体使用
    背景 本文总结使用pytest编写自动化测试时常用的assert断言。 说明 本文将从以下几点做总结: 为测试结果作断言 为断言不通过的结果添加说明信息 ...
    99+
    2022-11-12
  • shell命令之mv的具体使用
    目录文件间的移动文件到目录的移动目录到目录的移动思考操作系统为 macOS 10.12.4 shell 的 mv 命令为移动(moving)的意思,实际可分为文件间的移动,文件到目录的移动,目录到目录的移动。 mv参数设...
    99+
    2022-06-05
    shell命令mv
  • pandas学习之df.set_index的具体使用
    目录构建实例key:label array-like or list of label/arraysdrop:bool,default Trueappend:bool default...
    99+
    2022-11-11
  • pandas学习之df.fillna的具体使用
    目录构建实例:value:scalar,series,dict,dataframemethod:{backfill,bfill,pad,ffill,none},default non...
    99+
    2022-11-11
  • C++17之std::any的具体使用
    目录1. 使用std::any 2. std::any类型和操作2.1 std::any的类型2.2 std::any操作    一般来说,c++是一种...
    99+
    2022-11-13
  • C++17之std::visit的具体使用
    目录1. 使用对象函数方式访问 2. 使用泛型Lambdas访问3. 使用重载的Lambdas来访问    它们必须明确地为每种可能的类型提供函数调用操...
    99+
    2022-11-13
  • GoLang之gobuild命令的具体使用
    目录1.go build命令2.手动案例2.1新建文件2.2配置2.3go mod init2.4go get -u github.com/jinzhu/configor2.5go ...
    99+
    2022-11-11
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作