Skip to content
Tony Shen edited this page Jun 22, 2020 · 10 revisions

Disk 继承了 Persistence,它代表的是磁盘缓存。

package com.safframework.rxcache.persistence.disk;

import com.safframework.rxcache.persistence.Persistence;

/**
 * Created by tony on 2018/9/29.
 */
public interface Disk extends Persistence {

    int storedMB();
}

在 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,在 converter 模块还有 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);
            }
        });
    }
}

Serialization

Persistence 默认使用 gson 实现对象的序列化和反序列化,当然也额外支持使用 fastjson、moshi 等等实现对象的序列化和反序列化。

下面封装了 BaseDiskWithConverter

public class BaseDiskWithConverter {

    public BaseDiskWithConverter(Converter converter) {

        File cacheDirectory = new File("aaa");

        if (!cacheDirectory.exists()) {

            cacheDirectory.mkdir();
        }

        DiskImpl diskImpl = new DiskImpl(cacheDirectory,converter);

        RxCache.config(new RxCache.Builder().persistence(diskImpl));

        RxCache rxCache = RxCache.getRxCache();

        testObject(rxCache);
        testMap(rxCache);
        testList(rxCache);
        testSet(rxCache);
    }

    public static void testObject(RxCache rxCache) {

        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);
            }
        });
    }

    public static void testMap(RxCache rxCache){

        Map<String,User> map = new HashMap<>();

        User u1 = new User();
        u1.name = "tonyMap1";
        u1.password = "map1123456";
        map.put("u1",u1);

        User u2 = new User();
        u2.name = "tonyMap12";
        u2.password = "map12345";
        map.put("u2",u2);
        rxCache.save("map",map);
        Type type = TypeBuilder
                .newInstance(HashMap.class)
                .addTypeParam(String.class)
                .addTypeParam(User.class)
                .build();

        Observable<Record<Map<String,User>>> observable = rxCache.load2Observable("map", type);

        observable.subscribe(new Consumer<Record<Map<String,User>>>() {

            @Override
            public void accept(Record<Map<String,User>> record) throws Exception {

                Map<String,User> recordDataList = record.getData();

                if (Preconditions.isNotBlank(recordDataList)) {

                    User user = recordDataList.get("u1");
                    System.out.println(user.name);
                    System.out.println(user.password);


                    User user2 = recordDataList.get("u2");
                    System.out.println(user2.name);
                    System.out.println(user2.password);
                }
            }
        });
    }

    public static void testList(RxCache rxCache) {

        List<User> list = new ArrayList<>();

        User u1 = new User();
        u1.name = "tonyList1";
        u1.password = "list1123456";
        list.add(u1);

        User u2 = new User();
        u2.name = "tonyList12";
        u2.password = "list12345";
        list.add(u2);
        rxCache.save("list", list);

        Type type = TypeBuilder
                .newInstance(List.class)
                .addTypeParam(User.class)
                .build();

        Observable<Record<List<User>>> observable = rxCache.load2Observable("list", type);

        observable.subscribe(new Consumer<Record<List<User>>>() {

            @Override
            public void accept(Record<List<User>> record) throws Exception {

                List<User> recordDataList = record.getData();

                if (Preconditions.isNotBlank(recordDataList)) {
                    for (User user : recordDataList) {
                        System.out.println(user.name);
                        System.out.println(user.password);
                    }
                }
            }
        });
    }

    public static void testSet(RxCache rxCache) {

        Set<User> set = new HashSet<>();

        User u1 = new User();
        u1.name = "tonySet1";
        u1.password = "set1123456";
        set.add(u1);

        User u2 = new User();
        u2.name = "tonySet12";
        u2.password = "set12345";
        set.add(u2);
        rxCache.save("set", set);

        Type type = TypeBuilder
                .newInstance(Set.class)
                .addTypeParam(User.class)
                .build();

        Observable<Record<Set<User>>> observable = rxCache.load2Observable("set", type);

        observable.subscribe(new Consumer<Record<Set<User>>>() {

            @Override
            public void accept(Record<Set<User>> record) throws Exception {

                Set<User> recordDataList = record.getData();

                if (Preconditions.isNotBlank(recordDataList)) {
                    for (User user : recordDataList) {
                        System.out.println(user.name);
                        System.out.println(user.password);
                    }
                }
            }
        });
    }
}

使用 fastjson 实现序列化

public class TestDiskImplWithFastjson {

    public static void main(String[] args) {

        new BaseDiskWithConverter(new FastJSONConverter());
    }
}

Encryption

import com.safframework.rxcache.RxCache;
import com.safframework.rxcache.domain.Record;
import com.safframework.rxcache.persistence.converter.GsonConverter;
import com.safframework.rxcache.persistence.encrypt.AES128Encryptor;
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/10/2.
 */
public class TestDiskImplWithEncrypt {

    public static void main(String[] args) {

        File cacheDirectory = new File("aaa");

        if (!cacheDirectory.exists()) {

            cacheDirectory.mkdir();
        }

        AES128Encryptor encryptor = new AES128Encryptor("abcdefghijklmnop");
        DiskImpl diskImpl = new DiskImpl(cacheDirectory,new GsonConverter(encryptor));

        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);
            }
        });
    }
}

Getting Started

Java

Kotlin

Android

Information

Clone this wiki locally