Cluster Shared Cache [closed]

与世无争的帅哥 提交于 2019-12-02 15:13:51
Talip Ozturk

How about this?

Have a local ConcurrentHashMap as your local cache. Create a Hazelcast distributed map/cache. Start listening for the distributed map events and update your local ConcurrentHashMap.

Now local caches on each member will be the same. Auto-synched.

import com.hazelcast.core.IMap; 
import com.hazelcast.core.Hazelcast;
import com.hazelcast.core.EntryListener;
import com.hazelcast.core.EntryEvent; 
import java.util.concurrent.ConcurrentHashMap;

public class Sample implements EntryListener {
        Map localCache = new ConcurrentHashMap ();

        public static void main(String[] args) { 
                Sample sample = new Sample();
                IMap   map    = Hazelcast.getMap("default"); 

                //Listen for all added/updated/removed entries
                map.addEntryListener(sample, true);  
        }

        public void entryAdded(EntryEvent event) {
             localCache.put(event.getKey(), event.getValue());            
        }

        public void entryRemoved(EntryEvent event) {
             localCache.remove(event.getKey());            
        }

        public void entryUpdated(EntryEvent event) {
             localCache.put(event.getKey(), event.getValue());            
        }
}

Have you considered Infinispan - http://www.jboss.org/infinispan/ ? The API is very simple and based on a standard (JSR-107). The usage is also very simple

CacheManager manager = new DefaultCacheManager(
                GlobalConfiguration.getClusteredDefault() );

Cache cache = manager.getCache();

cache.put("key", "value");

--Hardy

After some more searching, I found JGroup's ReplicatedHashMap. It has not been thoroughly tested but it seems like an excellent start. It fills all my requirements without giving me too much features I don't need. It's also quite flexible. I'm still searching for the "perfect" answer though :)

Thanks for your answers.

Have you considered Terracotta? Might be overkill: http://www.terracotta.org/web/display/orgsite/Data+Caching

There was a JSR in the area of caching a while ago, do any of the following fit the bill: http://java-source.net/open-source/cache-solutions/jcache ?

I personally used FKache a few years ago and it worked well, but I didn't use it in distributed mode.

Is it important that it's a distributed cache with local copies of data? There's also the JavaSpaces stuff if it's shared memory you need...

I've used a few technologies in this area, I can highly recommend JBoss Cache as the best choice for what you're trying to do. It uses JGroups as its transport, but provides a higher-level transactional abstraction. Out-of-the-box it gives you a distributed tree-node structure.

edit: Oh, and JBossCache is independent of JBoss Application Server, you can use it in any environment. If anything, it works better outside of JBossAS than it does inside it.

Memcached has several Java Clients.

My option is Java Caching System from Apache, it has support of TCP Lateral Cache which in my opinion is the feature you need.

http://ehcache.org/ is very good and light cache. It can be shared between multiple JVMs. Internally it can use JGroups.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!