hibernate-memcached补丁:支持Whalin的MemCached Java客户端

来源:互联网 发布:工程清单计价软件 编辑:程序博客网 时间:2024/05/29 04:22

 hibernate-memcached(http://code.google.com/p/hibernate-memcached/ )是允许hibernate使用MemCached作为二级缓存的项目,它用到了spymemcached(http://code.google.com/p/spymemcached/ )这个MemCached的Java客户端。花了一点时间做了个补丁,让它也支持Whalin的Java客户端(http://whalin.com/memcached/ )。


所有的代码(包括原hibernate-memcached的所有源码)、javadoc及重新制作的发布包请见附件,限于篇幅就不具体说明了。以下是我更新过的hibernate-memcached的配置说明,原配置说明请参见http://code.google.com/p/hibernate-memcached/wiki/Configuration

 

Adding hibernate-memcached to your application

If you are using Maven2 for your application build you can follow the instructions on the MavenRepository page. For those that aren't using Maven2 you'll need to collect a few simple jars for yourself. I am going to assume you already have hibenrnate3 setup before you came here.

You'll need...

  • hibernate-memcached-<version>.jar
  • commons-codec 1.3
  • slf4j-1.5.0.jar
  • An slf4j implementation for your preferred logging framework. For log4j you can use slf4j-log4j12-1.5.0.jar .

If you are using spymemcached as the MemCached client, you also need

  • memcached-2.1.jar
  • spy-2.4.jar

or if you prefer Whalin's memcached client for java , you need to download java_memcached-release_2.0.1.jar from the download page according to your Java VM version.

Configuring Hibernate

The first thing to do is to tell hibernate which cache provider you'd like to use, and to enable the second level cache.

hibernate.cache.provider_classcom.googlecode.hibernate.memcached.MemcachedCacheProvider

Using just the property above you get basic entity caching by default. The hibernate-memcached library supports query caching, but you have to enable query caching separately.

hibernate.cache.use_query_cachetrue

!Now you can start setting up the hibernate-memcached specific properties. The hibernate-memcached properties are divided into two categories; cache-wide settings and cache-region settings.

Cache Wide Settings

The cache wide settings all have defaults can mostly be overwritten at a cache-region level. For both spymemcached and Whalin's client, you can set:

PropertyDefaultDescriptionhibernate.memcached.serverslocalhost:11211Space delimited list of memcached instances in host:port formathibernate.memcached.cacheTimeSeconds300The default number of seconds items should be cached. Can be overriden at the regon level.hibernate.memcached.keyStrategyHashCodeKeyStrategySets the strategy class to to use for generating cache keys. Must provide a class name that implements KeyStrategyhibernate.memcached.clearSupportedfalseEnables support for the MemcachedCache.clear() method for all cache regions. The way clear is implemented for memcached is expensive and adds overhead to all get/set operations. It is not recommended for production use.

For spymemcached, you can set:

hibernate.memcached.readBufferSizeDefaultConnectionFactory.DEFAULT_READ_BUFFER_SIZEThe read buffer size for each server connection from this factoryhibernate.memcached.operationQueueLengthDefaultConnectionFactory.DEFAULT_OP_QUEUE_LENMaximum length of the operation queue returned by this connection factoryhibernate.memcached.operationTimeoutDefaultConnectionFactory.DEFAULT_OPERATION_TIMEOUTDefault operation timeout in millisecondshibernate.memcached.hashAlgorithmHashAlgorithm.KETAMA_HASHWhich hash algorithm to use when adding items to the cache. Note: the MemcachedClient defaults to using HashAlgorithm.NATIVE_HASH, while the hibernate-memcached cache defaults to KETAMA_HASH for "consistent hashing"

For Whalin's client, you can set:

hibernate.memcached.weights Space delimited list of server weights. If not set, all servers get the same weighthibernate.memcached.errorHandlercom.googlecode.hibernate.memcached.
dangamemcached.SimpleErrorHandler
Error handler used by Whalin's client internallyhibernate.memcached.poolNamedefaultThe name of SockIOPoolhibernate.memcached.initConn1Initial pool sizehibernate.memcached.minConn1Min pool sizehibernate.memcached.maxConn10Max pool sizehibernate.memcached.maxIdle3000000Max idle time for available sockets. Default 5 minuteshibernate.memcached.socketTimeout30000Default timeout of socket reads. Default 30 secondshibernate.memcached.socketConnectTimeout3000Default timeout of socket connections. Default 3 secondshibernate.memcached.maintSleep30000Maintenance thread sleep time. Default 30 secondshibernate.memcached.compressEnabletrueIf compression enabledhibernate.memcached.defaultEncodingUTF-8Default encoding

Cache Region Settings

Cache regon properties are set by giving your cached data a "region name" in hibernate. You can tune the MemcachedCache instance for your region using the following properties. These properties essentially override the cache-wide properties above.

PropertyDefaultDescriptionhibernate.memcached.[region-name].cacheTimeSecondsnone, see hibernate.memcached.cacheTimeSecondsSet the cache time for this cache region, overriding the cache-wide setting.hibernate.memcached.[region-name].keyStrategynone, see hibernate.memcached.keyStrategyOverrides the strategy class to to use for generating cache keys in this cache region. Must provide a class name that implements KeyStrategyhibernate.memcached.[region-name].clearSupportednone, see hibernate.memcached.clearSupportedEnables clear() operations for this cache region only. Again, the clear operation incurs cost on every get/set operation.
  • Hibernate-MemCached-1.0.zip (139.3 KB)
  • 下载次数: 74

来源:http://movingboy.javaeye.com/blog/250534

原创粉丝点击