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. (转)SpringBoot :(has no explicit mapping for /error)

    转载自:https://www.cnblogs.com/panchanggui/p/9945261.html 异常:This application has no explicit mapping f ...

  2. SpringCloud微服务:Sentinel哨兵组件,管理服务限流和降级

    源码地址:GitHub·点这里||GitEE·点这里 一.基本简介 1.概念描述 Sentinel 以流量为切入点,从流量控制.熔断降级.系统负载保护等多个维度保护服务的稳定性.包括核心的独立类库,监 ...

  3. Mac brew命令的使用

    mac 终端程序管理工具 能让你更快速的安装你想要的工具.而不用考虑大量的依赖. 安装brew复制下面的命令,终端执行  官网Homebrew /usr/bin/ruby -e "$(cur ...

  4. 题解 P1278 【单词游戏】

    前言 首先,看到这道题目,我首先想到的是暴搜,通过\(vector\)来搞,代码也是很短的. 这里用了一个类似于分治的思想 把一个大问题转化为小问题 先枚举第一个单词,之后把能拼接在它后面的单词都一个 ...

  5. PAT-B 1003. 我要通过!(20) Java版

    "答案正确"是自动判题系统给出的最令人欢喜的回复.本题属于PAT的"答案正确"大派送 -- 只要读入的字符串满足下列条件,系统就输出"答案正确&quo ...

  6. 渗透测试之Web安全

    写在前面: 渗透测试包含但不限于Web安全 渗透测试并不相当于Web渗透 Web安全学习是入门渗透测试最容易的途径,门槛最低 Web安全入门: 基础入门 整体框架 SQL注入 XSS攻击 业务逻辑漏洞 ...

  7. 模板字符串原理,原生js实现字符串模板

    在使用模板字符串的时候使用的是 '{{}}'形式进行书写,本文则向各位解密这么写的原因 初体验正则 首先要先明白正则表达式中exec的使用 例如: let str = 'axu1997@qq.com' ...

  8. iOS 真机查看沙盒目录

    iExplorer 的方法试的时候设备都无法检测到,建议放弃 启用iTunes文件共享,才能够看沙盒内的文件,只需要在plist文件中添加如下信息: <key>UIFileSharingE ...

  9. go server框架学习之路 - 写一个自己的go框架

    go server框架学习之路 - 写一个自己的go框架 用简单的代码实现一个go框架 代码地址: https://github.com/cw731/gcw 1 创建一个简单的框架 代码 packag ...

  10. Java并发基础09. 多个线程间共享数据问题

    先看一个多线程间共享数据的问题: 设计四个线程,其中两个线程每次对data增加1,另外两个线程每次对data减少1. 从问题来看,很明显涉及到了线程间通数据的共享,四个线程共享一个 data,共同操作 ...