Skip to content
Tony Shen edited this page Feb 9, 2019 · 20 revisions

在 example 模块下,包含了一些常见 Java 使用的例子。

Basic

缓存基本的用法,可以返回 Observable/Flowable/Single/Maybe 对象,或者直接返回 Record<T> 对象。

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

Expire Time

对象在保存到 RxCache 时,可以拥有过期时间。

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

Cache Strategy

缓存存放的策略,缓存的对象支持存放到内存、持久层或者两者都存放该对象。

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

Getting Started

Java

Kotlin

Android

Information

Clone this wiki locally