zoukankan      html  css  js  c++  java
  • springboot中使用cache和redis

    知识点:springboot中使用cache和redis

     (1)springboot中,整合了cache,我们只需要,在入口类上加 @EnableCaching 即可开启缓存

     例如:在service层使用@Cacheable和CacheEvict

     //添加缓存
    @Cacheable(cacheNames = "TestCACHE",key = "#root.methodName +'_'+ #id")
    public Map<String, Object> testSetCache(Integer id){
    Map<String, Object> user = userMapper.findUserById(id);
    return user;
    }

    //清除缓存
    @CacheEvict(cacheNames = "TestCACHE", allEntries = true)
    public Boolean testEvictCache(){
    return true;
    }

    (2)引入redis依赖,将Cache中的数据放到redis数据库中,然后从redis中取数据

    a.引入依赖
    <!--加入redis依赖-->
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
    </dependency>

     b.application.yml中设置redis连接等配置

    redis:
    host: 192.10.21.237
    port: 6379
    database: 5
    password:
    timeout: 1800000
    jedis:
    pool:
    max-idle: 10
    min-idle: 0
    max-active: 10
    max-wait: 1000

    存入redis的数据如下








    问题:key乱码 可参考另一篇博客解决办法 https://www.cnblogs.com/shuaifing/p/11213253.html
    之后会总结1.cache其他用法 2.springboot中的原理

    参考:https://blog.csdn.net/weixin_36279318/article/details/82820880
  • 相关阅读:
    Elasticsearch布尔查询——bool
    Elasticsearch查询
    数据从文件导入Elasticsearch
    Elasticsearch批处理操作——bulk API
    Mysql笔记——DCL
    Mysql笔记——DML
    Mysql笔记——DDL
    Java笔记——面向接口编程(DAO模式)
    Java笔记——equals和==的区别
    JavaWeb笔记——注册登录系统项目思路
  • 原文地址:https://www.cnblogs.com/bruce1992/p/13899073.html
Copyright © 2011-2022 走看看