ehcache通过注解实现缓存



1.首先是在spring的配置文件的头部增加:
xmlns:cache="http://www.springframework.org/schema/cache"和
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache.xsd


2.在spring配置文件内容中增加配置:
<cache:annotation-driven cache-manager="cacheManager" />
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
<property name="cacheManager" ref="ehcache" />
</bean>


<bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation" value="classpath:ehcache.xml"/>
</bean>


3.增加ehcache的配置文件ehcache.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"
         name="eyasAppApi" maxBytesLocalHeap="32M">


    <diskStore path="java.io.tmpdir"/>


    <defaultCache maxBytesLocalHeap="24M" eternal="false"
                  timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true"
                  maxElementsOnDisk="100000" diskPersistent="false"
                  diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU"/>


    <cache name="bankNameCodes" maxBytesLocalHeap="8M" eternal="false"
           timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="false"
           maxElementsOnDisk="10000" diskPersistent="false"
           diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU"/>
</ehcache>


需要缓存的内容需要配置对应的名字:name="bankNameCodes"


4.然后在对应的service上面用注解进行配置,首先需要建一个接口(必须要实现接口的才能够):
public interface IBankNameCodeService {
    List<BankNameCode>  getBankNameCodeList();
}


实现类:
@Service
public class BankNameCodeServiceImpl implements IBankNameCodeService {
   


    /**
     * 获取银行名字和编码列表,并且缓存
     * @return List<BankNameCode>
     */
    @Override
    @Cacheable(value = "bankNameCodes")
    public List<BankNameCode> getBankNameCodeList() {
        
    }


}




配置好过后第一次和第二次请求情况完全不一样,第二次直接从缓存中获取。


ehcache通过注解实现缓存配置就是这样的,ehcache对于少量数据缓存,已经性能要求不高的还是比较适用的,但是当性能要求特别高的时候,不能够很好的满足性能要求,
会对程序进行升级,用redis来进行数据的缓存,下一篇会介绍redis的配置和使用。
1.首先是在spring的配置文件的头部增加:
xmlns:cache="http://www.springframework.org/schema/cache"和
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache.xsd


2.在spring配置文件内容中增加配置:
<cache:annotation-driven cache-manager="cacheManager" />
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
<property name="cacheManager" ref="ehcache" />
</bean>


<bean id="ehcache" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation" value="classpath:ehcache.xml"/>
</bean>


3.增加ehcache的配置文件ehcache.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"
         name="eyasAppApi" maxBytesLocalHeap="32M">


    <diskStore path="java.io.tmpdir"/>


    <defaultCache maxBytesLocalHeap="24M" eternal="false"
                  timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true"
                  maxElementsOnDisk="100000" diskPersistent="false"
                  diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU"/>


    <cache name="bankNameCodes" maxBytesLocalHeap="8M" eternal="false"
           timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="false"
           maxElementsOnDisk="10000" diskPersistent="false"
           diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU"/>
</ehcache>


需要缓存的内容需要配置对应的名字:name="bankNameCodes"


4.然后在对应的service上面用注解进行配置,首先需要建一个接口(必须要实现接口的才能够):
public interface IBankNameCodeService {
    List<BankNameCode>  getBankNameCodeList();
}


实现类:
@Service
public class BankNameCodeServiceImpl implements IBankNameCodeService {
   


    /**
     * 获取银行名字和编码列表,并且缓存
     * @return List<BankNameCode>
     */
    @Override
    @Cacheable(value = "bankNameCodes")
    public List<BankNameCode> getBankNameCodeList() {
        
    }


}




配置好过后第一次和第二次请求情况完全不一样,第二次直接从缓存中获取。


ehcache通过注解实现缓存配置就是这样的,ehcache对于少量数据缓存,已经性能要求不高的还是比较适用的,但是当性能要求特别高的时候,不能够很好的满足性能要求,
会对程序进行升级,用redis来进行数据的缓存,下一篇会介绍redis的配置和使用。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值