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();
} }

最新文章

  1. &ldquo;享受&rdquo;英语的快乐&mdash;我是如何学英语的
  2. java 开发, jdk 1.6 官方下载地址
  3. eclipse安装CDT插件遇到的问题
  4. 常用iptables规则整理
  5. WCF宿主实践入门
  6. C errors recods
  7. C#修改 Excel指定单元格的值
  8. JS对文档进行操作
  9. JS列表的下拉菜单组件(仿美化控件select)
  10. Android图表库MPAndroidChart(六)——换一种思考方式,水平条形图的实现过程
  11. Linux下MySql的登陆和管理操作
  12. 项目管理目标:添加人员并向其分配任务 - Project
  13. Django 2.0.4 微博第三方登录
  14. Java中的Interrupt使用
  15. JDBC调用MySQL的调用过程CallableStatement
  16. Centos+Redis 集群
  17. AVL平衡二叉树实现
  18. 第五节《Git基本操作》
  19. Android网络通信(8):WiFi Direct
  20. 如何将 Java 项目转换成 Maven 项目

热门文章

  1. 爬虫如何使用phantomjs无头浏览器解决网页源代码经过渲染的问题(以scrapy框架为例)
  2. VS2019 C++动态链接库的创建使用(2) - 客户调用接口
  3. kubernetes 1.17.2结合ceph13.2.8 实现jenkins部署并用traefik2.1代理
  4. Spring WebFlux 入门
  5. 8千字干货教程|java反射精讲
  6. Linux Ubuntu 开发环境配置 ——最具生产力工具一览
  7. 北邮OJ 89. 统计时间间隔 java版
  8. UNIX环境高级编程——TCP/IP网络编程 常用网络信息检索函数
  9. C# 基础知识系列- 6 Lambda表达式和Linq简单介绍
  10. MySQL的死锁系列- 锁的类型以及加锁原理