Use of caching techniques (level two cache of Hibernate or JPA, and caching at the page JSP level) __JS

Source: Internet
Author: User

Oscache: Designed by Opensymphony, it is a groundbreaking JSP custom tag application that provides the ability to implement fast memory buffering within existing JSP pages.

Business Description: The site for infrequently changed content can use the page of the local cloth cache, improve the speed of Web site access

One, global cache
1, introduce Jar Oscache-2.4.1.jar
2 Configuration Oscache.properties
3 Specifies the number of objects cached in memory cache.capacity=10000
4 Specify the cache expiration time in Web.xml. And which access paths are cached
<!--page cache, cached for 5 minutes-->
<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>
Second, the cloth cache
(1) Introduction of Jar Oscache-2.4.1.jar
(2) Reference cache label <% @taglib uri= "/web-inf/lib/oscache-2.4.1.jar" prefix= "OS"%>
(3) Put the need for the contents of the cache into the cache tag <os:cache time= "Ten" > <%=new Date ()%></os:cache>
You can also specify expiration time, refresh, scope
Parameter description:
Time= "10" Specifies the expiration time
Refresh= "False" is always refreshed
Scope= "Request" Cache scope request,page,session,application

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

Ehcache: is a pure Java in-process caching framework, with fast, lean and so on, is the hibernate default Cacheprovider.

1>. Configuring Echcache.xml Files

<?xml version= "1.0" encoding= "UTF-8"?>
<ehcache>
<defaultcache maxelementsinmemory= "10000" eternal= "false" timetoidleseconds= "1200" timetoliveseconds= "1200" Overflowtodisk= "false" >
</defaultCache>
<!--a separate cache policy setting for a entity
<cache name= "com.payment.entity.PromotionEntity" maxelementsinmemory= "100"
Eternal= "false"
timetoidleseconds= "1200" timetoliveseconds= "1200" overflowtodisk= "false"
Clearonflush= "true" >
</cache>-->
</ehcache>

2>: Open Level Two cache, modify Persistence.xml file (here for JPA, the hibernate level two cache has already been written blog, here is no longer written)

<?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= "/>"
<property name= "Hibernate.jdbc.batch_size" value= "ten"/>
<property name= "Hibernate.show_sql" value= "true"/>
<property name= "Hibernate.format_sql" value= "false"/>
<!--set up Tier II cache vendor-->
<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>. Specifies that the entity classes use level Two caching: 1, add annotations on the entity, 2, set the cache before querying, and then query.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.