在Android的最佳途径缓存数据 [英] Best Way to Cache Data in Android

查看:124
本文介绍了在Android的最佳途径缓存数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个的ArrayList 自定义的,简单的序列化的对象,我想缓存到磁盘读取重-发射。我的数据是非常小的,大约25物体和最多5列出了,所以我觉得SQLite的将是矫枉过正。在iPhone的世界,我会用的NSKeyedArchiver NSKeyedUnarchiver 伟大的工程。在Android上我已经尝试与做一个的FileOutputStream 的ObjectOutputStream 键,而结果是一样的,性能是可怕的。有没有更好的(读取速度更快)的方式来缓存在Android的小物件到文件系统?

I have an ArrayList of custom, simple Serializable objects I would like to cache to disk and read on re-launch. My data is very small, about 25 objects and at most 5 lists so I think SQLite would be overkill. In the iPhone world I would use NSKeyedArchiver and NSKeyedUnarchiver which works great. On Android I've attempted to do this with with a FileOutputStream and ObjectOutputStream and while the result is the same, the performance is terrible. Is there a better (read faster) way to cache small objects to the file system in Android?

推荐答案

对于什么是值得我缓存我的一些字符串数据使用的BufferedWriter / BufferedReader中盘,它的速度非常快。事实的物质,它比存储相同数据到共享preferences更快。在code是这样的(请注意,事情发生快,当你提供缓冲区大小)

For what it worth I cache some of my String data to disk using BufferedWriter/BufferedReader and it's very fast. Matter of fact it is faster than storing the same data to SharedPreferences. The code goes something like this (note that things happen faster when you provide buffer size)

final BufferedWriter out = new BufferedWriter(new FileWriter(file), 1024);
out.write(stuff);
out.close();

这篇关于在Android的最佳途径缓存数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆