Skip to content

Cache Statistics

Tony Shen edited this page Jul 11, 2020 · 12 revisions

RxCache 的 info() 方法用于显示缓存中的信息。

配置 Memory

如果使用了 memory,则可以看到缓存使用的统计数据。

import com.safframework.rxcache.RxCache;
import com.safframework.rxcache.memory.impl.FIFOMemoryImpl;

/**
 * Created by tony on 2019-02-08.
 */
public class TestCacheStatistics {

    public static void main(String[] args) {

        RxCache.config(new RxCache.Builder().memory(new FIFOMemoryImpl(10)));

        RxCache rxCache = RxCache.getRxCache();

        for (int i=0;i<10;i++) {

            rxCache.save("test"+i,i);
        }

        rxCache.save("test10",10); // 保存第十一个数据,在内存缓存中移除第一个数据

        rxCache.info();
    }
}

执行结果:

{"memory":{"keys":["test4","test5","test2","test3","test8","test9","test6","test10","test7","test1"],"memoryImpl":"FIFOMemoryImpl","cacheStatistics":{"size":10,"putCount":11,"evictionCount":1,"hitCount":0,"missCount":0}}}

将以上字符串格式化后,可以看到cacheStatistics

{
    "memory":{
        "keys":[
            "test4",
            "test5",
            "test2",
            "test3",
            "test8",
            "test9",
            "test6",
            "test10",
            "test7",
            "test1"],
        "memoryImpl":"FIFOMemoryImpl",
        "cacheStatistics":{
            "size":10,
            "putCount":11,
            "evictionCount":1,
            "hitCount":0,
            "missCount":0
        }
    }
}

配置 Persistence

如果使用了 persistence,则可以看到持久层使用的类型和 Converter 的类型。

public class TestCacheStatistics {

    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().memory(new FIFOMemoryImpl(10)).persistence(diskImpl));

        RxCache rxCache = RxCache.getRxCache();

        for (int i=0;i<10;i++) {

            rxCache.save("test"+i,i);
        }

        rxCache.save("test10",10); // 保存第十一个数据,在内存缓存中移除第一个数据

        rxCache.info();
    }
}

执行结果:

{
    "memory":{
        "keys":[
            "test4",
            "test5",
            "test2",
            "test3",
            "test8",
            "test9",
            "test6",
            "test10",
            "test7",
            "test1"],
        "memoryImpl":"FIFOMemoryImpl",
        "cacheStatistics":{
            "size":10,
            "putCount":11,
            "evictionCount":1,
            "hitCount":0,
            "missCount":0
        }
    },
    "persistence":{
        "keys":[
            "test10",
            "test9",
            "test0",
            "test7",
            "test6",
            "test1",
            "test8",
            "test4",
            "test3",
            "test2",
            "test5"],
        "persistenceImpl":"DiskImpl",
        "converterName":"gson"
    }
}

Getting Started

Java

Kotlin

Android

Information

Clone this wiki locally