Skip to content

Commit

Permalink
Merge pull request #7 from fengzhizi715/dev
Browse files Browse the repository at this point in the history
Dev
  • Loading branch information
fengzhizi715 authored Feb 14, 2023
2 parents 25df251 + e8d83de commit b533b3b
Show file tree
Hide file tree
Showing 15 changed files with 132 additions and 16 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
RxCache
===

Version 2.1.8
---
2023-2-14
* 第三方库的升级
* 增加对外暴露的日志接口

Version 2.1.7
---
2023-1-16
Expand Down
8 changes: 4 additions & 4 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,23 @@ ext {

caffeine : "2.9.2",

okio : "2.10.0",
okio : "3.3.0",

mapdb : "3.0.8",

moshi : "1.12.0",
moshi : "1.14.0",

protostuff : "1.7.4",

ktor : "1.6.4",

chronicle_map : "3.23ea1",
chronicle_map : "3.24ea2",

bytekit : "v1.3.0",

result : "v1.0.4",

coroutines_utils : "v1.1.5"
coroutines_utils : "v1.1.6"
]
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.safframework.rxcache.domain.CacheHolder;
import com.safframework.rxcache.exception.RxCacheException;
import com.safframework.rxcache.log.LoggerProxy;
import io.protostuff.*;
import io.protostuff.runtime.DefaultIdStrategy;
import io.protostuff.runtime.Delegate;
Expand Down Expand Up @@ -111,6 +112,7 @@ private static <T> Schema<T> getSchema(Class<T> cls) {
public static <T> byte[] serialize(T obj) {

if (obj == null) {
LoggerProxy.INSTANCE.getLogger().i("序列号对象不能为空","rxcache");
throw new RxCacheException("序列号对象不能为空");
}

Expand All @@ -126,6 +128,7 @@ public static <T> byte[] serialize(T obj) {
}
return ProtostuffIOUtil.toByteArray(serializeObject, schema, buffer);
} catch (Exception e) {
LoggerProxy.INSTANCE.getLogger().e("serialize is failed...","rxcache", e);
throw new IllegalStateException(e.getMessage(), e);
} finally {
buffer.clear();
Expand All @@ -143,6 +146,7 @@ public static <T> byte[] serialize(T obj) {
public static <T> T deserialize(byte[] data, Class<T> clazz) {

if (data == null) {
LoggerProxy.INSTANCE.getLogger().i("反序列号对象不能为空","rxcache");
throw new RxCacheException("反序列号对象不能为空");
}

Expand All @@ -158,6 +162,7 @@ public static <T> T deserialize(byte[] data, Class<T> clazz) {
return wrapper.getData();
}
} catch (Exception e) {
LoggerProxy.INSTANCE.getLogger().e("deserialize is failed...","rxcache", e);
throw new IllegalStateException(e.getMessage(), e);
}
}
Expand Down
14 changes: 13 additions & 1 deletion core/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
plugins {
id 'java'
id 'kotlin'
}

group = 'com.github.fengzhizi715'
Expand All @@ -15,8 +16,19 @@ repositories {

dependencies {
testImplementation group: 'junit', name: 'junit', version: '4.12'

implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk8:$kotlin_version"
implementation "com.google.code.gson:gson:${libs.gson}"
implementation "com.github.fengzhizi715.bytekit:core:${libs.bytekit}"
implementation "org.jooq:joor-java-8:${libs.joor_java}"

}
compileKotlin {
kotlinOptions {
jvmTarget = "1.8"
}
}
compileTestKotlin {
kotlinOptions {
jvmTarget = "1.8"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import com.safframework.rxcache.domain.CacheStrategy;
import com.safframework.rxcache.domain.Record;
import com.safframework.rxcache.key.KeyEviction;
import com.safframework.rxcache.log.Logger;
import com.safframework.rxcache.log.LoggerProxy;
import com.safframework.rxcache.memory.Memory;
import com.safframework.rxcache.persistence.Persistence;
import com.safframework.rxcache.persistence.converter.Converter;
Expand Down Expand Up @@ -317,7 +319,7 @@ protected boolean checkKey(String key) {
try {
return getStringData(key)!=null;
} catch (Exception e){
e.printStackTrace();
LoggerProxy.INSTANCE.getLogger().e("checkKey() is failed...", "rxcache", e);
return false;
}
}
Expand Down
41 changes: 41 additions & 0 deletions core/src/main/java/com/safframework/rxcache/RxCache.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
import com.safframework.rxcache.domain.CacheStrategy;
import com.safframework.rxcache.domain.Record;
import com.safframework.rxcache.key.KeyEviction;
import com.safframework.rxcache.log.Logger;
import com.safframework.rxcache.log.LoggerProxy;
import com.safframework.rxcache.memory.Memory;
import com.safframework.rxcache.memory.impl.FIFOMemoryImpl;
import com.safframework.rxcache.persistence.Persistence;
Expand Down Expand Up @@ -59,6 +61,8 @@ private RxCache(Builder builder) {
cacheRepository = new CacheRepository(builder.memory, builder.persistence, builder.keyEviction);
adapter = builder.adapter;

LoggerProxy.INSTANCE.initLogger(builder.log);

if (builder.keyEviction == KeyEviction.ASYNC && adapter!=null) {
adapter.interval(this);
}
Expand Down Expand Up @@ -379,6 +383,7 @@ public void dispose() {
public static final class Builder {
private Memory memory;
private Persistence persistence;
private Logger log;
private KeyEviction keyEviction;
private Adapter adapter;

Expand All @@ -392,6 +397,11 @@ public Builder persistence(Persistence persistence) {
return this;
}

public Builder log(Logger logger) {
this.log = logger;
return this;
}

public Builder keyEviction(KeyEviction keyEviction) {
this.keyEviction = keyEviction;
return this;
Expand All @@ -411,6 +421,37 @@ public RxCache build() {
keyEviction = KeyEviction.SYNC; // 默认情况,使用同步删除淘汰 key 的方式
}

if (log == null) { // 默认情况,设置 Logger
log = new Logger() {
@Override
public void i(String msg, String tag) {
System.out.println(tag + " " + msg);
}

@Override
public void v(String msg, String tag) {
System.out.println(tag + " " + msg);
}

@Override
public void d(String msg, String tag) {
System.out.println(tag + " " + msg);
}

@Override
public void w(String msg, String tag, Throwable tr) {
tr.printStackTrace();
System.out.println(tag + " " + msg);
}

@Override
public void e(String msg, String tag, Throwable tr) {
tr.printStackTrace();
System.err.println(tag + " " + msg);
}
};
}

return new RxCache(this);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
* @FileName: com.safframework.rxcache.key.KeyEviction
* @author: Tony Shen
* @date: 2021-02-09 21:14
* @version: V1.0 <描述当前版本功能>
* @version: V1.0 key 淘汰的枚举,包含同步淘汰 key 和 异步淘汰 key
*/
public enum KeyEviction {
SYNC,
ASYNC
SYNC, // 默认为 同步淘汰 key的方式,在获取缓存 key 的同时,会判断是否过期
ASYNC // 异步淘汰 key 的方式: RxCache 内部定时判断所有的 key ,如果有过期的 key,则删除
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.safframework.rxcache.domain.CacheStatistics;
import com.safframework.rxcache.exception.RxCacheException;
import com.safframework.rxcache.log.LoggerProxy;

import java.util.AbstractQueue;
import java.util.Iterator;
Expand Down Expand Up @@ -55,7 +56,10 @@ public V getSilent(K key) {

public void put(K key, V value) {

if(key == null || value == null) throw new RxCacheException("key is null or value is null");
if(key == null || value == null) {
LoggerProxy.INSTANCE.getLogger().i("key is null or value is null","rxcache");
throw new RxCacheException("key is null or value is null");
}

if(cache.containsKey(key)) {
queue.remove(key);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import com.safframework.rxcache.domain.Record;
import com.safframework.rxcache.domain.Source;
import com.safframework.rxcache.exception.RxCacheException;
import com.safframework.rxcache.log.LoggerProxy;
import com.safframework.rxcache.persistence.converter.Converter;
import com.safframework.rxcache.persistence.converter.GsonConverter;
import com.safframework.rxcache.persistence.disk.Disk;
Expand Down Expand Up @@ -97,7 +98,7 @@ public <T> Record<T> retrieve(String key, Type type) {

return result != null ? new Record<>(Source.PERSISTENCE, safetyKey, result, timestamp, expireTime) : null;
} catch (Exception ignore) {

LoggerProxy.INSTANCE.getLogger().e("retrieve() is failed...","rxcache", ignore);
throw new RxCacheException(ignore);
} finally {

Expand Down Expand Up @@ -143,7 +144,7 @@ public String getStringData(String key) {

return json;
} catch (Exception ignore) {

LoggerProxy.INSTANCE.getLogger().e("getStringData() is failed...","rxcache", ignore);
throw new RxCacheException(ignore);
} finally {

Expand All @@ -169,6 +170,7 @@ public <T> void save(String key, T value, long expireTime) {
outputStream = new FileOutputStream(file, false);
converter.writer(outputStream,new CacheHolder(converter.toJson(value),System.currentTimeMillis(),expireTime,converter.converterName()));
} catch (Exception e) {
LoggerProxy.INSTANCE.getLogger().e("save() is failed...","rxcache", e);
throw new RxCacheException(e);
} finally {
IOUtils.closeQuietly(outputStream);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.safframework.rxcache.reflect;

import com.safframework.rxcache.exception.RxCacheException;
import com.safframework.rxcache.log.LoggerProxy;
import com.safframework.rxcache.reflect.impl.ParameterizedTypeImpl;
import com.safframework.rxcache.reflect.impl.WildcardTypeImpl;

Expand Down Expand Up @@ -38,6 +39,7 @@ public TypeBuilder beginSubType(Class raw) {
public TypeBuilder endSubType() {

if (parent == null) {
LoggerProxy.INSTANCE.getLogger().i("expect beginSubType() before endSubType()", "rxcache");
throw new RxCacheException("expect beginSubType() before endSubType()");
}

Expand All @@ -53,6 +55,7 @@ public TypeBuilder addTypeParam(Class clazz) {
public TypeBuilder addTypeParamExtends(Class... classes) {

if (classes == null) {
LoggerProxy.INSTANCE.getLogger().i("addTypeParamExtends() expect not null Class", "rxcache");
throw new RxCacheException("addTypeParamExtends() expect not null Class");
}

Expand All @@ -64,6 +67,7 @@ public TypeBuilder addTypeParamExtends(Class... classes) {
public TypeBuilder addTypeParamSuper(Class... classes) {

if (classes == null) {
LoggerProxy.INSTANCE.getLogger().i("addTypeParamSuper() expect not null Class", "rxcache");
throw new RxCacheException("addTypeParamSuper() expect not null Class");
}

Expand All @@ -75,6 +79,7 @@ public TypeBuilder addTypeParamSuper(Class... classes) {
public TypeBuilder addTypeParam(Type type) {

if (type == null) {
LoggerProxy.INSTANCE.getLogger().i("addTypeParam expect not null Type", "rxcache");
throw new RxCacheException("addTypeParam expect not null Type");
}

Expand All @@ -86,6 +91,7 @@ public TypeBuilder addTypeParam(Type type) {
public Type build() {

if (parent != null) {
LoggerProxy.INSTANCE.getLogger().i("expect endSubType() before build()", "rxcache");
throw new RxCacheException("expect endSubType() before build()");
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.safframework.rxcache.reflect;

import com.safframework.rxcache.exception.RxCacheException;
import com.safframework.rxcache.log.LoggerProxy;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
Expand All @@ -16,6 +17,7 @@ public TypeToken() {

Type superclass = getClass().getGenericSuperclass();
if (superclass instanceof Class) {
LoggerProxy.INSTANCE.getLogger().i("No generics found!", "rxcache");
throw new RxCacheException("No generics found!");
}
ParameterizedType type = (ParameterizedType) superclass;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.safframework.rxcache.reflect.impl;

import com.safframework.rxcache.exception.RxCacheException;
import com.safframework.rxcache.log.LoggerProxy;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
Expand All @@ -27,12 +28,15 @@ public ParameterizedTypeImpl(Class raw, Type[] args, Type owner) {
private void checkArgs() {

if (raw == null) {
LoggerProxy.INSTANCE.getLogger().i("raw class can't be null", "rxcache");
throw new RxCacheException("raw class can't be null");
}

TypeVariable[] typeParameters = raw.getTypeParameters();
if (args.length != 0 && typeParameters.length != args.length) {
throw new RxCacheException(raw.getName() + " expect " + typeParameters.length + " arg(s), got " + args.length);
String errorMsg = raw.getName() + " expect " + typeParameters.length + " arg(s), got " + args.length;
LoggerProxy.INSTANCE.getLogger().i(errorMsg, "rxcache");
throw new RxCacheException(errorMsg);
}
}

Expand Down
32 changes: 32 additions & 0 deletions core/src/main/kotlin/com/safframework/rxcache/log/LoggerProxy.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package com.safframework.rxcache.log

/**
*
* @FileName:
* com.safframework.rxcache.log.LoggerProxy
* @author: Tony Shen
* @date: 2023/2/13 8:00 PM
* @version: V1.0 <描述当前版本功能>
*/
interface Logger {
fun i(msg: String, tag: String? = "rxcache")
fun v(msg: String, tag: String? = "rxcache")
fun d(msg: String, tag: String? = "rxcache")
fun w(msg: String, tag: String? = "rxcache", tr: Throwable?)
fun e(msg: String, tag: String? = "rxcache", tr: Throwable?)
}

object LoggerProxy {

private lateinit var mLogger: Logger

fun initLogger(logger: Logger) {
mLogger = logger
}

fun getLogger() = mLogger
}

fun String.logI() = LoggerProxy.getLogger().i(this,"rxcache")

fun Throwable.logE(msg: String) = LoggerProxy.getLogger().e(msg, "rxcache", this)
Loading

0 comments on commit b533b3b

Please sign in to comment.