Integration problems of Hibernate and memcached

Source: Internet
Author: User
Tags file system memcached

Done in the project hibernate are directly using Ehcache as a cache, Ehcache is a good thing, the use of memory + file system can be qualified for most of the situation, and Hibernate and Ehcache is a perfect match, with a very tacit understanding.

However, the problem of cache synchronization is becoming more and more obvious in the cluster environment, although the latest version of Ehcache has supported the function of caching data synchronization of different processes through multicast, such a structure can degrade very well in many nodes of the cluster, and its stability is not clear. So Ehcache aside.

Memcached has heard of the early, it is a client server operating mode of centralized caching system, in many very large web sites are used. Before the Java version of the client API, found that a lot of error prone to errors, due to synchronization problems caused by the super poor performance. The recent Java Client API released a new version, the trial is not the same, so began to write memcached in the project Cacheprovider for hibernate use, the use of the process encountered some problems, now put the central idea of these questions to write out, I hope to Everyone has helped.

First, the structure of Ehcache and memcached is completely different. A Ehcache caching system can define multiple caches at the same time, each cache uses Key-value mode to store the data, and memcahced only Key-value, it is a large hash table. So when we have more than one cache configured in the hibernate, there is a problem in memcached, and the exception that manifests is classcastexception, because different objects use the same key to read and write cached data. This is not a problem in Ehcache, because this is the structure of the Ehcache. Thus, in order for hibernate to use the memcached caching system, we need to package the cached key at provider level, we can combine the cache name passed by Hibernate with key to generate a new key, This key is used for both read and write cache data, so that no exception resulting from a cached data conflict occurs.

Another problem is the caching of queries, which can be an error when we execute a slightly more complex HQL statement and cache the execution of the statement, because the content of the key contains the words defined on some memcached communication protocol Fu To memcached when parsing the protocol, there is an exception, so use the method mentioned above, the key for two times packaging. There are two ways to do this: directly convert the key to Hashcode and then hashcode as a new key, and if you're worried that the generated hashcode may be duplicated (in fact, it's very unlikely), you can also use the MD5 algorithm to generate a new string as the key, This way, we don't have to worry about the existence of some memcached reserved characters in our key to cause errors.

After such transformation, directly to the dlog4j to memcached as a caching system, the unknown has not found any problems, the operation is very good.

For detailed usage, see the comments in Memcachedcacheprovider.java

This article supporting source code: http://www.bianceng.net/java/201212/800.htm

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.