204. jetcache:在Spring Boot中怎么玩?

 

 

【视频&交流平台】

à SpringBoot视频http://t.cn/R3QepWG

à SpringCloud视频http://t.cn/R3QeRZc

à Spring Boot源码https://gitee.com/happyangellxq520/spring-boot

à Spring Boot交流平台http://412887952-qq-com.iteye.com/blog/2321532

à Spring Boot Shiro视频http://t.cn/R3QDMbh

à Spring Boot 2.0 之Spring Data 和JPAhttp://t.cn/R1pSojf

 说明

(1)Spring Boot 版本:2.0.3.RELEASE

(2)jetcache版本:2.5.2

(3)JDK:1.8

前言

       在前面我们对jetcache有了简单的了解,那么怎么在Spring Boot中进行使用,这才是关键。本节文章会简单的介绍下如何在SpringBoot中使用jetcache。

 

一、基本配置

       这里我们以redis进行讲解,对于在SpringBoot中引入jetcache还是很简单。

1.1 pom文件中添加依赖

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

 

以上添加了jetcache redis的依赖,这样就能使用jetcache操作redis了。

 

1.2 添加配置

       在application.properties添加如下配置:

###jetcache 
jetcache.statIntervalMinutes=15
jetcache.areaInCacheName=false
jetcache.local.default.type=linkedhashmap
jetcache.local.default.keyConvertor=fastjson
jetcache.remote.default.type=redis
jetcache.remote.default.keyConvertor=fastjson
jetcache.remote.default.valueEncoder=java
jetcache.remote.default.valueDecoder=java
jetcache.remote.default.poolConfig.minIdle=5
jetcache.remote.default.poolConfig.maxIdle=20
jetcache.remote.default.poolConfig.maxTotal=50
jetcache.remote.default.host=127.0.0.1
jetcache.remote.default.port=6379

 

       这里对jetcache进行了配置,我们不需要单独在配置redis了。

 

1.3 激活Cached和CreateCache注解

       在启动类中添加两个注解@EnableMethodCache和@EnableCreateCacheAnnotation,这两个注解分别激活Cached和CreateCache注解:

@SpringBootApplication
@EnableMethodCache(basePackages = "com.kfit.jetcache")
@EnableCreateCacheAnnotation
public class Springboot2JetcacheApplication {
    public static void main(String[] args) {
        SpringApplication.run(Springboot2JetcacheApplication.class, args);
    }
}

 

       到这里配置的地方就ok了,接下来就是具体的使用环节了。

 

二、使用方式

2.1 创建缓存实例

通过@CreateCache注解创建一个缓存实例:

@CreateCache
private Cache<Long,ArticleType> cache;

用起来就像map一样:

//ready for entity.
ArticleType articleType = new ArticleType();
articleType.setId(100L);
articleType.setName("小说");
//set to cache.
cache.put(articleType.getId(), articleType);
//get from cache
articleType = cache.get(articleType.getId());
//remove from cache
cache.remove(articleType.getId());

那么这个生成的keyname是:

c.k.j.d.c.TestCacheInstanceController.cache100

那么如何自定义key的名称呢?这个下节在讲jetcache小技巧的时候统一进行讲解。

2.2 创建方法缓存

使用@Cached方法可以为一个方法添加上缓存。JetCache通过Spring AOP生成代理,来支持缓存功能。注解可以加在接口方法上也可以加在类方法上,但需要保证是个Spring bean:

    @Cached
    public ArticleType getById(long id) {
        ArticleType articleType = new ArticleType();
        articleType.setId(id);
        articleType.setName("视频-"+id);
        return articleType;
    }

       这个默认生成的redis的name是:

c.k.j.d.s.ArticleTypeService.getById(J)[102]

       本文只是介绍了最基本的使用姿势,今天连走带跑了20公里,有点迷糊了,明天接着写,jetcache使用过程中的小技巧。

历史相关文章:

203. 阿里jetcache

 

微信公众号「SpringBoot最近更新:

 

210. Spring Boot WebFlux:概念篇
Java8新特性:Stream:实战篇
为了更勇敢,你可以害怕@一禅小和尚
Java8新特性:Stream:基础篇
Java8新特性:方法引用
209. SpringBoot quartz:sqlserver启动只有 DECLARE CURSOR 才允许使用...
风口之上,我是那头猪嘛?
Java8新特性:Lambda表达式: 摸摸里面
Java8新特性:Lambda表达式:过关斩将:使用场景
Java8新特性:Lambda表达式:小试牛刀
下雨天,适合学「Spring Boot」
Java8新特性:接口的默认方法
208. Spring Boot Swagger2:排序 – 漂游记
207. Spring Boot Swagger2:极简方式
我读的书很多,但都没有你好看【一禅录】
206. Spring Boot 2.0 Swagger2:使用
205. Spring Boot 2.0 Swagger2:初识Swagger
当要离开的时候,我却动情了
205. jetcache:你需要知道的小技巧
204. jetcache:在Spring Boot中怎么玩?

 搜索springboot或者扫描以下二维码即可关注:

 

 

原文地址:https://www.cnblogs.com/springboot-wuqian/p/9656492.html