Skip to content

Commit

Permalink
refactor cache module
Browse files Browse the repository at this point in the history
  • Loading branch information
bdqfork committed Feb 21, 2020
1 parent b5ccc0b commit 9db4889
Show file tree
Hide file tree
Showing 13 changed files with 191 additions and 82 deletions.
16 changes: 16 additions & 0 deletions festival-cache/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
apply plugin: 'kotlin'

def libs = rootProject.ext.libraries

dependencies {
implementation "io.vertx:vertx-redis-client:${libs.vertx}"
implementation "io.vertx:vertx-lang-kotlin-coroutines:${libs.vertx}"
implementation project(':festival-context')
}

compileKotlin {
kotlinOptions.jvmTarget = "1.8"
}
compileTestKotlin {
kotlinOptions.jvmTarget = "1.8"
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cn.bdqfork.web.cache.annotation;
package cn.bdqfork.cache.annotation;

import java.lang.annotation.*;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cn.bdqfork.web.cache.annotation;
package cn.bdqfork.cache.annotation;

import java.lang.annotation.*;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package cn.bdqfork.kotlin.web.constant
package cn.bdqfork.cache.constant

/**
* @author bdq
* @since 2020/2/17
*/
object CacheProperty {
const val DEFAULT_EXPIRE_TIME = 60

const val CACHE_ENABLE = "cache.enable"
const val CACHE_TYPE = "cache.type"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package cn.bdqfork.cache.processor;

import cn.bdqfork.cache.provider.CacheProvider;
import cn.bdqfork.cache.proxy.CacheInvocationHandler;
import cn.bdqfork.context.aware.BeanFactoryAware;
import cn.bdqfork.context.aware.ClassLoaderAware;
import cn.bdqfork.core.exception.BeansException;
import cn.bdqfork.core.factory.BeanFactory;
import cn.bdqfork.core.factory.processor.BeanPostProcessor;
import cn.bdqfork.core.proxy.javassist.Proxy;
import cn.bdqfork.core.util.AopUtils;

/**
* @author bdq
* @since 2020/2/21
*/
public class CacheSupportProcessor implements BeanPostProcessor, BeanFactoryAware, ClassLoaderAware {
private ClassLoader classLoader;
private BeanFactory beanFactory;

@Override
public Object postProcessBeforeInitializtion(String beanName, Object bean) throws BeansException {
return bean;
}

@Override
public Object postProcessAfterInitializtion(String beanName, Object bean) throws BeansException {
Class<?> targetClass = AopUtils.getTargetClass(bean);
CacheProvider cacheProvider = beanFactory.getBean(CacheProvider.class);
return Proxy.newProxyInstance(classLoader, targetClass.getInterfaces(), new CacheInvocationHandler(bean, cacheProvider));
}

@Override
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
this.beanFactory = beanFactory;
}

@Override
public void setClassLoader(ClassLoader classLoader) throws BeansException {
this.classLoader = classLoader;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package cn.bdqfork.cache.provider;

import cn.bdqfork.cache.constant.CacheProperty;

import java.io.Serializable;

/**
* @author bdq
* @since 2020/2/21
*/
public abstract class AbstractCacheProvider implements CacheProvider {
@Override
public void put(String key, Serializable value) {
put(key, value, CacheProperty.DEFAULT_EXPIRE_TIME);
}

@Override
public Object update(String key, Serializable value) {
return update(key, value, CacheProperty.DEFAULT_EXPIRE_TIME);
}

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package cn.bdqfork.web.cache;
package cn.bdqfork.cache.provider;

import io.reactivex.Flowable;

import java.io.Serializable;

Expand All @@ -17,58 +16,61 @@ public interface CacheProvider {
*
* @param key
* @param value
* @return
*/
Flowable<Void> put(String key, Serializable value);
void put(String key, Serializable value);

/**
* 添加缓存
*
* @param key
* @param value
* @return
*/
Flowable<Void> put(String key, Serializable value, long expireTime);
void put(String key, Serializable value, long expireTime);

/**
* 删除缓存
*
* @param key
* @return
*/
Flowable<Void> remove(String key);
void remove(String key);

/**
* 更新缓存
*
* @param key
* @param value
* @return
* @return 旧的值
*/
Flowable<Void> update(String key, Serializable value);
Object update(String key, Serializable value);

/**
* 更新缓存
*
* @param key
* @param value
* @return
* @return 旧的值
*/
Flowable<Void> update(String key, Serializable value, long expireTime);
Object update(String key, Serializable value, long expireTime);

/**
* 获取缓存
*
* @param key
* @return
* @return 缓存的值
*/
Flowable<Serializable> get(String key);
Object get(String key);

/**
* 清楚所有缓存
* key对应的缓存是否存在
*
* @param key
* @return
*/
Flowable<Void> clear();
boolean containKey(String key);

/**
* 清空所有缓存
*/
void clear();

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package cn.bdqfork.cache.provider;

import java.io.Serializable;

/**
* @author bdq
* @since 2020/2/21
*/
public class RedisCacheProvider extends AbstractCacheProvider {
@Override
public void put(String key, Serializable value, long expireTime) {

}

@Override
public void remove(String key) {

}

@Override
public Object update(String key, Serializable value, long expireTime) {
return null;
}

@Override
public Object get(String key) {
return null;
}

@Override
public boolean containKey(String key) {
return false;
}

@Override
public void clear() {

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package cn.bdqfork.cache.proxy;

import cn.bdqfork.cache.provider.CacheProvider;
import cn.bdqfork.cache.annotation.Cache;
import cn.bdqfork.cache.annotation.Evict;
import cn.bdqfork.core.util.AnnotationUtils;

import java.io.Serializable;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

/**
* @author bdq
* @since 2020/2/21
*/
public class CacheInvocationHandler implements InvocationHandler {
private Object target;
private CacheProvider cacheProvider;

public CacheInvocationHandler(Object target, CacheProvider cacheProvider) {
this.target = target;
this.cacheProvider = cacheProvider;
}

@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {

if (AnnotationUtils.isAnnotationPresent(method, Cache.class)) {
Cache cache = AnnotationUtils.getMergedAnnotation(method, Cache.class);
String cacheKey = cache.value();
if (cacheProvider.containKey(cacheKey)) {
return cacheProvider.get(cacheKey);
}
Object result = method.invoke(target, args);
cacheProvider.put(cacheKey, (Serializable) result, cache.expireTime());
return result;
}

if (AnnotationUtils.isAnnotationPresent(method, Evict.class)) {
Object result = method.invoke(target, args);
Evict evict = AnnotationUtils.getMergedAnnotation(method, Evict.class);
cacheProvider.remove(evict.value());
return result;
}

return method.invoke(target, args);
}
}
1 change: 0 additions & 1 deletion festival-kotlin-web/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ def libs = rootProject.ext.libraries
dependencies {
api "io.vertx:vertx-web:${libs.vertx}"
implementation "io.vertx:vertx-lang-kotlin-coroutines:${libs.vertx}"
implementation "com.caucho:hessian:${libs.hessian}"
compileOnly "com.fasterxml.jackson.dataformat:jackson-dataformat-xml:${libs.jackson}"
compileOnly "joda-time:joda-time:${libs.joda}"
api project(':festival-context')
Expand Down

This file was deleted.

This file was deleted.

1 change: 1 addition & 0 deletions settings.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,5 @@ include 'festival-example:kotlin-web'
findProject(':festival-example:kotlin-web')?.name = 'kotlin-web'
include 'festival-example:web'
findProject(':festival-example:web')?.name = 'web'
include 'festival-cache'

0 comments on commit 9db4889

Please sign in to comment.