1. 一: 缓存原理:
  2. 二: orm 框架缓存介绍
  3. 三: 缓存框架

 

一: 缓存原理:

 缓存就是将默写资源或者数据会频繁会被使用到的数据或者资源存储在系统外,比如数据库、硬盘文件等,那么每次操作这些数据的时候都从数据库或者硬盘上去获取,速度会很慢,会造成性能问题。

     一个简单的解决方法就是:把这些数据缓存到内存里面,每次操作的时候,先到内存里面找,看有没有这些数据,如果有,那么就直接使用,如果没有那么就获取它,并设置到缓存中,下一次访问的时候就可以直接从内存中获取了。从而节省大量的时间,

    缓存是一种典型的空间换时间的方案。

   在Java中最常见的一种实现缓存的方式就是使用Map。

 

二: orm 框架缓存介绍

    

  hibernate 一二级缓存

  一级:是Session级别的缓存,它是第一级别缓存属于进程范围内的缓存,由Hibernate自行管理一般情况下无需进行干预.

  二级: 是SessionFactory级别的缓存,它是第二级别的缓存属于集群范围与进程范围的缓存.它可以自己进行配置与更改,而且可以动态加载与卸载

 

  mybatis 一二级缓存

一级缓存是基于 PerpetualCache(mybatis自带)的 HashMap 本地缓存,作用范围为session,所以当session commit或close后,缓存就会被清空 

   二级缓存默认也是基于 PerpetualCache,但是可以为其制定存储源,比如ehcache 

   一级缓存缓存的是SQL语句,而二级缓存缓存的是结果对象,看如下例子(mybatis的日志级别设为debug)

   

 配置文件

<settingname="cacheEnabled"value="true"/>
<!-- <cache readOnly="true" type="org.mybatis.caches.ehcache.LoggingEhcache"/>   -->
<cachetype="org.mybatis.caches.ehcache.EhcacheCache"/>

 

 


三: 缓存框架

  3.1 :Ehcache:    

   可注解式侵入Spring]

pom.xml

<dependency>
    <groupId>net.sf.ehcache</groupId>
    <artifactId>ehcache-core</artifactId>
    <version>2.6.9</version>
  </dependency>

spring-cache.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:cache="http://www.springframework.org/schema/cache"
 xsi:schemaLocation="http://www.springframework.org/schema/beans 
 http://www.springframework.org/schema/beans/spring-beans-4.1.xsd
 http://www.springframework.org/schema/cache 
    http://www.springframework.org/schema/cache/spring-cache-4.1.xsd">
 
 <cache:annotation-driven cache-manager="cacheManager" proxy-target-class="true" />
 
 <bean id="ehcacheManager"
  class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
  <property name="configLocation" value="classpath:cache/ehcache-local.xml" />
 </bean>
 <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
  <property name="cacheManager" ref="ehcacheManager" />
  <property name="transactionAware" value="true" /> 
 </bean>
</beans>



@CacheConfig(cacheNames = "sysCountry_cache")      //指定全局Cache配置
@Cacheable(key="'allCountry'")    //对其结果进行缓存 可指定key
@CacheEvict(allEntries = true)   //根据一定的条件对缓存进行清空
@CacheConfig(cacheNames = "sysCountry_cache")      //指定全局Cache配置
@Cacheable(key="'allCountry'")    //对其结果进行缓存 可指定key
@CacheEvict(allEntries = true)   //根据一定的条件对缓存进行清空


   缓存数据有两级:内存和磁盘,因此无需担心容量问题 

   可以通过RMI、可插入API等方式进行分布式缓存


<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:noNamespaceSchemaLocation="ehcache.xsd" updateCheck="false"
 monitoring="autodetect">
 <!-- 
 <diskStore path="java.io.tmpdir" /> -->
 <!--  
    name:Cache的唯一标识  
    maxElementsInMemory:内存中最大缓存对象数  
    maxElementsOnDisk:磁盘中最大缓存对象数,若是0表示无穷大  
    eternal:Element是否永久有效,一但设置了,timeout将不起作用  
    overflowToDisk:配置此属性,当内存中Element数量达到maxElementsInMemory时,Ehcache将会Element写到磁盘中  
    timeToIdleSeconds:设置Element在失效前的允许闲置时间。仅当element不是永久有效时使用,可选属性,默认值是0,也就是可闲置时间无穷大  
    timeToLiveSeconds:设置Element在失效前允许存活时间。最大时间介于创建时间和失效时间之间。仅当element不是永久有效时使用,默认是0.,也就是element存活时间无穷大   
    diskPersistent:是否缓存虚拟机重启期数据  
    diskExpiryThreadIntervalSeconds:磁盘失效线程运行时间间隔,默认是120秒  
    diskSpoolBufferSizeMB:这个参数设置DiskStore(磁盘缓存)的缓存区大小。默认是30MB。每个Cache都应该有自己的一个缓冲区  
    memoryStoreEvictionPolicy:当达到maxElementsInMemory限制时,Ehcache将会根据指定的策略去清理内存。默认策略是LRU(最近最少使用)。你可以设置为FIFO(先进先出)或是LFU(较少使用)   
    -->  
 <diskStore path="d:/telpay_file/tt/cache" />
 <!-- <diskStore path="java.io.tmpdir/tt/ehcache/" />-->
 <defaultCache maxElementsInMemory="10000" eternal="false"
  timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true"
  maxElementsOnDisk="10000000" diskPersistent="false"
  diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU" />
 <!-- 开放平台缓存 -->
 <cache name="platformCache" maxElementsInMemory="10000"
  maxElementsOnDisk="1000" eternal="false" overflowToDisk="true"
  diskSpoolBufferSizeMB="10" timeToIdleSeconds="300" timeToLiveSeconds="300"
  memoryStoreEvictionPolicy="LFU" />
    
</ehcache>

 

 3.2  :redis 缓存