Hibernate Memcached 配置

本文介绍如何将Memcached作为二级缓存集成到Hibernate中,包括所需依赖、配置步骤及示例代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

官方网址: http://code.google.com/p/hibernate-memcached/
目前最新版本为1.0, 支持Hibernate3.3.

下面是具体的使用方法:
hibernate-memcached需要支持的类库如下:


配置方法如下:

配置Hibernate使用cache提供类

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

 

设置查询缓存开启

hibernate.cache.use_query_cachetrue


其它一些参数设置说明:

Property Default Description
hibernate.memcached.serverslocalhost:11211memcached 服务地址,多个用空格分隔
格式host:port
hibernate.memcached.cacheTimeSeconds300缓存失效时间,单位秒
hibernate.memcached.keyStrategyHashCodeKeyStrategy缓存Key生成存储HashCode算法
hibernate.memcached.readBufferSizeDefaultConnectionFactory.DEFAULT_READ_BUFFER_SIZE从服务器读取数据缓存区大小
hibernate.memcached.operationQueueLengthDefaultConnectionFactory.DEFAULT_OP_QUEUE_LENMaximum length of the operation queue returned by this connection factory
hibernate.memcached.operationTimeoutDefaultConnectionFactory.DEFAULT_OPERATION_TIMEOUT操作超时时间设置
hibernate.memcached.hashAlgorithmHashAlgorithm.KETAMA_HASH新增缓存数据到服务器时使用的Hash散列算法。 当 hibernate-memcached 设置成 KETAMA_HASH算法时,注意:默认客户端API使用的是 HashAlgorithm.NATIVE_HASH
hibernate.memcached.clearSupportedfalse支持MemcachedCache.clear()方法清空缓存。
建议不要开启。


配置示例(本文以Hibernate3.3-entitymanager为例)
配置 persistence.xml文件

<? xml version="1.0" encoding="UTF-8" ?>
< persistence xmlns ="http://java.sun.com/xml/ns/persistence" " target ="_new" > http://java.sun.com/xml/ns/persistence" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0">

< persistence-unit name ="entityManager" transaction-type ="RESOURCE_LOCAL" >
< provider > org.hibernate.ejb.HibernatePersistence </ provider >
< jta-data-source > java:comp/env/jdbc/qualitydb </ jta-data-source >

< properties >
< property name ="hibernate.dialect" value ="org.hibernate.dialect.PostgreSQLDialect" />
< property name ="hibernate.max_fetch_depth" value ="3" />
< property name ="hibernate.show_sql" value ="true" />


< property name ="hibernate.cache.region_prefix" value ="quality.cache.ehcache" />
< property name ="hibernate.cache.use_second_level_cache" value ="true" />
< property name ="hibernate.cache.use_structured_entries" value ="true" />
< property name ="hibernate.cache.use_query_cache" value ="true" />
< property name ="hibernate.cache.provider_class" value ="com.googlecode.hibernate.memcached.MemcachedCacheProvider" />
< property name ="hibernate.memcached.servers" value ="localhost:11211" />

</ properties >

</ persistence-unit >

</ persistence >



启动后,提示如下:
2008-08-28 17:10:08,312 JCLLoggerAdapter.java265 INFO -- Starting MemcachedClient...
2008-08-28 17:10:08.718 INFO net.spy.memcached.MemcachedConnection: Added {QA sa=localhost/127.0.0.1:11211, #Rops=0, #Wops=0, #iq=0, topRop=null, topWop=null, toWrite=0, interested=0} to connect queue
2008-08-28 17:10:08.750 INFO net.spy.memcached.MemcachedConnection: Connection state changed for sun.nio.ch.SelectionKeyImpl@16e59da

表示我们第一步配置已经成功了,接下来,对需要进行缓存的Entity进行配置

1 @Entity
2 @Cache(usage = CacheConcurrencyStrategy.READ_WRITE) // 设置要求缓存
3 public class Student {
4
5    @Id
6    @Column(length = 32 )
7      private String id;
8     
9      @Column(length = 20 )
10      private string name;
11     
12      @OneToMany
13      @Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
14      private Set < Book > books;
15
16 }



Ok,现在配置已经完成。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值