要想使用 java 的本地缓存,可以考虑用 ehcache,或者 guava。
guava 更高端一点,可以自动定时刷新。我选择了 ehcache。
在 spring 中是集成了 ehcache 的。要使用 ehcache 的话,只需要下面几步:
当然需要首先引入 ehcache 相关的 jar 包。可以采用配置 pom 文件使用 maven 依赖的方式。
一、在 spring 的 applicationContext.xml 配置文件中配置好 ehcache 相关的 bean
<!-- ehcache --> <bean id="cacheManagerFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"> <property name="configLocation" value="spring/ehcache.xml"/> </bean> <bean id="manager" class="org.springframework.cache.ehcache.EhCacheCacheManager"> <property name="cacheManager" ref="cacheManagerFactory"/> </bean>
二、配置好 ehcache.xml
<?xml version="1.0" encoding="gbk"?> <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="ehcache.xsd"> <diskStore path="java.io.tmpdir"/> <defaultCache maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="30" timeToLiveSeconds="30" overflowToDisk="false"/> <!-- 配置自定义缓存 maxElementsInMemory:缓存中允许创建的最大对象数 eternal:缓存中对象是否为永久的,如果是,超时设置将被忽略,对象从不过期。 timeToIdleSeconds:缓存数据的钝化时间,也就是在一个元素消亡之前, 两次访问时间的最大时间间隔值,这只能在元素不是永久驻留时有效, 如果该值是 0 就意味着元素可以停顿无穷长的时间。 timeToLiveSeconds:缓存数据的生存时间,也就是一个元素从构建到消亡的最大时间间隔值, 这只能在元素不是永久驻留时有效,如果该值是0就意味着元素可以停顿无穷长的时间。 overflowToDisk:内存不足时,是否启用磁盘缓存。 memoryStoreEvictionPolicy:缓存满了之后的淘汰算法。 --> <cache name="TerritoryCache" maxElementsInMemory="10000" eternal="false" overflowToDisk="false" timeToIdleSeconds="900" timeToLiveSeconds="1800" memoryStoreEvictionPolicy="LFU" /> </ehcache>
三、具体类中使用 CacheManager
@Component public class TerritoryRangeCache { @Autowired EhCacheCacheManager manager; @Autowired TerritoryRangeDao territoryRangeDao; Cache cache; public Object getAllTerritorRanges(String key) {
cache = manager.getCacheManager().getCache("TerritoryCache");
Object value = cache.get(key); if (value == null) { cache.putIfAbsent(new Element(key, territoryRangeDao.selectAll())); return cache.get(key).getObjectValue(); } return cache.get(key).getObjectKey(); } }
其实具体类中使用注入的 EhCacheCacheManager 是 spring 自己封装过了的 CacheManager。要想获取引入的 ehCache 包里面的 CacheManager 的话,需要把spring 包装过的EhCacheCacheManager通过 getManager 拿出来。我们来看先EhCacheCacheManager的源码:
public class EhCacheCacheManager extends AbstractTransactionSupportingCacheManager { private net.sf.ehcache.CacheManager cacheManager; /** * Create a new EhCacheCacheManager, setting the target EhCache CacheManager * through the {@link #setCacheManager} bean property. */ public EhCacheCacheManager() { } /** * Create a new EhCacheCacheManager for the given backing EhCache CacheManager. * @param cacheManager the backing EhCache {@link net.sf.ehcache.CacheManager} */ public EhCacheCacheManager(net.sf.ehcache.CacheManager cacheManager) { this.cacheManager = cacheManager; } /** * Set the backing EhCache {@link net.sf.ehcache.CacheManager}. */ public void setCacheManager(net.sf.ehcache.CacheManager cacheManager) { this.cacheManager = cacheManager; } /** * Return the backing EhCache {@link net.sf.ehcache.CacheManager}. */ public net.sf.ehcache.CacheManager getCacheManager() { return this.cacheManager; } …… }
四、ehcaceh 的定时刷新,可以自己写一个方法,来更新缓存中的数据:
@Scheduled(cron = " ") @PostConstruct public void refreshAll() { cache = manager.getCacheManager().getCache("key"); getAllTerritorRanges("territory_range"); }
可以加上上面的方法,来通过 cron 表达式中传入的参数来定时刷新缓存中的数据。
关于@PostConstruct的使用以及含义,会在我的另一篇博文中介绍~。可以先看下这个注解的源码:
package javax.annotation; import java.lang.annotation.*; import static java.lang.annotation.ElementType.*; import static java.lang.annotation.RetentionPolicy.*; /** * The PostConstruct annotation is used on a method that needs to be executed * after dependency injection is done to perform any initialization. This * method MUST be invoked before the class is put into service. This * annotation MUST be supported on all classes that support dependency * injection. The method annotated with PostConstruct MUST be invoked even * if the class does not request any resources to be injected. Only one * method can be annotated with this annotation. The method on which the * PostConstruct annotation is applied MUST fulfill all of the following * criteria - - The method MUST NOT have any parameters except in the case of EJB * interceptors in which case it takes an InvocationC ontext object as * defined by the EJB specification. * - The return type of the method MUST be void. * - The method MUST NOT throw a checked exception. * - The method on which PostConstruct is applied MAY be public, protected, * package private or private. * - The method MUST NOT be static except for the application client. * - The method MAY be final. * - If the method throws an unchecked exception the class MUST NOT be put into * service except in the case of EJBs where the EJB can handle exceptions and * even recover from them. * @since Common Annotations 1.0 * @see javax.annotation.PreDestroy * @see javax.annotation.Resource */ @Documented @Retention (RUNTIME) @Target(METHOD) public @interface PostConstruct { }
时间: 2024-10-04 13:05:08