Which Java collection should I use to implement thread safe caching?

I want to implement a simple cache without too much work (NATURAL) In my opinion, one of the standard Java collections should be enough, with a little extra work Specifically, I am storing the response from the server. The key can be a request URL string or a hash code generated from the URL

I thought I could use weakhashmap, but it seems that this method forces me to manage the objects I want to keep. Any objects without strong references are immediately swept away Should I try a concurrenthashmap with softreference value? Or will you be so sober?

I'm looking at the LinkedHashMap class With some modifications, it looks promising for MRU caching Any other suggestions?

No matter which collection I use, should I try to trim LRU values manually, or can I trust virtual machine bias to recycle recently accessed objects?

FYI, I'm developing Android, so I don't want to import any third-party libraries I'm working on a very small heap (16 to 24 MB), so the VM may be very eager to recycle resources I think GC will be positive

Solution

If you use softreference - based keys, the VM will favor (strongly) the most recently accessed objects However, determining cache semantics will be quite difficult – the only guarantee softreference gives you (via WeakReference) is to clear outofmemoryerror before throwing it It is perfectly legal for JVM implementations to treat them the same as weakreferences, which may result in a cache that does not cache anything

I don't know how to work on Android, but with sun's recent JVM, you can use the - XX: softreflrupolicymspermb command line option to adjust the softreference behavior. This option determines the number of milliseconds that can be easily accessed objects will retain and the available memory per MB of heap As you can see, it will be very difficult to obtain any predictable life cycle behavior. This setting is global for all soft references in the virtual machine and cannot be adjusted separately for the use of softreferences of a single class (different parameters are required for each use)

The easiest way to create an LRU cache is by extending LinkedHashMap as described here Because you need thread safety, the easiest way to initially extend is to use collections. On an instance of this custom class Synchronize DMAP to ensure safe concurrent behavior

Beware of premature optimization - unless you need very high throughput, the theoretical cost of coarse synchronization is unlikely to be a problem And the good news - if the analysis shows that your running speed is too slow due to strong lock contention, you will have enough information for the runtime use of your cache. You can propose an appropriate lock free alternative (based on concurrenthashmap and some manual LRU processing) without guessing its load profile

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>