jetCache 基本使用
1.pom引用
<!--jetcache缓存 lettuce-->
<dependency>
<groupId>com.alicp.jetcache</groupId>
<artifactId>jetcache-starter-redis-lettuce</artifactId>
<version>2.5.</version>
</dependency> 这里引用的集成是lettuce的redis客户端
2.
@SpringBootApplication
@EnableMethodCache(basePackages="com.example.demo") //开启 Cache注解
@EnableCreateCacheAnnotation //启用createCache注解
public class DemoApplication { public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
} }
3.配置文件配置 yml格式
#jetcache 集成使用
jetcache:
statIntervalMinutes: 15 // 每隔多久统计信息的时长配置
areaInCacheName: false //是否配置前缀
local:
default:
type: caffeine //本地缓存类型
keyConvertor: fastjson //key的序列化转化的协议
limit: 10000 //本地缓存最大个数
defaultExpireInMillis: 10000 //缓存的时间全局 默认值
remote:
default:
type: redis.lettuce //缓存数据库类型
keyConvertor: fastjson
uri: redis://127.0.0.28:7224/ //这里支持集群配置
#redis://127.0.0.28:7224/
#redis://127.0.0.28:7224/
defaultExpireInMillis: 20000 //全局缓存失效时间
#keyPrefix: ec
经过以上俩步已经可以使用jetCache注解 下面是基本的实战
package com.example.demo; import com.alicp.jetcache.Cache;
import com.alicp.jetcache.anno.CacheType;
import com.alicp.jetcache.anno.CreateCache;
import io.lettuce.core.RedisClient;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired; import java.io.IOException;
import java.util.*;
import java.util.concurrent.TimeUnit; public class redisTest extends BaseTest { @Autowired //lettuce 客户端专用
private RedisClient defaultClient;
/*
@Autowired
private Pool defaultPool;*/ @CreateCache(name = "test", expire = , timeUnit = TimeUnit.SECONDS, cacheType = CacheType.BOTH)
private Cache<String, String> cache;
//可以根据自己的数据类型自定义value的数据类型
@CreateCache(name = "test", expire = , timeUnit = TimeUnit.MINUTES, cacheType = CacheType.BOTH)
private Cache<String, List<String>> listCache; @Test
public void test() throws IOException, InterruptedException {
cache.put("liuxw:1", "liuxw");
System.out.println("liuxw:1 " + cache.get("liuxw:1")); cache.computeIfAbsent("liuxw:2", res -> { return "liuxw2";
}); System.out.println(cache.get("liuxw:1 " + "liuxw2")); cache.computeIfAbsent("liuxw:3", res -> { return "liuxw2";
}, true, , TimeUnit.MINUTES); System.out.println("liuxw:3 " + cache.get("liuxw:1")); Set<String> set = new HashSet<>(Arrays.asList("liuxw:1", "liuxw2"));
Map<String, String> map = cache.getAll(set);
cache.removeAll(set); //推荐使用这个 分布式锁
boolean hasRun = cache.tryLockAndRun("liuxw3", , TimeUnit.SECONDS, () -> {
System.out.println("我获取到锁了");
}); new Thread(() -> {
//推荐使用这个
boolean hasRun1 = cache.tryLockAndRun("liuxw3", , TimeUnit.SECONDS, () -> {
System.out.println("我获取到锁了"+Thread.currentThread().getName()+System.currentTimeMillis());
try {
Thread.sleep();
} catch (InterruptedException e) {
e.printStackTrace();
}
});
if (hasRun1){ }else{
System.out.println("我没获取到锁了"+Thread.currentThread().getName());
}
}, "a1"+new Random().toString()).start();
Thread.sleep(); new Thread(() -> {
//推荐使用这个
boolean hasRun1 = cache.tryLockAndRun("liuxw3", , TimeUnit.SECONDS, () -> {
System.out.println("我获取到锁了"+Thread.currentThread().getName()+System.currentTimeMillis());
try {
Thread.sleep();
} catch (InterruptedException e) {
e.printStackTrace();
}
});
if (hasRun1){ }else{
System.out.println("我没获取到锁了"+Thread.currentThread().getName());
}
}, "a2"+new Random().toString()).start(); Thread.sleep(); new Thread(() -> {
//推荐使用这个 todo 分布式锁实现逻辑学习一下
boolean hasRun1 = cache.tryLockAndRun("liuxw3", , TimeUnit.SECONDS, () -> {
System.out.println("我获取到锁了"+Thread.currentThread().getName()+System.currentTimeMillis());
try {
Thread.sleep();
} catch (InterruptedException e) {
e.printStackTrace();
}
});
if (hasRun1){ }else{
System.out.println("我没获取到锁了"+Thread.currentThread().getName());
}
}, "a3"+new Random().toString()).start(); System.in.read();
} }
jetCache 基本使用的更多相关文章
- 205. jetcache:你需要知道的小技巧
[视频&交流平台] àSpringBoot视频:http://t.cn/R3QepWG à SpringCloud视频:http://t.cn/R3QeRZc à Spring Boot源码: ...
- 204. jetcache:在Spring Boot中怎么玩?
[视频&交流平台] àSpringBoot视频:http://t.cn/R3QepWG à SpringCloud视频:http://t.cn/R3QeRZc à Spring Boot源 ...
- 203. 阿里jetcache
[视频&交流平台] àSpringBoot视频:http://t.cn/R3QepWG à SpringCloud视频:http://t.cn/R3QeRZc à Spring Boot源码: ...
- Jetcache
转存 Jetcache https://github.com/alibaba/jetcache/wiki/GettingStarted_CN
- 阿里开源的缓存框架JetCache
之前一直在用Spring Cache进行接口数据的缓存,主要是Spring Cache在对具体key缓存失效时间的设置不是很方法,还要自己去扩展,无意中发现了阿里的JetCache.大部分的需求都能满 ...
- 阿里巴巴Jetcache springboot使用教程
原文地址:https://www.jianshu.com/p/03b289439de2 springboot中使用说明 jetcache原理参见:https://www.jianshu.com/p/8 ...
- JetCache埋点的骚操作,不服不行啊
阐述背景 缓存是应对高并发绝对的利器,在很多业务场景允许的情况下,都可以使用缓存来提供性能. 既然用了缓存,那对缓存进行监控必不可少.比如缓存加载耗时,新增耗时等. 在 JetCache 中进行埋点操 ...
- JetCache 源码分析
一.简介 JetCache是一个基于Java的缓存系统封装,提供统一的API和注解来简化缓存的使用. JetCache提供了比SpringCache更加强大的注解,可以原生的支持TTL.两级缓存.分布 ...
- 阿里jetcache
随机推荐
- Jenkins下构建UI自动化之初体验
一.缘 起 笔者之前一直在Windows环境下编写UI自动化测试脚本,近日在看<京东系统质量保障技术实战>一书中,萌生出在jenkins下构建UI自动化测试的想法 二.思 路 首先,在Li ...
- Redis启动服务和String常用命令
Redis启动服务和String常用命令 1. 启动Redis服务 E:\redis>redis-server.exe redis.windows.conf _._ _.-``__ ''-._ ...
- Ubuntu的BEEP去哪里了?
一直知道ubuntu的beep不响应了,但是一直都没太关注过它怎么了. 今天关注了一下,发现网上都是在问怎么关掉它的,时间还是在07年左右. 搜索到了一些帖子,有一些是说没有找到恢复的方法,还有一些, ...
- 一文洞悉JVM内存管理机制
前言 本文已经收录到我的Github个人博客,欢迎大佬们光临寒舍: 我的GIthub博客 学习导图: 一.为什么要学习内存管理? Java与C++之间有一堵由内存动态分配和垃圾回收机制所围成的高墙,墙 ...
- MySQL----DQL(查询数据库表中数据)
##DQL:查询表中的记录 1.语法: select 字段列名 from 表名列表 where 条件列表 group by 分组字段 having 分组之后的条件 order by 排序 lim ...
- 动态规划-Minimum Cost to Merge Stones
2019-07-07 15:48:46 问题描述: 问题求解: 最初看到这个问题的时候第一反应就是这个题目和打破气球的题目很类似. 但是我尝试了使用dp将问题直接转为直接合并到一个堆问题复杂度迅速提高 ...
- 【MySQL】面试官:谈谈你对Mysql的MVCC的理解?
MVCC(Mutil-Version Concurrency Control),就是多版本并发控制.MVCC 是一种并发控制的方法,一般在数据库管理系统中,实现对数据库的并发访问. 在Mysql的In ...
- 单线程多任务协程vip电影爬取
单线程多任务协程vip电影爬取 --仅供学习使用勿作商用如有违规后果自负!!! 这几天一直在使用python爬取电影,主要目的也是为了巩固前段时间强化学习的网络爬虫,也算是一个不错的检验吧,面对众 ...
- postgre安装和使用(R&Python)
安装postgre http://helianthus-code.lofter.com/post/1dfe03e0_1c68233aa 这里选C更好 这里口令密码输入就是黑的 我装的时候反复报错,查了 ...
- windows找不到文件gpedit.msc处理方法
新建一个txt,输入 @echo offpushd "%~dp0"dir /b C:\Windows\servicing\Packages\Microsoft-Windows-Gr ...