Java multithreaded cache using a single updater thread

I have a web service with an average of ~1k request threads running at the same time These threads access data from the cache (currently on ehcache) When an entry in the cache expires, the thread that hits the expired entry attempts to get a new value from the DB, while other threads also try to hit the entry block, that is, I use the blockingehcache decorator I want other threads to use the stale value corresponding to the missing key instead of waiting for the get thread Has any third party developed an ehcache decorator for this purpose? Do you know of any other caching solutions with this behavior? Other suggestions?

Solution

I don't know whether ehcache is enough to provide specific suggestions to solve the problem, so I will outline what I will do without ehcache

It is assumed that all threads access this cache using a service interface named fooservice and a service bean named simplefooservice The service will have the methods (also cached) needed to get the required data In this way, you can hide the fact that it is cached from the front end (HTTP request object)

Instead of simply storing the data to be cached in the properties of the service, we create a special object for it We call it foocache manager It stores the cache in a property of foocachemanger (such as its map type) It will have getters to get the cache It also has a special method called reload (), which loads data from the DB (by calling the service method to get the data, or by Dao) and replaces the cached content (saved in the attribute)

Here are the tips:

>Declare the cache attribute in foocachemanger as atomicreference (a new object declared in Java 1.5) This ensures thread safety when you read and allocate threads Your read / write operations will never conflict or read half write values. > Reload () will first load the data into the temporary mapping, and then assign the new mapping to the attributes saved in foocachemanager after completion Since the attribute is atomicreference, the assignment is atomic, so it basically swipes the map instantly without locking. > TTL implementation - let foocachemanager implement the quartzjob interface and make it a quartz job In the job's execute method, let it run reload() In spring XML, this job is defined to run every XX minutes (your TTL). If propertyplaceholderconfigurer is used, it can also be defined in the property file

This method works because the reading thread:

>Do not block reading > do not call isexpired() every time you read, i.e. 1K / s

The write thread does not block when writing data

If not, I can add sample code

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
分享
二维码
< <上一篇
下一篇>>