-
Notifications
You must be signed in to change notification settings - Fork 15
Disk
Tony Shen edited this page Feb 8, 2019
·
10 revisions
在 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字符串。 如果使用了 Encryptor 的实现类,则存储到 Disk 上的数据是加密后的字符串。
下面展示 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