设为首页 收藏本站
查看: 931|回复: 0

[经验分享] [转]hibernate-memcached补丁:支持Whalin的MemCached Java客户端

[复制链接]
累计签到:6 天
连续签到:1 天
发表于 2015-9-1 09:50:50 | 显示全部楼层 |阅读模式
hibernate-memcached补丁:支持Whalin的MemCached Java客户端

  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:
Property Default Description
hibernate.memcached.servers localhost:11211 Space delimited list of memcached instances in host:port format
hibernate.memcached.cacheTimeSeconds 300 The default number of seconds items should be cached. Can be overriden at the regon level.
hibernate.memcached.keyStrategy HashCodeKeyStrategy Sets the strategy class to to use for generating cache keys. Must provide a class name that implements KeyStrategy
hibernate.memcached.clearSupported false Enables 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.readBufferSize DefaultConnectionFactory.DEFAULT_READ_BUFFER_SIZE The read buffer size for each server connection from this factory
hibernate.memcached.operationQueueLength DefaultConnectionFactory.DEFAULT_OP_QUEUE_LEN Maximum length of the operation queue returned by this connection factory
hibernate.memcached.operationTimeout DefaultConnectionFactory.DEFAULT_OPERATION_TIMEOUT Default operation timeout in milliseconds
hibernate.memcached.hashAlgorithm HashAlgorithm.KETAMA_HASH Which 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 [td]Space delimited list of server weights. If not set, all servers get the same weight
hibernate.memcached.errorHandler com.googlecode.hibernate.memcached.
dangamemcached.SimpleErrorHandler
Error handler used by Whalin's client internally
hibernate.memcached.poolName default The name of SockIOPool
hibernate.memcached.initConn 1 Initial pool size
hibernate.memcached.minConn 1 Min pool size
hibernate.memcached.maxConn 10 Max pool size
hibernate.memcached.maxIdle 3000000 Max idle time for available sockets. Default 5 minutes
hibernate.memcached.socketTimeout 30000 Default timeout of socket reads. Default 30 seconds
hibernate.memcached.socketConnectTimeout 3000 Default timeout of socket connections. Default 3 seconds
hibernate.memcached.maintSleep 30000 Maintenance thread sleep time. Default 30 seconds
hibernate.memcached.compressEnable true If compression enabled
hibernate.memcached.defaultEncoding UTF-8 Default 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.
Property Default Description
hibernate.memcached.[region-name].cacheTimeSeconds none, see hibernate.memcached.cacheTimeSeconds Set the cache time for this cache region, overriding the cache-wide setting.
hibernate.memcached.[region-name].keyStrategy none, see hibernate.memcached.keyStrategy Overrides the strategy class to to use for generating cache keys in this cache region. Must provide a class name that implements KeyStrategy
hibernate.memcached.[region-name].clearSupported none, see hibernate.memcached.clearSupported Enables 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)
  
  -----------------------------------
  


1 楼 movingboy 2008-10-09   引用
还是要说明一下:
附件里的代码中,包com.googlecode.hibernate.memcached.dangamemcached中的代码是我加的,其它的是原项目中的代码。我已经发邮件给Ray Krueger,把我的代码捐赠给了这个项目,因为Ray还收到了另一份捐赠的Whalin补丁,现在还不知道他到底会采用谁的代码。不管怎样,希望这个项目越来越好
上面的配置中忘了提到一点:使用Whalin的客户端时,还必须把hibernate.memcached.memcacheClientFactory设为com.googlecode.hibernate.memcached.whalin.DangaMemcacheClientFactory  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-108231-1-1.html 上篇帖子: Memcached telnet端命令 下篇帖子: ASP.NET-Memcached基础
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表