博客详情

springcloud中集成springcache(redis) (原创)

作者: 朝如青丝暮成雪
发布时间:2023-02-13 16:14:27  文章分类:java编程   阅读(773)  评论(0)




1、pom.xml中引入依赖
<!-- redis 依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
            <version>2.7.2</version>
        </dependency>
        <!-- spring cache 依赖 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-cache</artifactId>
            <version>2.7.2</version>
        </dependency>


2、项目的application.yml配置

spring:
  redis:
    host: 127.0.0.1
    port: 6379
    #password: 
    ssl: false
    database: 0
    timeout: 6000
    lettuce:
      pool:
        max-active: 8 #缺省值8
        max-idle: 8 #缺省值8
        min-idle: 0 #缺省值0
        max-wait: 3000 #缺省值-1

  cache:
    type: redis
    redis:
      time-to-live: 1800000
      key-prefix: spring_cache_demoserver
      use-key-prefix: true
      #允许缓存空值,避免缓存穿透
      cache-null-values: true


3、springboot启动类上标记@EnableCaching注解 

@EnableCaching //开启缓存
@SpringBootApplication
public class DemoServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoServerApplication.class, args);
    }


}


4、springcache的案例应用
@Service
@CacheConfig(cacheNames="demoPerson")
public class DemoPersonService {


   /**
     * 新增对象
     * @param req
     */
    @CachePut(key="#result.id")
    public DemoPerson add(DemoPersonAddReq req) {

        DemoPerson person = new DemoPerson();
        person.setDelFlag("N");
        person.setCreateTime(new Date());
        BeanUtil.copyProperties(req,person);
        demoPersonMapper.insert(person);
        return person;
    }

   /**
     * 根据id更新对象
     * @param req
     */
    @CacheEvict(key="#req.id") //清除缓存
    public void updateById(DemoPersonUpdateReq req) {
        DemoPerson person = new DemoPerson();
        BeanUtil.copyProperties(req,person);
        demoPersonMapper.updateById(person);
    }

 /**
     * 根据id查询
     * @param id
     * @return
     */
    @Cacheable(key="#id")  //放入缓存
    public DemoPerson findById(Long id) {
        System.out.println("根据id查询:"+id);
        return demoPersonMapper.selectById(id);
    }


 /**
     * 根据id删除
     * @param id
     */
    @CacheEvict(key="#id")  //清除缓存
    public void deleteById(Long id) {
        //demoPersonMapper.deleteById(id);
        DemoPerson person=new DemoPerson();
        person.setId(id);
        person.setDelFlag("Y");
        demoPersonMapper.updateById(person);// 逻辑删除
    }

    /**
     * 清除所有
     */
    @CacheEvict(allEntries=true)
    public void deleteAll() {
        System.out.println("清除所有");
        demoPersonMapper.updateDelAll();
    }

    /**
     * 按照姓名(模糊)+性别查询列表
     * @param name
     * @param sex
     * @return
     */
    @Cacheable(key = "'name='+#name+'&sex='+#sex")
    public List<DemoPerson> findByNameSexLike(String name, Integer sex) {
       List<DemoPerson> list =  demoPersonMapper.findByNameSexLike(name,sex);
       return list ;
    }

    /**
     *  查询列表(自定义条件)
     * @param req
     * @return
     */
    @Cacheable(key = "#root.methodName+':'+#req.toKeyString()")
    public List<DemoPerson> findList(DemoPersonListReq req) {
        List<DemoPerson> list =  demoPersonMapper.findList(req);
        return list ;
    }

    /**
     * 编程式 操作缓存
     */
    public void clearAll() {
        Collection<String> cacheNames= cacheManager.getCacheNames();
        System.out.println("cacheNames:"+cacheNames);
        Cache cache = cacheManager.getCache("demoPerson");
        cache.clear();// 按照缓存名称清除
        //cache.evict(key);

    }


}




      

关键字:  springcache
评论信息
暂无评论
发表评论

亲,您还没有登陆,暂不能评论哦! 去 登陆 | 注册

博主信息
   
数据加载中,请稍候...
文章分类
   
数据加载中,请稍候...
阅读排行
 
数据加载中,请稍候...
评论排行
 
数据加载中,请稍候...

Copyright © 叮叮声的奶酪 版权所有
备案号:鄂ICP备17018671号-1

鄂公网安备 42011102000739号