问题访问LRU磁盘缓存翻过activites [英] Issue accessing LRU disk cache accross activites

查看:167
本文介绍了问题访问LRU磁盘缓存翻过activites的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用杰克沃顿商学院的LRU磁盘缓存来存储和retrive被显示在一个ListView位图。这只要我存储和来自同一个活动中获得的位图工作正常。但是,如果我尝试访问该应用程序内其他活动CACH(即,所以我不必立即下载相同的图像两次),我得到一个NullPointerException异常。我失去了一些东西在这里?这不是一个存储器缓存,其中文件可以/将被删除。应该不是磁盘缓存是从应用程序内的所有尝试各种活动accessable,只要我点他们到正确的目录内部存储中?

  01-14 22:53:44.465:E / AndroidRuntime(10720):显示java.lang.NullPointerException
01-14 22:53:44.465:E / AndroidRuntime(10720):在java.util.regex.Matcher.reset(Matcher.java:181)
。01-14 22:53:44.465:E / AndroidRuntime(10720):在java.util.regex.Matcher中的<&初始化GT;(Matcher.java:94)
01-14 22:53:44.465:E / AndroidRuntime(10720):在java.util.regex.Pattern.matcher(Pattern.java:290)
01-14 22:53:44.465:E / AndroidRuntime(10720):在com.example.echofriendly.DiskLruCache.validateKey(DiskLruCache.java:629)
01-14 22:53:44.465:E / AndroidRuntime(10720):在com.example.echofriendly.DiskLruCache.get(DiskLruCache.java:375)
01-14 22:53:44.465:E / AndroidRuntime(10720):在com.example.echofriendly.DiskLruImageCache.containsKey(DiskLruImageCache.java:145)
01-14 22:53:44.465:E / AndroidRuntime(10720):在com.example.echofriendly.ChatroomFragment.getMessages(ChatroomFragment.java:309)
01-14 22:53:44.465:E / AndroidRuntime(10720):在com.example.echofriendly.ChatroomFragment.access $ 3(ChatroomFragment.java:284)
01-14 22:53:44.465:E / AndroidRuntime(10720):在com.example.echofriendly.ChatroomFragment $ 2.run(ChatroomFragment.java:94)
01-14 22:53:44.465:E / AndroidRuntime(10720):在java.lang.Thread.run(Thread.java:1019)


解决方案

  

应该不是磁盘高速缓存是从应用程序内的所有尝试各种活动accessable,只要我点他们内部存储器中正确的目录?


听起来你在每一个地方,你需要它的活动实例缓存。我不认为这是一个好主意。由于杰克的LruCache使用上可以在我看来,同一个目录的工作日记不同的实例很容易分散注意力对方。

我的建议是你介绍某种单身无论是作为活动和高速缓存或只存储引用只有一个缓存实例为整个应用程序之间的一层。

此外,我会建议你使用某种二级缓存,例如一个内存缓存与diskcache组合(这是我在我的应用程序一样)。所以,你可以先检查内存缓存,并获得图像的真快,如果它被缓存在那里。 (可以是同步的)如果不存在,你问diskcache。 (应该是异步)随着最后一个电话,你会下载。 (也异步)

I am using Jake Wharton's LRU disk cache to store and retrive bitmaps that are displayed in a ListView. This works fine so long as I store and access bitmaps from within the same activity. However, if I try to access the cach from other activities within the app (i.e. so I don't have to downlaod the same image twice) I get a NullPointerException. Am I missing something here? This isn't a memory cache where the files could/would be removed. Shouldn't the disk cache be accessable from all the activites within the app, so long as I point them to the right directory within the internal storage?

01-14 22:53:44.465: E/AndroidRuntime(10720): java.lang.NullPointerException
01-14 22:53:44.465: E/AndroidRuntime(10720): at java.util.regex.Matcher.reset(Matcher.java:181)
01-14 22:53:44.465: E/AndroidRuntime(10720): at java.util.regex.Matcher.<init>(Matcher.java:94)
01-14 22:53:44.465: E/AndroidRuntime(10720): at java.util.regex.Pattern.matcher(Pattern.java:290)
01-14 22:53:44.465: E/AndroidRuntime(10720): at com.example.echofriendly.DiskLruCache.validateKey(DiskLruCache.java:629)
01-14 22:53:44.465: E/AndroidRuntime(10720): at com.example.echofriendly.DiskLruCache.get(DiskLruCache.java:375)
01-14 22:53:44.465: E/AndroidRuntime(10720): at com.example.echofriendly.DiskLruImageCache.containsKey(DiskLruImageCache.java:145)
01-14 22:53:44.465: E/AndroidRuntime(10720): at com.example.echofriendly.ChatroomFragment.getMessages(ChatroomFragment.java:309)
01-14 22:53:44.465: E/AndroidRuntime(10720): at com.example.echofriendly.ChatroomFragment.access$3(ChatroomFragment.java:284)
01-14 22:53:44.465: E/AndroidRuntime(10720): at com.example.echofriendly.ChatroomFragment$2.run(ChatroomFragment.java:94)
01-14 22:53:44.465: E/AndroidRuntime(10720): at java.lang.Thread.run(Thread.java:1019)

解决方案

Shouldn't the disk cache be accessable from all the activites within the app, so long as I point them to the right directory within the internal storage?

Sounds like you are instantiating the cache in every Activity where you need it. I don't think that is a good idea. Since Jake's LruCache uses a journal different instances that work on the same directory could in my opinion easily distract each other.

My suggestion is that you introduce some kind of singleton either as a layer between the activities and the cache or to just store the reference to exactly one cache instance for the whole application.

Furthermore I would suggest that you use some kind of two-level cache, e.g. a memcache combined with a diskcache (this is what I do in my app). So you can first check the memcache and get the image really fast if it was cached there. (can be synchronous) If it is not there you ask the diskcache. (should be asynchronous) And as last call you would download it. (also asynchronous)

这篇关于问题访问LRU磁盘缓存翻过activites的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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