在Hibernate中使用Memcached作为一个二级分布式缓存
今天在网上看到一个用Memcached作为Hibernate二级分布式缓存,感觉挺有兴趣,就是尝试用了,感觉还不错,就推荐给大家看一下。
官方网址: http://code.google.com/p/hibernate-memcached/
目前最新版本为1.0, 支持Hibernate3.3.
下面是具体的使用方法:
hibernate-memcached需要支持的类库如下:
- hibernate-memcached-<version>.jar
- memcached-2.1.jar
- spy-2.4.jar
- commons-codec 1.3
- slf4j-1.5.0.jar 如果你的项目使用的是Log4j,可以用slf4j-log4j12-1.5.0.jar
配置方法如下:
配置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文件
<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进行配置
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作为一个二级分布式缓存的更多相关文章
- hibernate中HQL练习时候一个小小的错误导致语法异常
package cn.db.po.test; import java.util.List; import cn.db.po.User; import cn.db.po.biz.UserBiz; pub ...
- SpringBoot,用200行代码完成一个一二级分布式缓存
缓存系统的用来代替直接访问数据库,用来提升系统性能,减小数据库复杂.早期缓存跟系统在一个虚拟机里,这样内存访问,速度最快. 后来应用系统水平扩展,缓存作为一个独立系统存在,如redis,但是每次从缓存 ...
- ASP.NET Core与Redis搭建一个简易分布式缓存
本文主要介绍了缓存的概念,以及如何在服务器内存中存储内容.今天的目标是利用IDistributedCache来做一些分布式缓存,这样我们就可以横向扩展我们的web应用程序. 在本教程中,我将使用Re ...
- win10配置Memcached及MVC5测试分布式缓存入门
win10配置Memcached: 1.安装包下载 2.解压后有: 3.以管理员省份运行cmd: 4.安装:输入cmd命令: E:/memcached-amd64/memcached.exe -d ...
- 攻城狮在路上(壹) Hibernate(二)--- 第一个hibernate程序
1.直接通过JDBC API持久化实体域对象: A.java.sql常用接口和类: DriverManager:驱动程序管理器,负责创建数据库连接. Connection:代表数据库连接. State ...
- hibernate中的SessionFactory,Session分别表示什么啊?如何理解?
Session接口 Session接口对于Hibernate 开发人员来说是一个最重要的接口.然而在Hibernate中,实例化的Session是一个轻量级的类,创建和销毁它都不会 ...
- 关于hibernate中的session与数据库连接关系以及getCurrentSession 与 openSession() 的区别
1.session与connection,是多对一关系,每个session都有一个与之对应的connection,一个connection不同时刻可以供多个session使用. 2.多个sessi ...
- Hibernate中使用子查询
子查询: 子查询是SQL语句中非常重要的功能特性,它可以在SQL语句中利用另外一条SQL语句的查询结果,在Hibernate中HQL查询同样对子查询功能提供了支持. 如下面代码所示: List ...
- 分布式缓存Memcached/memcached/memcache详解及区别
先来解释下标题中的三种写法:首字母大写的Memcached,指的是Memcached服务器,就是独立运行Memcached的后台服务器,用于存储缓存数据的“容器”.memcached和memcache ...
随机推荐
- Gym - 100920H 2010-2011 OpenCup IX Onsite, II Yandex Summer School H.Squares 暴力
题面 题意:有10w个点,问你选4个点,能组成平行于坐标轴的正方形有多少个 题解:不知道正解,我的做法就是暴力的基础上优化一点,每次按x排好序,每次枚举的2个点都是x相同的 这样算是个优化?但并不能过 ...
- Visual Studio Code配置GitHub(Win7环境)
一.软件环境说明(演示环境) 1.操作系统:Windows7旗舰版(64bit) 2.Visual Studio Code版本:1.32.3 3.Git版本:2.21.0.windows.1 二.软件 ...
- reactnative(2) - Navigator 使用案例
'use strict'; import React, { Component } from 'react'; import { AppRegistry, ScrollView, StyleSheet ...
- 二次封装OKHttp网络框架(1)
1. 框架功能简介:暂时只有get.post两个请求 2. 请求的主要流程和区别: 2.1 get请求: (1)创建请求客户的 OkHttpClient对象 (2)创建请求构建器 Request.Bu ...
- node 第三方包学习
时间格式化 moment var moment = require('moment'); moment().format();
- JS高级——闭包练习
从上篇文章我们知道与浏览器的交互操作如鼠标点击,都会被放入任务队列中,而放入到任务队列中是必须等到主线程的任务都执行完之后才能执行,故而我们有时利用for循环给dom注册事件时候,难以获取for循环中 ...
- JS高级——arguments
arguments 1.函数内部的一个对象,在函数调用的时候,默认的会将所有传入的实参依次存入该对象 2.是一个伪数组 3.arguments.length 可以用来表示传入实参的个数 4.argum ...
- 微信小程序php后台实现
这里简单介绍用php后台实现获取openid并保存到数据库: 微信的登陆流程是这样的 首先前端发送请求到服务器: wx.login({ success: function (res) { var co ...
- python函数参数的区别
在运用python的过程中,发现当函数参数为list的时候,在函数内部调用list.append()会改变形参,与C/C++的不太一样,查阅相关资料,在这里记录一下. python中id可以获取对象的 ...
- 如何知道自己的CPU支持SLAT
因为WP8 SDK发布,很多WP8的开发者们也开始陆续安装WP8的SDK的,然而安装WP8的SDK有很多软件和硬件的要求,其中有一个就是——要求CPU支持二级地址转换(SLAT),如果CPU不支持二级 ...