1.pom引用
这里引用的集成是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();
}
}
手机扫一扫
移动阅读更方便
你可能感兴趣的文章