阿里巴巴Jetcache springboot使用教程

原文地址:https://www.jianshu.com/p/03b289439de2

springboot中使用说明

jetcache原理参见:https://www.jianshu.com/p/8cff0062a899
jetcache 源码参见:https://github.com/alibaba/jetcache.git

1 引入pom依赖

<dependency>
 <groupId>com.alicp.jetcache</groupId>
 <artifactId>jetcache-starter-redis</artifactId>
 <version>2.4.4</version>
</dependency>

2 在启动类上增加注解

@SpringBootApplication(scanBasePackages = {"com.example.firstjetcacheprj.business","com.alicp.jetcache.autoconfigure"}) 
@EnableMethodCache(basePackages = "com.example.firstjetcacheprj.business") 
@EnableCreateCacheAnnotation 
public class FirstjetcacheprjApplication { 
         public static void main(String[] args) {
               SpringApplication.run(FirstjetcacheprjApplication.class, args);
 }
}

其中需要注意的是:

  • 在@SpringBootApplication注解对应的scanBasePackages中增加jetcache自动配置对应的包。
  • 增加注解EnableMethodCache,并制定开启缓存对应的包路径。
  • 增加注解EnableCreateCacheAnnotation,这个注解是开启对应的CreateCache注解。

3 在application.yml中增加对应的缓存全局配置

jetcache:  
    statIntervalMinutes: 15  
    areaInCacheName: false  
    local:    
          default:      
                type: linkedhashmap      
                keyConvertor: fastjson    
         otherCacheName:      
               type: xxx      
               keyConverter: yyy  
    remote:    
         default:      
               type: redis      
               keyConvertor: fastjson      
               valueEncoder: java      
               valueDecoder: java      
               poolConfig:        
                      minIdle: 5        
                      maxIdle: 20        
                      maxTotal: 50      
               host: 127.0.0.1      
               port: 6379

配置中字段讲解可以参考https://github.com/alibaba/jetcache/wiki/Config_CN

4 在对应接口或者类方法上增加缓存注解

具体注解详细说明请参考:https://github.com/alibaba/jetcache/wiki/MethodCache_CN

4.1增加缓存

接口Service对应的代码如下:

public interface Service {   
    @Cached(cacheType = CacheType.LOCAL)   
    int printSay(String message);
}

只需要在对应接口的方法上增加注解@Cache,即可以在对应这个方法增加缓存。

4.2缓存刷新

对应的代码如下:

public interface Service {   
      @Cached(cacheType = CacheType.LOCAL)   
      @CacheRefresh(refresh = 60)   
      int printSay(String message);
}

@CacheRefresh上面的配置是1分钟刷新一次

4.3 缓存失效

对应的代码如下:

@CacheInvalidate(name = "c1", key = "args[0]")
void delete(String id);

表示从缓存名称为c1,将对应key为id值的记录从缓存c1中删除。

4.4 缓存更新

对应的代码如下:

@CacheUpdate(name = "c1", key = "#id", value = "args[1]")
void update(String id, int value);

刷新缓存对应的缓存名称为c1,缓存中对应的key为id的值,更新key的值为value的值。

4.5 缓存开启

对应的代码如下:

@Cached(enabled = false)
public int countWithDisabledCache(){   
    return count++;
}
@EnableCache
public int enableCacheWithAnnoOnClass(){   
    return countWithDisabledCache();
}

从上面代码中可以看出方法countWithDisabledCache对应的方法定义了缓存功能,但是这个功能被关闭了,而方法enableCacheWithAnnoOnClass方法上开启了缓存的功能,则方法countWithDisabledCache虽然本身的缓存被关闭了,但是调用方法开启了,则方法countWithDisabledCache对应的缓存功能也被开启了。



作者:瑜骐
链接:https://www.jianshu.com/p/03b289439de2
来源:简书
简书著作权归作者所有,任何形式的转载都请联系作者获得授权并注明出处。
原文地址:https://www.cnblogs.com/eyesfree/p/10704042.html