今天在网上看到一个用Memcached作为Hibernate二级分布式缓存,感觉挺有兴趣,就是尝试用了,感觉还不错,就推荐给大家看一下。

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

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

配置方法如下:

配置Hibernate使用cache提供类

hibernate.cache.provider_class com.googlecode.hibernate.memcached.MemcachedCacheProvider

设置查询缓存开启

hibernate.cache.use_query_cache true

其它一些参数设置说明:

Property Default Description
hibernate.memcached.servers localhost:11211 memcached 服务地址,多个用空格分隔
格式host:port 
hibernate.memcached.cacheTimeSeconds 300 缓存失效时间,单位秒
hibernate.memcached.keyStrategy HashCodeKeyStrategy 缓存Key生成存储HashCode算法
hibernate.memcached.readBufferSize DefaultConnectionFactory.DEFAULT_READ_BUFFER_SIZE 从服务器读取数据缓存区大小
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 操作超时时间设置
hibernate.memcached.hashAlgorithm HashAlgorithm.KETAMA_HASH 新增缓存数据到服务器时使用的Hash散列算法。 当 hibernate-memcached 设置成 KETAMA_HASH算法时,注意:默认客户端API使用的是 HashAlgorithm.NATIVE_HASH
hibernate.memcached.clearSupported false 支持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,现在配置已经完成。

最新文章

  1. MVC架构学习之Smarty学习——病来而蔫
  2. linux磁盘读写性能优化
  3. xmind的第十一天笔记
  4. [转]关于jquery中html()、text()、val()的区别
  5. 用 perl 统计 fasta 文件序列的总长
  6. WIN7系统下U盘安装Ubuntu双系统
  7. 《Linux内核设计与实现》读书笔记(十五)- 进程地址空间(kernel 2.6.32.60)
  8. .Net Core 学习 (1) - ASP.NET Core 总览
  9. android 36 线程通信
  10. 走FILTER效率高的2种情况
  11. c++命名空间using
  12. SquirrelMQ消息队列
  13. jQuery基础---Ajax基础教程(二)
  14. iOS_文章3党库SDWebImage
  15. [译]Java垃圾回收器的类型
  16. JEECG DataGridColumn dictionary使用问题
  17. 51Nod1336 RMQ逆问题 其他
  18. 每天一个linux命令(13):less命令
  19. 寒假集训——搜索 B - Sudoku
  20. [转帖]git、github、gitlab之间的关系

热门文章

  1. JavaScript--Array 数组对象
  2. Sqoop 是什么?(二)
  3. 数字签名与数字证书以及https
  4. PHP MySQL 连接数据库,进行增、删、改、查、操作
  5. JS高级——变量提升
  6. python 将中文转拼音后填充到url做参数并写入excel
  7. 抓包工具Fiddler及Charles
  8. MHA的MySQL高可用方案实战
  9. snprintf不能使用&quot;字符串指针&quot;赋值,可以使用字符数组
  10. SGU100