设为首页 收藏本站
查看: 1297|回复: 0

[经验分享] memcached分布式缓存和hibernate结合-- Hibernate+ehcache二级缓存技术

[复制链接]

尚未签到

发表于 2015-11-18 13:07:48 | 显示全部楼层 |阅读模式
Memcached是由Danga Interactive开发的,高性能的,分布式的内存对象缓存系统,用于在动态应用中减少数据库负载,提升访问速度。Memcached 的缓存是一种分布式的,可以让不同主机上的多个用户同时访问, 因此解决了共享内存只能单机应用的局限,更不会出现使用数据库做类似事情的时候,磁盘开销和阻塞的发生。
memcached的使用
Memcached服务器端的安装 (此处将其作为系统服务安装)
  下载文件:memcached 1.2.1 for Win32 binaries (Dec 23,2006)
   1 解压缩文件到c:\memcached
   2 命令行输入 'c:\memcached\memcached.exe -d install'
   3 命令行输入 'c:\memcached\memcached.exe -d start' ,该命令启动 Memcached ,默认监听端口为 11211
  通过 memcached.exe -h 可以查看其帮助

问题
  如果没有下载服务器端(如果memcached的使用)开启memcached服务的话,那么将出现一系列的错误,最主要是报连接的问。所以大家在使用memcached的时候一定要安装服务器端。
memcached和Hibernate的结合
现在Hibernate实现了memcached的无缝连接,已经说过了,hibernate暴力的地方是他的缓存,hibernate结合memcached也是相当方便,只需配置,启动服务器没有错误算配置OK啦。去在网上看了一大堆关于hibernate和memcached结合的文章,文章内容基本上都是一致的,郁闷死,参考价值就少了很多。本人公司正使用memcached作为Hibernate的二级缓存,在这里也整理一下相关的内容。
  hibernate的缓存机制是其成为主流持久层霸主地位的重要组成部分,他的缓存机制是很黄很暴力的。二级缓存是SessionFactory级别的全局缓存,可以通过配置文件的hibernate.cache.provider_class 进行指定。在上次的日志上有提到相关的配置,在这里我再次贴一次供需要的人了解。
hibernate.cfg.xml
代码
<?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?>
  <!DOCTYPE hibernate-configuration PUBLIC &quot;-//Hibernate/Hibernate Configuration DTD 3.0//EN&quot;
                                          &quot;http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd&quot;>
  <hibernate-configuration>
  <session-factory name=&quot;default&quot;>
   <property name=&quot;hibernate.show_sql&quot;>true</property>
   <property name=&quot;hibernate.format_sql&quot;>true</property>
   <property name=&quot;hibernate.hbm2ddl.auto&quot;>update</property>
   <!-- <property name=&quot;hibernate.dialect&quot;>org.hibernate.dialect.MySQLDialect</property> -->
   <property name=&quot;hibernate.dialect&quot;>org.hibernate.dialect.Oracle10gDialect</property>
   <property name=&quot;hibernate.jdbc.fetch_size&quot;>50</property>
   <property name=&quot;hibernate.jdbc.batch_size&quot;>0</property>
   <property name=&quot;hibernate.default_schema&quot;>user</property>
   
      <!-- hibernate-memcache配置-->
      <!-- 开启二级缓存-->  
     <property name=&quot;hibernate.cache.use_second_level_cache&quot;>  
         true   
     </property>  
     <!-- 设置缓存类-->  
     <property name=&quot;hibernate.cache.provider_class&quot;>com.googlecode.hibernate.memcached.MemcachedCacheProvider</property>  
     <!-- 设置memcache缓存服务器端口 -->  
     <property name=&quot;hibernate.memcached.servers&quot;>localhost:11211</property>  
     <!-- 设置二级缓存的前缀名称 -->  
     <property name=&quot;hibernate.cache.region_prefix&quot;>quality.cache.ehcache</property>  
     <!-- 否使用结构化的方式缓存对象  -->  
     <property name=&quot;hibernate.cache.use_structured_entries&quot;>true</property>  
     <!-- 是否缓存查询结果 -->  
     <property name=&quot;hibernate.cache.use_query_cache&quot;>true</property>  

   <mapping resource=&quot;com/weiruan/projs/mydc/pojos/Usert.hbm.xml&quot;/>
   <mapping resource=&quot;com/weiruan/projs/mydc/pojos/Developer.hbm.xml&quot;/>
  </session-factory>
  </hibernate-configuration>



依赖的包:hibernate-memcache.jar,memcached-2.3.1.jar、spy2.4、slf4j-1.5.0.jar、slf4j-log4j12-1.5.0.jar还有一个hibernate-memcached[版本].jar,下载地址:http://code.google.com/p/hibernate-memcached/downloads/list。






  1、首先设置EhCache,建立配置文件ehcache.xml,默认的位置在class-path,可以放到你的src目录下:
  <?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?>
<ehcache>
 <diskStore path=&quot;java.io.tmpdir&quot;/>
  <defaultCache
   maxElementsInMemory=&quot;10000&quot; <!-- 缓存最大数目 -->
   eternal=&quot;false&quot; <!-- 缓存是否持久 -->
   overflowToDisk=&quot;true&quot; <!-- 是否保存到磁盘,当系统当机时-->
   timeToIdleSeconds=&quot;300&quot; <!-- 当缓存闲置n秒后销毁 -->
   timeToLiveSeconds=&quot;180&quot; <!-- 当缓存存活n秒后销毁-->
   diskPersistent=&quot;false&quot;
   diskExpiryThreadIntervalSeconds= &quot;120&quot;/>
</ehcache>


  2、在Hibernate配置文件中设置:

  <!-- 设置Hibernate的缓存接口类,这个类在Hibernate包中 -->
<property name=&quot;cache.provider_class&quot;>org.hibernate.cache.EhCacheProvider</property>
 <!-- 是否使用查询缓存 -->
 <property name=&quot;hibernate.cache.use_query_cache&quot;>true</property>
  如果使用spring调用Hibernate的sessionFactory的话,这样设置:
  <!--HibernateSession工厂管理 -->
   <bean id=&quot;sessionFactory&quot; class=&quot;org.springframework.orm.hibernate3.LocalSessionFactoryBean&quot;>
   <property name=&quot;dataSource&quot;>
    <ref bean=&quot;datasource&quot; />
   </property>
   <property name=&quot;hibernateProperties&quot;>
   <props>
    <prop key=&quot;hibernate.dialect&quot;>org.hibernate.dialect.Oracle9Dialect</prop>
    <prop key=&quot;connection.provider_class&quot;>org.hibernate.connection.C3P0ConnectionProvider</prop>
    <prop key=&quot;hibernate.show_sql&quot;>true</prop>
    <prop key=&quot;hibernate.cache.use_query_cache&quot;>true</prop>
    <prop key=&quot;hibernate.cache.provider_class&quot;>org.hibernate.cache.EhCacheProvider</prop>
   </props>
 </property>
 <property name=&quot;mappingDirectoryLocations&quot;>
  <list>
   <value>/WEB-INF/classes/cn/rmic/manager/hibernate/</value>
  </list>
 </property>
</bean>


  说明一下:如果不设置“查询缓存”,那么hibernate只会缓存使用load()方法获得的单个持久化对象,如果想缓存使用findall()、list()、Iterator()、createCriteria()、createQuery()等方法获得的数据结果集的话,就需要设置
hibernate.cache.use_query_cache true 才行

3、在Hbm文件中添加<cache usage=&quot;read-only&quot;/>

4、如果需要“查询缓存”,还需要在使用Query或Criteria()时设置其setCacheable(true);属性

5、实践出真知,给一段测试程序,如果成功的话第二次查询时不会读取数据库

  package cn.rmic.hibernatesample;
import java.util.List;


import org.hibernate.CacheMode;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.Session;


import cn.rmic.hibernatesample.hibernate.HibernateSessionFactory;
import cn.rmic.manager.po.Resources;


public class testCacheSelectList ...{


 /** *//**
 * @param args
 */
 public static void main(String[] args) ...{
  // TODO Auto-generated method stub


  Session s=HibernateSessionFactory.getSession();
  Criteria c=s.createCriteria(Resources.class);
  c.setCacheable(true);
  List l=c.list();
  // Query q=s.createQuery(&quot;From Resources r&quot;)
  // .setCacheable(true)
  // .setCacheRegion(&quot;frontpages&quot;) ;
  // List l=q.list();
  Resources resources=(Resources)l.get(0);
  System.out.println(&quot;-1-&quot;&#43;resources.getName());
  HibernateSessionFactory.closeSession();
  try ...{
   Thread.sleep(5000);
  } catch (InterruptedException e) ...{
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  s=HibernateSessionFactory.getSession();
  c=s.createCriteria(Resources.class);
  c.setCacheable(true);
  l=c.list();
  // q=s.createQuery(&quot;From Resources r&quot;).setCacheable(true)
  // .setCacheRegion(&quot;frontpages&quot;);
  // l=q.list();
  resources=(Resources)l.get(0);
  System.out.println(&quot;-2-&quot;&#43;resources.getName());
  HibernateSessionFactory.closeSession();
 }

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-140727-1-1.html 上篇帖子: 初见memcached 下篇帖子: 通过log4j关闭memcached的日志
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表