springboot 中使用缓存

1.配置

在 pom.xml 中加入Ehcache 依赖

        
        
            net.sf.ehcache
            ehcache
        
		
        
        
        	com.google.guava
        	guava
        	25.1-jre
        

开启缓存:在启动类上加入 注解:@EnableCaching   //开启缓存支持

2.注入Bean


    @Bean
    public CacheManager getCache(){
        return new EhCacheCacheManager();  //使用Ehcache缓存
//        return new GuavaCacheManager();  //使用guava缓存
    }
    

如果使用Ehcache做缓存,需要在src/main/resources 目录中加入Ehcache配置文件 ehcache.xml

ehcache.xml




    
    
    
           


3.编码方式使用

    
    @Autowired
    private CacheManager cacheManager;
    
    Cache ch   ;

    void init(){
       
         ch  = cacheManager.getCache("books");
    }
    
    /**
     * 向内存中加入数据
     */
    @RequestMapping("cset")
    public void cacheSet(Integer id){
        init();

        BookDO book = new BookDO();
        book.setId(id);
        book.setName("set名字 "); 
        ch.put(id, book);
    }

    /**
     * 如果内存中没有该数据,就加入
     */
    @RequestMapping("csetif")
    public void cacheSetIf(Integer id){
        init();
        BookDO book = new BookDO();
        book.setId(id);
        book.setName("set名字 IFFFF "); 
        ch.put(id, book);
        
        //如果不存在,保存?? **测试结果:是否存在,都保存,直接覆盖**
        ValueWrapper vw = ch.putIfAbsent(id, book); 
        System.out.println(vw);
        System.out.println(vw.get());
        
    }
    
    /**
     * 读取数据
     */
    @RequestMapping("cget")
    public void cacheGet(Integer id){
        long a = System.currentTimeMillis();
        init();
        BookDO book = ch.get(id,BookDO.class);
        
        System.out.println("缓存中:"+book);

        System.out.println(System.currentTimeMillis()-a);
    }

    /**
     * 删除一条数据
     */
    @RequestMapping("cdel")
    public void cacheDel(Integer id){
        init();
        ch.evict(id);
    }

    /**
     * 清空数据
     */
    @RequestMapping("cdelall")
    public void cacheDelAll(){
        init();
        ch.clear();
    }
    

4.注解式使用

  
   
    /**
     * 保存数据时,同时更新缓存, 
     * 系统会将方法返回的数据放入缓存,
     */
    @CachePut(value = "book",key = "#book.id" )
    @Override
    public BookDO save(BookDO book){
        bookDAO.insertBook(book);
        System.out.println("bookId: "+book.getId()+"  保存数据  做缓存");
        return book;
    }
    
    /**
     * 修改数据时,同时更新缓存
     */
    @CachePut(value = "book",key = "#book.id"  )
    @Override
    public void update(BookDO book){
        bookDAO.updateBook(book);
        System.out.println("修改数据   缓存");
         
    }
    
    /**
     * 查询数据,先从缓存中获取,没有再去数据库查,查到了放到缓存中
     */
    @Cacheable(value = "book" ,key = "#id")
    @Override
    public BookDO find(int id){
       BookDO b = bookDAO.getBookById(id);
        System.out.println("数据做了缓存");
        return b;
    }
    
    /**
     * 删除缓存
     */
    @CacheEvict(value = "book",key = "#id")
    @Override
    public void remove(int id){
        System.out.println("删除缓存");
    }
    /**
     * 清空缓存
     */
    @CacheEvict(value = "book",allEntries=true)
    @Override
    public void removeAll(){
        System.out.println("清空缓存");
    }
    
    

 

 

你可能感兴趣的:(SpringBoot)