今天在网上看到一个用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,现在配置已经完成。

在Hibernate中使用Memcached作为一个二级分布式缓存的更多相关文章

  1. hibernate中HQL练习时候一个小小的错误导致语法异常

    package cn.db.po.test; import java.util.List; import cn.db.po.User; import cn.db.po.biz.UserBiz; pub ...

  2. SpringBoot,用200行代码完成一个一二级分布式缓存

    缓存系统的用来代替直接访问数据库,用来提升系统性能,减小数据库复杂.早期缓存跟系统在一个虚拟机里,这样内存访问,速度最快. 后来应用系统水平扩展,缓存作为一个独立系统存在,如redis,但是每次从缓存 ...

  3. ASP.NET Core与Redis搭建一个简易分布式缓存

    ​本文主要介绍了缓存的概念,以及如何在服务器内存中存储内容.今天的目标是利用IDistributedCache来做一些分布式缓存,这样我们就可以横向扩展我们的web应用程序. 在本教程中,我将使用Re ...

  4. win10配置Memcached及MVC5测试分布式缓存入门

    win10配置Memcached: 1.安装包下载 2.解压后有: 3.以管理员省份运行cmd: 4.安装:输入cmd命令: E:/memcached-amd64/memcached.exe -d  ...

  5. 攻城狮在路上(壹) Hibernate(二)--- 第一个hibernate程序

    1.直接通过JDBC API持久化实体域对象: A.java.sql常用接口和类: DriverManager:驱动程序管理器,负责创建数据库连接. Connection:代表数据库连接. State ...

  6. hibernate中的SessionFactory,Session分别表示什么啊?如何理解?

    Session接口         Session接口对于Hibernate   开发人员来说是一个最重要的接口.然而在Hibernate中,实例化的Session是一个轻量级的类,创建和销毁它都不会 ...

  7. 关于hibernate中的session与数据库连接关系以及getCurrentSession 与 openSession() 的区别

    1.session与connection,是多对一关系,每个session都有一个与之对应的connection,一个connection不同时刻可以供多个session使用.   2.多个sessi ...

  8. Hibernate中使用子查询

    子查询:   子查询是SQL语句中非常重要的功能特性,它可以在SQL语句中利用另外一条SQL语句的查询结果,在Hibernate中HQL查询同样对子查询功能提供了支持.   如下面代码所示: List ...

  9. 分布式缓存Memcached/memcached/memcache详解及区别

    先来解释下标题中的三种写法:首字母大写的Memcached,指的是Memcached服务器,就是独立运行Memcached的后台服务器,用于存储缓存数据的“容器”.memcached和memcache ...

随机推荐

  1. Mobile

    模块===包   传统开发:整个网页我们写了一个js文件,所有的特效都写在里面了. 缺点:耦合度太高,代码关联性太强,不便于后期维护,会造成全局污染. 发生的请求次数过多,依赖模糊,难于维护. 以上都 ...

  2. DB2锁表或超时解决方案

    DB2锁表或超时 一.场景 对数据表进行更新(查询没问题),错误提示如下: SQLCODE=-911, SQLSTATE=40001, DRIVER=3.63.75SQL0911N The curre ...

  3. jQueryTools-Scrollable.js

    转载一篇例子,学习使用: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http: ...

  4. 【洛谷2257/BZOJ2820】YY的GCD(数论/莫比乌斯函数)

    题目: 洛谷2257 预备知识:莫比乌斯定理(懵逼乌斯定理) \(\mu*1=\epsilon\)(证bu明hui略zheng) 其中(我校学长把\(\epsilon(x)\)叫单位函数但是为什么我没 ...

  5. Codeforces 612D 前缀和处理区间问题

    传送门:http://codeforces.com/problemset/problem/612/D (转载请注明出处谢谢) 题意: 给出数字n和k,n表示接下来将输入n个在x轴上的闭区间[li,ri ...

  6. 单例模式在多线程环境下的lazy模式为什么要加两个if(instance==null)

    刚才在看阿寻的博客”C#设计模式学习笔记-单例模式“时,发现了评论里有几个人在问单例模式在多线程环境下为什么lazy模式要加两个if进行判断,评论中的一个哥们剑过不留痕,给他们写了一个demo来告诉他 ...

  7. 如何修改wampserver中mysql中字符编码的解决方案

    因为我用的一般都是utf8,所以有必要改一下: 打开mysql控制台,输入密码登录之后,执行命令: show variables like ‘%char%’; 注意引号的中英文格式以及最后面的分号不要 ...

  8. 超经典~超全的jQuery插件大全

    海量的jQuery插件帖,很经典,不知道什么时候开始流传,很早以前就收藏过,为了工作方便还是发了一份放在日志里面. 其中有些已经无法访问,或许是文件移除,或许是被封锁.大家分享的东西,没什么特别的可说 ...

  9. 控制台——args参数的赋值方法

    args参数的赋值方法有好几种,主要介绍两种. 外部传参的方法:先找到bin目录下的exe文件,并创建快捷方法,在目标后面追加参数. 控制台主函数入口实现方法 static void Main(str ...

  10. Zabbix自带的mysql监控模块

    Zabbix自带的mysql监控模块 [root@Cagios zabbix-]# cp conf/zabbix_agentd/userparameter_mysql.conf /usr/local/ ...