当前位置: 首页 > article >正文

springboot配置redis、Spring cache

1.Jedis库

依赖库

<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>5.0.2</version>
</dependency>

使用案例:

 @Test
    public void jedis(){
        Jedis jedis = new Jedis("127.0.0.1", 6379);
        jedis.set("name","yi");
        jedis.close();
    }

2.springboot官方编写的整合库

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
            <version>3.1.5</version>
        </dependency>

使用案例:

 	@Autowired
    private RedisTemplate redisTemplate;//通过自动注入
    @Test
    public void t(){
		//使用opsFor系列方法获取xxxOperations对象
         ValueOperations valueOperations = redisTemplate.opsForValue();
        valueOperations.set("name3","yi");

        HashOperations hashOperations = redisTemplate.opsForHash();
        hashOperations.put("student","name","yi");
        hashOperations.put("student","age","18");
        String age = (String) hashOperations.get("student", "age");

    }

RedisTemplate默认使用 JdkSerializationRedisSerializer 进行序列化。
序列化的key结果为:
在这里插入图片描述

为什么是这样一串奇怪的 16 进制? ObjectOutputStream#writeString(String str, boolean unshared) 实际就是标志位 + 字符串长度 + 字符串内容

如果需要redis中设定的key值与我们在程序中设定的值相同,则需要改变序列化的方式,即自定义RedisTemplate。
创建一个配置类来定义RedisTemplate Bean,设置序列化方式 StringRedisSerializer。

@Configuration
public class RedisConfiguration extends CachingConfigurerSupport {
    @Bean
    RedisTemplate<Object,Object> redisTemplate(RedisConnectionFactory redisConnectionFactory){
        RedisTemplate<Object,Object> redisTemplate = new RedisTemplate();
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(new StringRedisSerializer());
        redisTemplate.setHashKeySerializer(new StringRedisSerializer());
        redisTemplate.setConnectionFactory(redisConnectionFactory);
        return redisTemplate;
    }
}

3.Spring cache

通过注解来新增/删除缓存。

配置信息:

引入redis架包:

  <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
            <version>3.1.5</version>
        </dependency>

指定缓存的类型为redis,并配置redis IP端口号

#application.yml:
spring:
  redis:
    host: 127.0.0.1
    port: 6379
    database: 0
   
  cache:
    type: redis

操作步骤

  1. 在启动类上加入@EnableCaching,开启缓存功能
  2. 注入CacheManager,选择缓存实现方法
    @Autowired
    private CacheManager cacheManager;
  3. 在需要cache操作的方法上加上相关注解

注解

@CachePut

使用 @CachePut 注解就能够将方法的返回值放到缓存中,使用该注解时可以指定以下几个参数:

参数解释example
value缓存的名称,每个缓存名称下可以有多个 key,必须指定至少一个@CachePut(value=“my cache”)
key缓存的 key,可以为空,如果指定要按照 SpEL 表达式编写,如果不指定,默认按照方法的所有参数进行组合@CachePut(value=“testcache”, key=“#user.id”)
condition缓存的条件,可以为空,使用 SpEL 编写,返回 true 或者 false,只有为 true 才进行缓存@CachePut(value=“testcache”, condition=“#userName.length()>2”)
@CacheEvict

使用 @CacheEvict 注解就能够将一条或多条数据从缓存中删除,使用该注解时可以指定以下几个参数:

参数解释example
value缓存的名称,每个缓存名称下可以有多个 key,必须指定至少一个@CacheEvict(value=“my cache”)
key缓存的 key,可以为空,如果指定要按照 SpEL 表达式编写,如果不指定,默认按照方法的所有参数进行组合@CacheEvict(value=“testcache”, key=“#id”)
condition缓存的条件,可以为空,使用 SpEL 编写,返回 true 或者 false,只有为 true 才进行清空缓存@CacheEvict(value=“testcache”, condition=“#userName.length()>2”)
allEntries是否清空所有缓存内容,默认为 false,如果指定为 true,则方法调用后将立即清空所有缓存@CachEvict(value=“testcache”, allEntries=true)
beforeInvocation是否在方法执行前就清空,默认为 false,如果指定为 true,则在方法还没有执行的时候就清空缓存,缺省情况下,如果方法执行抛出异常,则不会清空缓存@CachEvict(value=“testcache”,beforeInvocation=true)
@Cacheable

使用 @Cacheable 注解,在方法执行前 Spring 会先查看缓存中是否有数据,如果有数据,则直接返回缓存数据;若没有数据,调用方法并将方法返回值放到缓存中。使用该注解时可以指定以下几个参数:

参数解释example
value缓存的名称,每个缓存名称下可以有多个 key,必须指定至少一个例如: @Cacheable(value=“mycache”) @Cacheable(value={“cache1”, “cache2”}
key缓存的 key,可以为空,如果指定要按照 SpEL 表达式编写,如果不指定,默认按照方法的所有参数进行组合@Cacheable(value=“testcache”, key=“#userName”)
condition缓存的条件,可以为空,使用 SpEL 编写,返回 true 或者 false,只有为 true 才进行缓存@Cacheable(value=“testcache”, condition=“#userName.length()>2”)
unless与 condition 相反,满足条件的时候则不缓存数据@Cacheable(value=“testcache”, unless=“#result == null”)

http://www.kler.cn/a/104865.html

相关文章:

  • 【Python第三方包】使用Python的Translate包进行文本翻译
  • CentOS 7
  • java 编译 引用 jar 包进行编译和执行编译后的class文件
  • 外网nat+nat server,内网做路由过滤,以及ppp CHAR认证 企业网搭建
  • 如何选择向量数据库|Weaviate Cloud v.s. Zilliz Cloud
  • webservice接口调用
  • Spring注解RequestBody与RequestParam详解
  • 实践小记—静态成员的使用注意(或许由此产生的不知名Bug)
  • 人工智能基础_机器学习001_线性回归_多元线性回归_最优解_基本概念_有监督机器学习_jupyter notebook---人工智能工作笔记0040
  • HAL库 CubeMX STM32采用SDIO实现对SD卡和NAND Flash的读写
  • RocksDB基本架构与原理详解
  • 跨境出口亚马逊美国和加拿大市场水基灭火器UL测试报告审核解析
  • ubuntu安装golang
  • 神光秘诀React
  • 2021-arXiv-The Power of Scale for Parameter-Efficient Prompt Tuning
  • 云尘靶场-铁三域控
  • Clickhouse实时数仓建设
  • 迅镭激光万瓦切割设备中标全球轨交装备龙头中国中车
  • 【Objective-C】浅析Block及其捕获机制
  • vue3 使用 elementUi: ./lib/theme-chalk/index.css is not exported from package