在互联网应用中,应用并发比传统企业及应用会高出很多。解决并发的根本在于系统的响应时间与单位时间的吞吐量。思路可分为:一减少系统的不必要开支(如缓存),二是提高系统单位时间内的运算效率(如集群)。 在硬件资源一定的情况下,在软件层面上解决高并发会比较经济实惠一些。缓存又分为客户端缓存(web浏览器)与服务器缓存;常用的比较流行的服务器缓存框架如Ehcache。下面针对最近学习的Ehcache缓存做一下介绍。

一、ehcache需要引入包

<!--ehcache 相关包 -->
<dependency>
    <groupId>net.sf.ehcache</groupId>
        <artifactId>ehcache</artifactId>
    <version>2.7.5</version>
</dependency>
<dependency>
    <groupId>com.googlecode.ehcache-spring-annotations</groupId>
    <artifactId>ehcache-spring-annotations</artifactId>
    <version>1.2.0</version>
</dependency>

 

二、配置applicationContext-ehcache.xml和ehcache.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" xmlns:context="http://www.springframework.org/schema/context" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache.xsd">
    
    <bean id="cacheManagerFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
        <property name="configLocation" value="classpath:ehcache.xml"/>
    </bean>
    <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
        <property name="cacheManager" ref="cacheManagerFactory"/>
    </bean>
    
    <!-- 开启spring缓存 -->
    <cache:annotation-driven cache-manager="cacheManager" />

</beans>
ehcache配置文件

相关文章: