Skip to content

XCache缓存用法

王兴春 edited this page Mar 23, 2017 · 3 revisions

XCache缓存是修改更新的ASimpleCache,在原基础上新增了一些支持和优化了一些代码


首先感谢原作者,如果有什么意见可以联系我!

1、它可以缓存什么东西?

普通的字符串、JsonObject、JsonArray、Bitmap、Drawable、序列化的java对象、byte数据。

2、它有什么特色?

  • 特色主要是:
  • 1:轻量级,使用简单。
  • 2:可配置,可以配置缓存路径,缓存大小,缓存数量等。
  • 3:可以设置缓存超时时间,缓存超时自动失效,并被删除。
  • 4:支持多进程。

3、它在android中可以用在哪些场景?

  • 1、替换SharePreference当做配置文件
  • 2、可以缓存网络请求数据(缓存请求的网络内容[json],设置一定时间后自动失效,重新请求新的数据,提高用户体验,减少服务器压力!)
  • 3、缓存图片、实体类.....

4、如何使用 XCache?

XCache mCache = XCache.get(this);

//添加数据,value可以为不同类型,自己举一反三
mCache.put("test_key1", "test value");
mCache.put("test_key2", "test value", 10);//保存10秒,如果超过10秒去获取这个key,将为null
mCache.put("test_key3", "test value", ACache.TIME_DAY);//保存一天,如果超过两天去获取这个key,将为null

//获取数据
String value = mCache.getAsString("test_key1");
JSONObject value = mCache.getAsJSONObject("test_key1");
JSONArray value = mCache.getAsJSONArray("test_key1");
byte[] value = mCache.getAsBinary("test_key1");
Object value = mCache.getAsObject("test_key1");
Bitmap value = mCache.getAsBitmap("test_key1");
Drawable value = mCache.getAsDrawable("test_key1");