项目中须要加缓存,故学习了java
一、spring cache、ehcache的使用及集成redis
二、缓存的命中率等统计数据spring
1、spring cache数据库
一、概述数组
Spring 3.1 引入了基于注解(annotation)的缓存(cache)技术缓存
二、须要的jar包jvm
三、使用方法socket
存分布式
@Cacheable(value=”缓存容器”,key=“键值”)学习
注意事项:
①key值中是SPLE表达式,例如“#id”(不能直接写字符串,须要SPLE表达式),参数组合是用“#id.concat("#name")”,使用concat有前提,就是拼接的两个参数都必须是字符串,不然会报表达式错误,若是两个非字符串须要拼接,参照#id“+“_”+“#name”
②须要用哪一个容器,就写哪一个容器的value值
更新
①清空对应
@Cacheable(value="***", key="#id")
②清空全部
@Cacheable(value="***", allEntries=true)
按照条件操做缓存
@Cacheable(value="***",condition="#userName.length() <= 4")
既要保证方法被调用,又但愿结果被缓存
@CachePut(value="accountCache",key="#account.getName()")// 更新 accountCache 缓存
XML配置
xmlns:p="http://www.springframework.org/schema/p" xmlns:cache="http://www.springframework.org/schema/cache" http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd <cache:annotation-driven /> <!-- generic cache manager --> <bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager"> <property name="caches"> <set> <bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean" p:name="default" /> <bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean" p:name="accountCache" /> </set> </property> </bean>
参考文档:https://www.ibm.com/developerworks/cn/opensource/os-cn-spring-cache/
2、EHcache
一、简介
二、缺点
对分布式支持不够好,通常与Redis一块使用
三、ehcache 和 redis 比较
ehcache直接在jvm虚拟机中缓存,速度快,效率高;可是缓存共享麻烦,集群分布式应用不方便
redis是经过socket访问到缓存服务,效率比ecache低,比数据库要快不少,
处理集群和分布式缓存方便,有成熟的方案。若是是单个应用或者对缓存访问要求很高的应用,用ehcache。若是是大型系统,存在缓存共享、分布式部署、缓存内容很大的,建议用redis。
ehcache也有缓存共享方案,不过是经过RMI或者Jgroup多播方式进行广播缓存通知更新,缓存共享复杂,维护不方便;简单的共享能够,可是涉及到缓存恢复,大数据缓存,则不合适。
四、使用方法
①导包
<dependency> <groupId>net.sf.ehcache</groupId> <artifactId>ehcache</artifactId> <version>2.10.2</version> </dependency>
②新建xml
<?xml version="1.0" encoding="UTF-8"?> <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"> <!-- 磁盘缓存位置 --> <diskStore path="java.io.tmpdir/ehcache"/> <!-- 默认缓存 --> <defaultCache maxEntriesLocalHeap="10000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120" maxEntriesLocalDisk="10000000" diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU"> <persistence strategy="localTempSwap"/> </defaultCache> <!-- helloworld缓存 --> <cache name="HelloWorldCache" maxElementsInMemory="1000" eternal="false" timeToIdleSeconds="5" timeToLiveSeconds="5" overflowToDisk="false" memoryStoreEvictionPolicy="LRU"/> </ehcache>
maxElementsInMemory :内存中容许存储的最大的元素个数,0表明无限个 clearOnFlush:内存数量最大时是否清除。 eternal :设置缓存中对象是否为永久的,若是是,超时设置将被忽略,对象从不过时。 根据存储数据的不一样,例如一些静态不变的数据如省市区等能够设置为永不过期 timeToIdleSeconds : 设置对象在失效前的容许闲置时间(单位:秒)。仅当eternal=false对象不是永久有效时使用, 可选属性,默认值是0,也就是可闲置时间无穷大。 timeToLiveSeconds :缓存数据的生存时间(TTL),也就是一个元素从构建到消亡的最大时间间隔值,这只能在元素不是永久驻留时有效, 若是该值是0就意味着元素能够停顿无穷长的时间。 overflowToDisk :内存不足时,是否启用磁盘缓存。默认false maxEntriesLocalDisk:当内存中对象数量达到maxElementsInMemory时,Ehcache将会对象写到磁盘中。 maxElementsOnDisk:硬盘最大缓存个数。 diskSpoolBufferSizeMB:这个参数设置DiskStore(磁盘缓存)的缓存区大小。默认是30MB。 每一个Cache都应该有本身的一个缓冲区。 diskPersistent:是否在VM重启时存储硬盘的缓存数据。默认值是false。 diskExpiryThreadIntervalSeconds:磁盘失效线程运行时间间隔,默认是120秒。 maxEntriesLocalHeap:是用来限制当前缓存在堆内存上所能保存的最大元素数量的 memoryStoreEvictionPolicy: 若是内存中数据超过内存限制,向磁盘缓存时的策略。默认值LRU,可选FIFO、LFU。
四、整合spring
<!-- 启用缓存注解开关 --> <cache:annotation-driven cache-manager="cacheManager"/> <bean id="cacheManagerFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"> <property name="configLocation" value="ehcache文件位置"/> <property name="shared" value="true"></property> </bean> <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager"> <property name="cacheManager" ref="cacheManagerFactory"/> </bean>
3、命中率
CacheManager manager = CacheManager.create(); String[] cacheNames = manager.getCacheNames(); net.sf.ehcache.Cache cache = manager.getCache(cacheName); long statisticsCache = cache.getStatistics().cacheHitCount(); long putCount = cache.getStatistics().cachePutAddedCount();