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
说明
(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());
那么这个生成的key的name是:
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使用过程中的小技巧。
历史相关文章:
微信公众号「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」或者扫描以下二维码即可关注:

204. jetcache:在Spring Boot中怎么玩?的更多相关文章
- 201. Spring Boot JNDI:Spring Boot中怎么玩JNDI
[视频&交流平台] àSpringBoot视频:http://t.cn/R3QepWG à SpringCloud视频:http://t.cn/R3QeRZc à Spring Boot源 ...
- jetcache:在Spring Boot中怎么玩?
- 必须知道的Spring Boot中的一些Controller注解
这篇文章是抄其他人的,原址:https://cloud.tencent.com/developer/article/1082720 本文旨在向你介绍在Spring Boot中controller中最基 ...
- spring boot 中 Cache 的使用
参考:https://blog.csdn.net/qq_38974634/article/details/80650810 一.JSR107 Java Caching 定义5个核心的接口,分别是Cac ...
- spring boot(三):Spring Boot中Redis的使用
spring boot对常用的数据库支持外,对nosql 数据库也进行了封装自动化. redis介绍 Redis是目前业界使用最广泛的内存数据存储.相比memcached,Redis支持更丰富的数据结 ...
- Spring Boot中的事务管理
原文 http://blog.didispace.com/springboottransactional/ 什么是事务? 我们在开发企业应用时,对于业务人员的一个操作实际是对数据读写的多步操作的结合 ...
- Spring Boot中的注解
文章来源:http://www.tuicool.com/articles/bQnMra 在Spring Boot中几乎可以完全弃用xml配置文件,本文的主题是分析常用的注解. Spring最开始是为了 ...
- 在Spring Boot中使用Https
本文介绍如何在Spring Boot中,使用Https提供服务,并将Http请求自动重定向到Https. Https证书 巧妇难为无米之炊,开始的开始,要先取得Https证书.你可以向证书机构申请证书 ...
- Spring Boot中使用Swagger2构建强大的RESTful API文档
由于Spring Boot能够快速开发.便捷部署等特性,相信有很大一部分Spring Boot的用户会用来构建RESTful API.而我们构建RESTful API的目的通常都是由于多终端的原因,这 ...
随机推荐
- vscode 创建.net core mvc
cd 进一个文件夹 1,创建一个sln 工程文件 [ dotnet new sln -n Demo1 ] 2,创建一个mvc项目 [ dotnet new mvc -n Demo1.Web ] 3, ...
- [Leetcode 134]汽车加油站 Gas Station (环形)
[题目] There are N gas stations along a circular route, where the amount of gas at station i is gas[i] ...
- springboot源码之(bean的递归注册)
在prepareContext中,用loader调用load方法,loader是 BeanDefinitionLoader,在BeanDefinitionLoader的构造方法中,会实例化一个Anno ...
- jmeter接口测试基础知识1.0
jmeter:性能测试工具,压测测试计划右键--添加--Threads(Users)--线程组(线程数就是并发数)--右键线程组--添加--Sampler--HTTP请求--最上面的名称可以修改,就是 ...
- day 17 项目开发常用模块
---恢复内容开始--- time模块 import time print(time.time()) # 时间戳: print(time.strftime("%Y-%m-%d %X" ...
- Delphi xe8 FMX StringGrid根据内容自适应列宽。
Delphi xe8 FMX StringGrid根据内容自适应列宽. 网上的资料比较复杂,而且不是根据字体字号等设置列宽.故自己写了个function来用. function GetColMaxDa ...
- 手把手教你用git
一.如何安装git 下载地址: https://git-scm.com/download/win 根据自己的电脑选择是32位的还是64位的.下载完后直接运行,之后一直next就好了.安装成功后,会有这 ...
- A Statistical Model for Scientific Readability-paper
Authors: Luo SiCarnegie Mellon University, Pittsburgh, PA Jamie CallanCarnegie Mellon University, Pi ...
- Linux 文件类型笔记
在UNIX中一切都是文件https://ph7spot.com/musings/in-unix-everything-is-a-file在UNIX中,一切都是字节流 ==== linux系统的文件类型 ...
- python trie
Trie 库 https://github.com/pytries/marisa-trie/blob/master/docs/tutorial.rst http://marisa-trie.readt ...