缓存技术的使用(hibernate或jpa的二级缓存,以及页面jsp级别的缓存)

oscache由OpenSymphony设计,它是一种开创性的JSP定制标记应用,提供了在现有JSP页面之内实现快速内存缓冲的功能。

业务描述:网站对于不经常变动的内容可以用页面的局布缓存,提高网站访问速度

一、全局缓存
1,引入JAR  oscache-2.4.1.jar
2配置oscache.properties
3 指定内存中缓存的对象个数cache.capacity=10000
4在web.xml中指定缓存过期时间。以及对哪些访问路径进行缓存
<!-- 页面缓存 ,缓存5分钟 --> 
     <filter>
  <filter-name>CacheFilter</filter-name> 
  <filter-class>com.opensymphony.oscache.web.filter.CacheFilter</filter-class> 
 <init-param>
  <param-name>time</param-name> 
  <param-value>300</param-value> 
  </init-param>
 <init-param>
  <param-name>scope</param-name> 
  <param-value>application</param-value> 
  </init-param>
  </filter>
  <filter-mapping>
  <filter-name>CacheFilter</filter-name> 
  <url-pattern>*.action</url-pattern> 
  </filter-mapping>
二、对局布缓存
(1)引入JAR  oscache-2.4.1.jar
(2)引用缓存标签<%@taglib uri="/WEB-INF/lib/oscache-2.4.1.jar" prefix="os" %>
(3)把需要局布缓存的内容放进缓存标签里面<os:cache time="10" > <%=new Date() %></os:cache>
同时可以指定过期时间,是否刷新,作用范围
参数说明:
time="10"指定过期时间
 refresh="false"是否时时刷新
 scope="request" 缓存范围 request,page,session,application

======================================

ehcache是一个纯Java的进程内缓存框架,具有快速、精干等特点,是Hibernate中默认的CacheProvider。

1>. 配置echcache.xml文件

<?xml version="1.0" encoding="UTF-8"?>
      <ehcache>
      <defaultCache maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="1200" timeToLiveSeconds="1200" overflowToDisk="false" >
      </defaultCache>
       <!-- 单独对某个entity的缓存策略设置
      <cache name="com.payment.entity.PromotionEntity" maxElementsInMemory="100" 
           eternal="false"
           timeToIdleSeconds="1200" timeToLiveSeconds="1200" overflowToDisk="false"
           clearOnFlush="true">
      </cache>-->
     </ehcache>

2>.开启二级缓存,修改persistence.xml文件(这里针对jpa,前面已经对hibernate二级缓存写了博客,在这就不再写了)

<?xml version="1.0"?>
<persistence xmlns="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="education" transaction-type="RESOURCE_LOCAL">
  <provider>org.hibernate.ejb.HibernatePersistence</provider>
<properties>
         <property name="hibernate.dialect" value="org.hibernate.dialect.MySQL5Dialect"/>
         <property name="hibernate.max_fetch_depth" value="3"/>
         <property name="hibernate.hbm2ddl.auto" value="update"/>
    <property name="hibernate.jdbc.fetch_size" value="18"/>
    <property name="hibernate.jdbc.batch_size" value="10"/>
    <property name="hibernate.show_sql" value="true"/>
    <property name="hibernate.format_sql" value="false"/> 
     <!-- 设置II级缓存供应商 -->
    <property name="hibernate.cache.provider_class" value="org.hibernate.cache.EhCacheProvider" />  
       <property name="hibernate.cache.provider_configuration" value="/ehcache.xml" />      
    <property name="hibernate.cache.use_query_cache" value="true"/>
    <property name="hibernate.cache.use_second_level_cache" value="true"/>
    <property name="hibernate.cache.use_structured_entries" value="true"/>
      </properties>
  </persistence-unit>
</persistence>

3>.指定那些实体类使用二级缓存:1、在实体上加注解;2、在查询时先设置缓存,再查询。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值