MyBatis缓存的简单使用
来源:互联网 发布:颓然乎其间者的互 编辑:程序博客网 时间:2024/06/05 14:59
业务场景:在调用开发的返回数据量有15万条(每条的字段大有三十个)左右某一接口时,发生接口响应时间很长的现象,于是尝试用MyBatis缓存机制来解决并最终效果不错。
下面是业务代码:
1、ehcache.xsd:
<?xml version="1.0" encoding="UTF-8"?><xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" version="1.6"> <xs:element name="ehcache" > <xs:complexType> <xs:sequence> <xs:element minOccurs="0" maxOccurs="1" ref="diskStore"/> <xs:element minOccurs="0" maxOccurs="1" ref="cacheManagerEventListenerFactory"/> <xs:element minOccurs="0" maxOccurs="unbounded" ref="cacheManagerPeerProviderFactory"/> <xs:element minOccurs="0" maxOccurs="unbounded" ref="cacheManagerPeerListenerFactory"/> <xs:element ref="defaultCache"/> <xs:element minOccurs="0" maxOccurs="unbounded" ref="cache"/> </xs:sequence> </xs:complexType> </xs:element> <xs:element name="diskStore"> <xs:complexType> <xs:attribute name="path" use="optional" /> </xs:complexType> </xs:element> <xs:element name="cacheManagerEventListenerFactory"> <xs:complexType> <xs:attribute name="class" use="required"/> <xs:attribute name="properties" use="optional"/> <xs:attribute name="propertySeparator" use="optional"/> </xs:complexType> </xs:element> <xs:element name="cacheManagerPeerProviderFactory"> <xs:complexType> <xs:attribute name="class" use="required"/> <xs:attribute name="properties" use="optional"/> <xs:attribute name="propertySeparator" use="optional"/> </xs:complexType> </xs:element> <xs:element name="cacheManagerPeerListenerFactory"> <xs:complexType> <xs:attribute name="class" use="required"/> <xs:attribute name="properties" use="optional"/> <xs:attribute name="propertySeparator" use="optional"/> </xs:complexType> </xs:element> <!-- add clone support for addition of cacheExceptionHandler. Important! --> <xs:element name="defaultCache"> <xs:complexType> <xs:sequence> <xs:element minOccurs="0" maxOccurs="unbounded" ref="cacheEventListenerFactory"/> <xs:element minOccurs="0" maxOccurs="unbounded" ref="cacheExtensionFactory"/> <xs:element minOccurs="0" maxOccurs="unbounded" ref="cacheLoaderFactory"/> <xs:element minOccurs="0" maxOccurs="1" ref="bootstrapCacheLoaderFactory"/> <xs:element minOccurs="0" maxOccurs="1" ref="cacheExceptionHandlerFactory"/> </xs:sequence> <xs:attribute name="diskExpiryThreadIntervalSeconds" use="optional" type="xs:integer"/> <xs:attribute name="diskSpoolBufferSizeMB" use="optional" type="xs:integer"/> <xs:attribute name="diskPersistent" use="optional" type="xs:boolean"/> <xs:attribute name="eternal" use="required" type="xs:boolean"/> <xs:attribute name="maxElementsInMemory" use="required" type="xs:integer"/> <xs:attribute name="memoryStoreEvictionPolicy" use="optional" type="xs:string"/> <xs:attribute name="overflowToDisk" use="required" type="xs:boolean"/> <xs:attribute name="timeToIdleSeconds" use="optional" type="xs:integer"/> <xs:attribute name="timeToLiveSeconds" use="optional" type="xs:integer"/> <xs:attribute name="maxElementsOnDisk" use="optional" type="xs:integer"/> </xs:complexType> </xs:element> <xs:element name="cache"> <xs:complexType> <xs:sequence > <xs:element minOccurs="0" maxOccurs="unbounded" ref="cacheEventListenerFactory"/> <xs:element minOccurs="0" maxOccurs="unbounded" ref="cacheExtensionFactory"/> <xs:element minOccurs="0" maxOccurs="unbounded" ref="cacheLoaderFactory"/> <xs:element minOccurs="0" maxOccurs="1" ref="bootstrapCacheLoaderFactory"/> <xs:element minOccurs="0" maxOccurs="1" ref="cacheExceptionHandlerFactory"/> </xs:sequence> <xs:attribute name="diskExpiryThreadIntervalSeconds" use="optional" type="xs:integer"/> <xs:attribute name="diskSpoolBufferSizeMB" use="optional" type="xs:integer"/> <xs:attribute name="diskPersistent" use="optional" type="xs:boolean"/> <xs:attribute name="eternal" use="required" type="xs:boolean"/> <xs:attribute name="maxElementsInMemory" use="required" type="xs:integer"/> <xs:attribute name="memoryStoreEvictionPolicy" use="optional" type="xs:string"/> <xs:attribute name="name" use="required" type="xs:string"/> <xs:attribute name="overflowToDisk" use="required" type="xs:boolean"/> <xs:attribute name="timeToIdleSeconds" use="optional" type="xs:integer"/> <xs:attribute name="timeToLiveSeconds" use="optional" type="xs:integer"/> <xs:attribute name="maxElementsOnDisk" use="optional" type="xs:integer"/> </xs:complexType> </xs:element> <xs:element name="cacheEventListenerFactory"> <xs:complexType> <xs:attribute name="class" use="required"/> <xs:attribute name="properties" use="optional"/> <xs:attribute name="propertySeparator" use="optional"/> </xs:complexType> </xs:element> <xs:element name="bootstrapCacheLoaderFactory"> <xs:complexType> <xs:attribute name="class" use="required"/> <xs:attribute name="properties" use="optional"/> <xs:attribute name="propertySeparator" use="optional"/> </xs:complexType> </xs:element> <xs:element name="cacheExtensionFactory"> <xs:complexType> <xs:attribute name="class" use="required"/> <xs:attribute name="properties" use="optional"/> <xs:attribute name="propertySeparator" use="optional"/> </xs:complexType> </xs:element> <xs:element name="cacheExceptionHandlerFactory"> <xs:complexType> <xs:attribute name="class" use="required"/> <xs:attribute name="properties" use="optional"/> <xs:attribute name="propertySeparator" use="optional"/> </xs:complexType> </xs:element> <xs:element name="cacheLoaderFactory"> <xs:complexType> <xs:attribute name="class" use="required"/> <xs:attribute name="properties" use="optional"/> <xs:attribute name="propertySeparator" use="optional"/> </xs:complexType> </xs:element></xs:schema>
2、ehcache.xml:
<?xml version="1.0" encoding="UTF-8"?><ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="classpath:META-INF/spring/ehcache.xsd"> <diskStore path="java.io.tmpdir"/> <defaultCache maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="30" timeToLiveSeconds="30" overflowToDisk="true"/> </ehcache>
3、ehcache-application.xml:
<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.1.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd"> <!-- ehcache配置 --> <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"> <property name="configLocation" value="classpath:META-INF/spring/ehcache.xml" /> <property name="shared" value="true" /> </bean></beans>
3、***Mapper.xml文件使用:
<cache type="org.mybatis.caches.ehcache.LoggingEhcache"> <property name="timeToIdleSeconds" value="3600"/><!--1 hour --> <property name="timeToLiveSeconds" value="3600"/><!--1 hour --> <property name="maxEntriesLocalHeap" value="1000"/> <property name="maxEntriesLocalDisk" value="10000000"/> <property name="memoryStoreEvictionPolicy" value="LRU"/></cache><!-- 获取语句 --><select id="getDataList" resultMap="marginHistory" useCache="true">...</select>
阅读全文
0 0
- MyBatis缓存的简单使用
- mybatis缓存的使用
- Mybatis缓存的使用
- Mybatis学习笔记-MyBatis缓存的使用
- mybatis 的简单使用
- mybatis简单的使用
- mybatis的简单使用
- MyBatis的简单使用
- MyBatis的简单使用
- MyBatis一级缓存的简单剖析
- 对spring mybatis 缓存的使用
- mybatis缓存的使用及理解
- mybatis缓存的使用及理解
- mybatis缓存的使用及理解
- mybatis 3.x 缓存Cache的使用
- mybatis缓存的使用及理解
- mybatis缓存的使用及理解
- mybatis 3.x 缓存Cache的使用
- html meta
- 2018 IXDC国际体验设计大会7月北京召开!
- function的写法
- bzoj3580 冒泡排序 模拟
- html图片下方空隙,表格边框,chrom小字体无效
- MyBatis缓存的简单使用
- 百度地图demo
- java技术规范
- 管道命令和xargs的区别(经典解释)
- derby嵌入式
- hadoop集群搭建
- js_动态添加控件
- golang基础(基础包链表使用)
- 当月获取最后一天&&上个月日期