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 基本使用的更多相关文章

  1. 205. jetcache:你需要知道的小技巧

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

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

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

  3. 203. 阿里jetcache

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

  4. Jetcache

    转存 Jetcache https://github.com/alibaba/jetcache/wiki/GettingStarted_CN

  5. 阿里开源的缓存框架JetCache

    之前一直在用Spring Cache进行接口数据的缓存,主要是Spring Cache在对具体key缓存失效时间的设置不是很方法,还要自己去扩展,无意中发现了阿里的JetCache.大部分的需求都能满 ...

  6. 阿里巴巴Jetcache springboot使用教程

    原文地址:https://www.jianshu.com/p/03b289439de2 springboot中使用说明 jetcache原理参见:https://www.jianshu.com/p/8 ...

  7. JetCache埋点的骚操作,不服不行啊

    阐述背景 缓存是应对高并发绝对的利器,在很多业务场景允许的情况下,都可以使用缓存来提供性能. 既然用了缓存,那对缓存进行监控必不可少.比如缓存加载耗时,新增耗时等. 在 JetCache 中进行埋点操 ...

  8. JetCache 源码分析

    一.简介 JetCache是一个基于Java的缓存系统封装,提供统一的API和注解来简化缓存的使用. JetCache提供了比SpringCache更加强大的注解,可以原生的支持TTL.两级缓存.分布 ...

  9. 阿里jetcache

随机推荐

  1. 使用numpy和PIL实现图像的手绘效果

    输入 输出 代码如下 图像的手绘效果的实现 from PIL import Image import numpy as np a = np.array(Image.open("index.j ...

  2. Docker学习-私有仓库docker-registry的使用

    1.从docker官方仓库下载registry 2.将registry放进容器内 3.在官方下载镜像上传本地仓库 4.私有仓库docker-registry使用的常见问题 5.配置阿里云镜像加速器 假 ...

  3. [UWP]抄抄《CSS 故障艺术》的动画

    1. 前言 什么是故障艺术(Glitch Art 风)?我们熟知的抖音的 LOGO 正是故障艺术其中一种表现形式.它有一种魔幻的感觉,看起来具有闪烁.震动的效果,很吸引人眼球.故障艺术它模拟了画面信号 ...

  4. flex弹性盒子实现微博页面

    结果图: 源代码: html部分: <!DOCTYPE html><html lang="en"><head> <meta charset ...

  5. elasticsearch基础及在Python中的简单使用

    目录 一. 安装java环境与elasticsearch.kibana 二. elasticsearch.kibana的部分文件说明 三. Kibana的Dev tools中ES的简单命令 四. ES ...

  6. nginx 内存池

    参考 https://www.cnblogs.com/xiekeli/archive/2012/10/17/2727432.html?tdsourcetag=s_pctim_aiomsg 源码版本 n ...

  7. Alpine Linux 常用命令

    一:Alpine Linux开启SSH远程登陆 1.简介: 最重要的一个服务了,远程登陆需要用它,文件传输需要用它,必备功能.不管你是在实体机上跑,虚拟机上跑,docker里面跑,这个都是必须的. 2 ...

  8. linux pdftk

    部分内容来源网络,如有版权问题,请联系删除: http://xuqin.blog.51cto.com/5183168/1117780,   http://blog.sina.com.cn/s/blog ...

  9. effective-java学习笔记---静态工厂方法替代构造方法

    使用静态方法的优点: 1.它们是有名字的,生成的客户端代码更易阅读. 如:返回素数的静态方法 BigInteger.probablePrime 2.与构造方法不同,它们不需要每次调用时都创建一个对象. ...

  10. OpenCV-Python 交互式前景提取使用GrabCut算法 | 三十五

    目标 在本章中, 我们将看到GrabCut算法来提取图像中的前景 我们将为此创建一个交互式应用程序. 理论 GrabCut算法由英国微软研究院的Carsten Rother,Vladimir Kolm ...