Hibernate-memcached是CacheProvider接口的memcached实现
步骤:
one:下载和安装运行memcache服务器程序 即:memcache.exe 下载到c:\
two:增加Hibernate-memcached的相关类库
包括:memcached-2.1.jar
spy-2.4.jar
commons-codec-1.3.jar
three:修改hibernate.cfg.xml文件
four:测试Hibernate-memcached
Hibernate.java
代码:
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" ""><!-- Generated by MyEclipse Hibernate Tools. -->
<hibernate-configuration><session-factory>
<property name="dialect"> org.hibernate.dialect.MySQLDialect </property> <property name="connection.url"> jdbc:mysql://localhost:3307/users </property> <property name="connection.username">root</property> <property name="connection.password">root</property> <property name="connection.driver_class"> com.mysql.jdbc.Driver </property> <property name="myeclipse.connection.profile">mysqlusers</property> <property name="show_sql">true</property> <property name="format_sql">true</property> <property name="current_session_context_class">thread</property> <property name="cache.provider_class"> com.googlecode.hibernate.memcached.MemcachedCacheProvider </property> <property name="ibernate.cache.use_query_cache">true</property> <mapping resource="com/b510/examplex/Category.hbm.xml" /> <mapping resource="com/b510/examplex/Product.hbm.xml" /></session-factory>
</hibernate-configuration>
HibernateTest.java
代码:
/**
* */package com.b510.examplex;import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;/**
* * @author XHW * * @date 2011-7-17 * */public class HibernateTest { public static void main(String[] args) { new HibernateTest().test(); } public void test(){ Session session=HibernateSessionFactoryUtil.getSessionFactory().openSession(); session.beginTransaction(); Query query=session.createQuery("from Product"); //查询 query.setCacheable(true); List<Product> list=query.list(); for(Product p:list){ System.out.println(p.getName()); } Query query2=session.createQuery("from Product"); List<Product> list2=query.list(); for(Product p2:list2){ System.out.println(p2.getName()); } session.getTransaction().commit(); }}运行结果:
log4j:WARN No appenders could be found for logger (org.hibernate.cfg.Environment).
log4j:WARN Please initialize the log4j system properly.31 [main] INFO com.googlecode.hibernate.memcached.MemcachedCacheProvider - Starting MemcachedClient...2011-07-17 22:15:34.015 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 queue2011-07-17 22:15:34.031 INFO net.spy.memcached.MemcachedConnection: Connection state changed for 140 [main] INFO com.googlecode.hibernate.memcached.MemcachedCacheProvider - Building cache for region [com.b510.examplex.Category]265 [main] INFO com.googlecode.hibernate.memcached.MemcachedCacheProvider - Building cache for region [com.b510.examplex.Category.products]Hibernate: select product0_.id as id1_, product0_.category_id as category2_1_, product0_.name as name1_, product0_.price as price1_, product0_.descripton as descripton1_ from users.product product0_java SE应用程序设计java WEB开发与实战Hibernate: select product0_.id as id1_, product0_.category_id as category2_1_, product0_.name as name1_, product0_.price as price1_, product0_.descripton as descripton1_ from users.product product0_java SE应用程序设计java WEB开发与实战注意:下面这步应该最先
运行-->cmd-->cd\ 回车-->memcached -vv
显示结果:
Microsoft Windows XP [版本 5.1.2600]
(C) 版权所有 1985-2001 Microsoft Corp.C:\Documents and Settings\Administrator>ed\
'ed\' 不是内部或外部命令,也不是可运行的程序或批处理文件。C:\Documents and Settings\Administrator>cd\
C:\>memcached -vv
slab class 1: chunk size 80 perslab 13107slab class 2: chunk size 100 perslab 10485slab class 3: chunk size 128 perslab 8192slab class 4: chunk size 160 perslab 6553slab class 5: chunk size 200 perslab 5242slab class 6: chunk size 252 perslab 4161slab class 7: chunk size 316 perslab 3318slab class 8: chunk size 396 perslab 2647slab class 9: chunk size 496 perslab 2114slab class 10: chunk size 620 perslab 1691slab class 11: chunk size 776 perslab 1351slab class 12: chunk size 972 perslab 1078slab class 13: chunk size 1216 perslab 862slab class 14: chunk size 1520 perslab 689slab class 15: chunk size 1900 perslab 551slab class 16: chunk size 2376 perslab 441slab class 17: chunk size 2972 perslab 352slab class 18: chunk size 3716 perslab 282slab class 19: chunk size 4648 perslab 225slab class 20: chunk size 5812 perslab 180slab class 21: chunk size 7268 perslab 144slab class 22: chunk size 9088 perslab 115slab class 23: chunk size 11360 perslab 92slab class 24: chunk size 14200 perslab 73slab class 25: chunk size 17752 perslab 59slab class 26: chunk size 22192 perslab 47slab class 27: chunk size 27740 perslab 37slab class 28: chunk size 34676 perslab 30slab class 29: chunk size 43348 perslab 24slab class 30: chunk size 54188 perslab 19slab class 31: chunk size 67736 perslab 15slab class 32: chunk size 84672 perslab 12slab class 33: chunk size 105840 perslab 9slab class 34: chunk size 132300 perslab 7slab class 35: chunk size 165376 perslab 6slab class 36: chunk size 206720 perslab 5slab class 37: chunk size 258400 perslab 4slab class 38: chunk size 323000 perslab 3slab class 39: chunk size 403752 perslab 2slab class 40: chunk size 504692 perslab 2<1944 server listening<1928 new client connection<1920 new client connection<1908 new client connection<1896 new client connection