【视频&交流平台】

à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 和JPA:http://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());

那么这个生成的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使用过程中的小技巧。

历史相关文章:

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」或者扫描以下二维码即可关注:

204. jetcache:在Spring Boot中怎么玩?的更多相关文章

  1. 201. Spring Boot JNDI:Spring Boot中怎么玩JNDI

      [视频&交流平台] àSpringBoot视频:http://t.cn/R3QepWG à SpringCloud视频:http://t.cn/R3QeRZc à Spring Boot源 ...

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

  3. 必须知道的Spring Boot中的一些Controller注解

    这篇文章是抄其他人的,原址:https://cloud.tencent.com/developer/article/1082720 本文旨在向你介绍在Spring Boot中controller中最基 ...

  4. spring boot 中 Cache 的使用

    参考:https://blog.csdn.net/qq_38974634/article/details/80650810 一.JSR107 Java Caching 定义5个核心的接口,分别是Cac ...

  5. spring boot(三):Spring Boot中Redis的使用

    spring boot对常用的数据库支持外,对nosql 数据库也进行了封装自动化. redis介绍 Redis是目前业界使用最广泛的内存数据存储.相比memcached,Redis支持更丰富的数据结 ...

  6. Spring Boot中的事务管理

    原文  http://blog.didispace.com/springboottransactional/ 什么是事务? 我们在开发企业应用时,对于业务人员的一个操作实际是对数据读写的多步操作的结合 ...

  7. Spring Boot中的注解

    文章来源:http://www.tuicool.com/articles/bQnMra 在Spring Boot中几乎可以完全弃用xml配置文件,本文的主题是分析常用的注解. Spring最开始是为了 ...

  8. 在Spring Boot中使用Https

    本文介绍如何在Spring Boot中,使用Https提供服务,并将Http请求自动重定向到Https. Https证书 巧妇难为无米之炊,开始的开始,要先取得Https证书.你可以向证书机构申请证书 ...

  9. Spring Boot中使用Swagger2构建强大的RESTful API文档

    由于Spring Boot能够快速开发.便捷部署等特性,相信有很大一部分Spring Boot的用户会用来构建RESTful API.而我们构建RESTful API的目的通常都是由于多终端的原因,这 ...

随机推荐

  1. 关于django的一些基础知识

    1.中间件 中间件的作用 : 在全局范围内控制django的输入和输出的钩子函数 中间件中有5种方法: process_request : 请求进来时,进行验证 process_views : 路由匹 ...

  2. centos安装ruby

    下面开始安装Ruby 一.下载Ruby源码包 wget ftp://ftp.ruby-lang.org/pub/ruby/2.1/ruby-2.1.7.tar.gz 二.解压安装Ruby tar zx ...

  3. XenServer7.6命令行导出导入虚拟机(迁移)

    一:命令行方法导出虚拟机(先关闭虚拟机) 1.1:打印虚拟机列表 xe vm-list uuid ( RO) : 43dfac04-515e-7769-b2d2-444d4b7cb198 name-l ...

  4. design

    type Config struct { Item lock } func (*Config) getItem(){ } func (*Config) SetItem(){ } channel : - ...

  5. 解决Error parsing SQL Mapper Configuration. Cause: java.io.IOException: Could not find resource com/cqupt/paging/dao/User.xml

    搭建了一个ssm项目,启动报错Error parsing SQL Mapper Configuration. Cause: java.io.IOException: Could not find re ...

  6. python day18--面向对象,继承

    # class Animal: # breath = '呼吸' # # def __init__(self, name, sex, age): # self.name = name # self.se ...

  7. 周强201771010141《面向对象程序设计(java)》第六周学习总结

    枚举是一种特殊的数据类型,之所以特殊是因为它既是一种类(class)类型却又比类型多了些特殊的约束,但是这些约束的存在也造就了枚举类型的简洁,安全性以及便捷性.创建枚举类型要使用enum关键字,隐含了 ...

  8. Windows7下安装python3.6.3

    官网下载即可!看清自己的电脑是32还是64! 分享一个网速慢同学可下载网址:https://pan.baidu.com/s/1kU5OCOB#list/path=%2Fpub%2Fpython 1.p ...

  9. linux学习笔记(二:权限)

    Linux下有两种用户: 超级用户(root).普通用户. 超级用户:可以再linux系统下做任何事情,不受限制 普通用户:在linux下做有限的事情,例如:rm -rf 只会删除自己的东西. 超级用 ...

  10. .Net Core 项目在Windows服务中托管【转载】

    本文以创建的WebAPI项目为例子进行讲解(本人使用VS Code创建的项目) 1.使用VS Code创建WebAPI项目(项目名称自定义) 2.在创建的项目csproj项目文件中,确认是否存在运行时 ...