SpringBoot之缓存Redis和springCache

spring-redis

导包:spring-boot-start-redis
配置:spring:
datasource:
# 驱动配置信息
url: jdbc:mysql://localhost:3306/spring_boot?useUnicode=true&characterEncoding=utf8
username: root
password: root
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver

    # 连接池的配置信息
    filters: stat
    maxActive: 20
    initialSize: 1
    maxWait: 60000
    minIdle: 1
    timeBetweenEvictionRunsMillis: 60000
    minEvictableIdleTimeMillis: 300000
    validationQuery: select 'x'
    testWhileIdle: true
    testOnBorrow: false
    testOnReturn: false
    poolPreparedStatements: true
    maxOpenPreparedStatements: 20

redis:
host: 127.0.0.1
port: 6379
password: pass1234
pool:
max-active: 100
max-idle: 10
max-wait: 100000
timeout: 0
操作:使用redistemplate方式。
使用方法如下:
stringRedisTemplate.opsForValue().set(“test”, “100”,60*10,TimeUnit.SECONDS);//向redis里存入数据和设置缓存时间
stringRedisTemplate.opsForValue().get(“test”)//根据key获取缓存中的val
stringRedisTemplate.boundValueOps(“test”).increment(-1);//val做-1操作
stringRedisTemplate.boundValueOps(“test”).increment(1);//val +1
stringRedisTemplate.getExpire(“test”)//根据key获取过期时间
stringRedisTemplate.getExpire(“test”,TimeUnit.SECONDS)//根据key获取过期时间并换算成指定单位
stringRedisTemplate.delete(“test”);//根据key删除缓存
stringRedisTemplate.hasKey(“546545”);//检查key是否存在,返回boolean值
stringRedisTemplate.expire(“red_123”,1000 , TimeUnit.MILLISECONDS);//设置过期时间
stringRedisTemplate.opsForSet().add(“red_123”, “1”,“2”,“3”);//向指定key中存放set集合
stringRedisTemplate.opsForSet().isMember(“red_123”, “1”)//根据key查看集合中是否存在指定数据
stringRedisTemplate.opsForSet().members(“red_123”);//根据key获取set集合

spring Cache:
spring自带的东西,不需要安装软件,少量配置和注解即可完成
注释说明:
1@Cacheable(value=“aaa”,key=“bbb”,condition条件值):调用这个方法是,从一个叫aaa的缓存中查询键为bbb的值,
#bbb:表示从传入参数中取出与变量名相同的值
2@CacheEvict(value=,key=):清空缓存
3 多个参数组合key,key="#userName.concat(#password)"
4@CachePut:这个注释既保证可以从缓存中查到信息,还可以保证的方法被执行。
具体参见:https://www.ibm.com/developerworks/cn/opensource/os-cn-spring-cache/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值