-
Notifications
You must be signed in to change notification settings - Fork 14
Java
Tony Shen edited this page Feb 8, 2019
·
20 revisions
在 example 模块下,包含了一些常见 Java 使用的例子。
缓存基本的用法,可以返回 Observable/Flowable/Single/Maybe 对象,或者直接返回 Record 对象。
import com.safframework.rxcache.RxCache;
import com.safframework.rxcache.domain.Record;
import domain.User;
import io.reactivex.Observable;
import io.reactivex.functions.Consumer;
/**
* Created by tony on 2018/9/29.
*/
public class Test {
public static void main(String[] args) {
RxCache.config(new RxCache.Builder());
RxCache rxCache = RxCache.getRxCache();
User u = new User();
u.name = "tony";
u.password = "123456";
rxCache.save("test",u);
Observable<Record<User>> observable = rxCache.load2Observable("test", User.class);
observable.subscribe(new Consumer<Record<User>>() {
@Override
public void accept(Record<User> record) throws Exception {
User user = record.getData();
System.out.println(user.name);
System.out.println(user.password);
}
});
}
}缓存保存时,可以带过期时间。
import com.safframework.rxcache.RxCache;
import com.safframework.rxcache.domain.Record;
import domain.User;
/**
* Created by tony on 2018/10/5.
*/
public class TestWithExpireTime {
public static void main(String[] args) {
RxCache.config(new RxCache.Builder());
RxCache rxCache = RxCache.getRxCache();
User u = new User();
u.name = "tony";
u.password = "123456";
rxCache.save("test",u,2000);
try {
Thread.sleep(2500);
} catch (InterruptedException e) {
e.printStackTrace();
}
Record<User> record = rxCache.get("test", User.class);
if (record==null) {
System.out.println("record is null");
}
}
}通过ttl()方法,返回某个对象的剩余生存时间。
import com.safframework.rxcache.RxCache;
import domain.User;
/**
* Created by tony on 2018/11/26.
*/
public class TestTTL {
public static void main(String[] args) {
RxCache.config(new RxCache.Builder());
RxCache rxCache = RxCache.getRxCache();
User u = new User();
u.name = "tony";
u.password = "123456";
rxCache.save("test",u,2500);
try {
Thread.sleep(2300);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("ttl="+rxCache.ttl("test",User.class));
}
}缓存存放的策略,缓存的对象支持存放到内存、持久层或者两者都存放该对象。
RxCache 通过get()方法可以指定相应的CacheStrategy,从 MEMORY、PERSISTENCE 或 ALL 中获取存储的缓存对象。
import com.safframework.rxcache.RxCache;
import com.safframework.rxcache.domain.CacheStrategy;
import com.safframework.rxcache.domain.Record;
import domain.User;
/**
* Created by tony on 2018-12-28.
*/
public class TestCacheStrategy {
public static void main(String[] args) {
RxCache.config(new RxCache.Builder());
RxCache rxCache = RxCache.getRxCache();
User u = new User();
u.name = "tony";
u.password = "123456";
rxCache.save("test",u);
Record<User> record1 = rxCache.get("test", User.class, CacheStrategy.PERSISTENCE);
if (record1 == null) {
System.out.println("record1 is null");
}
Record<User> record2 = rxCache.get("test", User.class, CacheStrategy.ALL);
if (record2 != null) {
System.out.println(record2.getData().name);
System.out.println(record2.getData().password);
}
}
}在 Disk 的实现类 DiskImpl 中,它的构造方法注入了 Converter 接口:
public class DiskImpl implements Disk {
private File cacheDirectory;
private Converter converter;
public DiskImpl(File cacheDirectory) {
this(cacheDirectory,new GsonConverter());
}
public DiskImpl(File cacheDirectory,Converter converter) {
this.cacheDirectory = cacheDirectory;
this.converter = converter;
}
......
}Converter 接口用于对象储存到文件的序列化和反序列化,目前 RxCache 默认支持 Gson,在 extra 模块还有 FastJSONConverter、MoshiConverter。
Converter 的抽象实现类 AbstractConverter 的构造方法注入了 Encryptor 接口:
public abstract class AbstractConverter implements Converter {
private Encryptor encryptor;
public AbstractConverter() {
}
public AbstractConverter(Encryptor encryptor) {
this.encryptor = encryptor;
}
......
}Encryptor 接口用于将存储到 Disk 上的数据进行加密和解密,目前 RxCache 支持 AES128 和 DES 两种加密方式。不使用 Encryptor 接口,则存储到 Disk 上的数据是明文,也就是一串json字符串。
下面展示 DiskImpl 的使用
import com.safframework.rxcache.RxCache;
import com.safframework.rxcache.domain.Record;
import com.safframework.rxcache.persistence.disk.impl.DiskImpl;
import domain.User;
import io.reactivex.Observable;
import io.reactivex.functions.Consumer;
import java.io.File;
/**
* Created by tony on 2018/9/30.
*/
public class TestDiskImpl {
public static void main(String[] args) {
File cacheDirectory = new File("aaa");
if (!cacheDirectory.exists()) {
cacheDirectory.mkdir();
}
DiskImpl diskImpl = new DiskImpl(cacheDirectory);
RxCache.config(new RxCache.Builder().persistence(diskImpl));
RxCache rxCache = RxCache.getRxCache();
User u = new User();
u.name = "tony";
u.password = "123456";
rxCache.save("test",u);
Observable<Record<User>> observable = rxCache.load2Observable("test", User.class);
observable.subscribe(new Consumer<Record<User>>() {
@Override
public void accept(Record<User> record) throws Exception {
User user = record.getData();
System.out.println(user.name);
System.out.println(user.password);
}
});
}
}- General
- Memory
- Persistence
-
Disk
-
Serialization
- Gson
- Fastjson
- Moshi
- Kryo
- Hessian
- FST
- Protobuf
-
Encryption
- AES 128
- DES
-
Serialization
-
Disk
- Cache Statistics
- Spring