JetCache 框架介绍
整体框架
- 注解中使用表达式的解析类:ExpressionEvaluator
- 注解@EnableCache的作用是即使对应的方法上的注解@Cached的enabled属性设置为false,只要@EnableCache开启,则对应的缓存就会被开启
- 注解使用的例子如下源码中例子拷贝:
@Component("testBean")
public class TestBean {
private static int count = 0;
Map m = new HashMap<>();
public TestBean() {
}
public int noCacheCount(){
return count++;
}
@Cached
public static int staticCount() {
return count;
}
@Cached
public int count() {
return count++;
}
@Cached(expire = 50, cacheType = CacheType.LOCAL, timeUnit = TimeUnit.MILLISECONDS)
public int countWithExpire50() {
return count++;
}
@Cached
public int count1() {
return count++;
}
@Cached(cacheType = CacheType.LOCAL)
public int countWithLocalCache(){
return count++;
}
@Cached(cacheType = CacheType.BOTH)
public int countWithBoth(){
return count++;
}
@Cached(enabled = false)
public int countWithDisabledCache(){
return count++;
}
@Cached(area = "A1" , cacheType = CacheType.LOCAL)
public int countLocalWithDynamicQuery(DynamicQuery q) {
return count++;
}
@Cached(area = "A1" , cacheType = CacheType.LOCAL, keyConvertor = "fastjson")
public int countLocalWithDynamicQueryAndKeyConvertor(DynamicQuery q) {
return count++;
}
@Cached(area = "A1")
public int countRemoteWithDynamicQuery(DynamicQuery q) {
return count++;
}
@Cached(area = "A1")
public int countLocalWithDynamicQueryWithEquals(DynamicQueryWithEquals q) {
return count++;
}
@Cached(condition = "mvel{bean('configBean').trueProp}")
public int countEnabledWithConfigBean(){
return count++;
}
@Cached(condition = "mvel{bean('configBean').falseProp}")
public int countDisabledWithConfigBean(){
return count++;
}
@Cached(condition = "mvel{xxx('configBean').trueProp}")
public int countWithWrongCondition(){
return count++;
}
@Cached(condition = "mvel{args[0]}")
public int count(boolean useCache){
return count++;
}
@Cached(name="n1")
public int namedCount1_WithNameN1(){
return count++;
}
@Cached(name="n1")
public int namedCount2_WithNameN1(){
return count++;
}
@Cached(name="n2")
public int namedCount_WithNameN2(){
return count++;
}
@Cached(name = "c1", key = "args[0]")
public int count(String id) {
Integer v = m.get(id);
if (v == null) {
v = count++;
}
v++;
m.put(id, v);
return v;
}
@CacheUpdate(name = "c1", key = "#id", value = "args[1]")
public void update(String id, int value) {
m.put(id, value);
}
@CacheInvalidate(name = "c1", key = "args[0]")
public void delete(String id) {
m.remove(id);
}
@CacheUpdate(name = "c2", key = "args[0]", value = "args[1]")
public void update2(String id, int value) {
m.put(id, value);
}
@CacheInvalidate(name = "c2", key = "args[0]")
public void delete2(String id) {
m.remove(id);
}
}
CompletionStage 简介
Future: Java 8 之前的 Java 版本功能较弱,仅支持两种用法:要么检查 future 是否已经完成,要么等待 future 完成;Java 8 增加了 CompletableFuture
redis lettuce简介
Lettuce是一个可伸缩的线程安全的Redis客户端,用于同步,异步和反应使用。 多个线程可以共享同一个RedisConnection。它利用优秀netty NIO框架来高效地管理多个连接。 支持先进的Redis功能,如Sentinel,集群,流水线,自动重新连接和Redis数据模型。用法如下:
RedisClient redisClient = RedisClient.create("redis://[email protected]:6379/0");// 新建客户端
StatefulRedisConnection connection = redisClient.connect();// 连接
RedisAsyncCommands asynCommands = connection.async();// 异步操作
RedisFuture future = asynCommands.get("key");// 使用future
future.thenAccept((e) -> System.out.println(e)); // 接收到数据时 执行
// 执行其他操作
// 关闭连接
connection.close();
redisClient.shutdown();
java.util.function-Function接口 简介
Function 接口:This is a functional interface and can therefore be used as the assignment target for a lambda expression or method reference. 文档参见:https://docs.oracle.com/javase/8/docs/api/java/util/function/Function.html
//将Function对象应用到输入的参数上,然后返回计算结果。
R apply(T t);
//返回一个先执行当前函数对象apply方法再执行after函数对象apply方法的函数对象。
default Function andThen(Function super R, ? extends V> after) {
Objects.requireNonNull(after);
return (T t) -> after.apply(apply(t));
}
//返回一个先执行before函数对象apply方法再执行当前函数对象apply方法的函数对象
default Function compose(Function super V, ? extends T> before) {
Objects.requireNonNull(before);
return (V v) -> apply(before.apply(v));
}
函数式接口 - Functional Interface
所谓的函数式接口,当然首先是一个接口,然后就是在这个接口里面只能有一个抽象方法。这种类型的接口也称为SAM接口,即Single Abstract Method interfaces。它们主要用在Lambda表达式和方法引用(实际上也可认为是Lambda表达式)上。如定义了一个函数式接口如下:
@FunctionalInterface
interface MyFirstFunctionInterface
{
void say(String message);
}
那么就可以使用Lambda表达式来表示该接口的一个实现(注:JAVA 8 之前一般是用匿名类实现的):
MyFirstFunctionInterface firstInterface= message -> System.out.println("Hello " + message);
详细信息参见:https://docs.oracle.com/javase/8/docs/api/java/lang/FunctionalInterface.html
项目源码和文档
项目源码和文档:https://github.com/alibaba/jetcache.git