在Android的最佳途径缓存数据缓存、途径、数据、Android

2023-09-04 08:09:51 作者:虚情假意。

我有一个的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();