Spring Cache 缓存的使用

一句话说缓存

缓存是一个能有效提升性能和节省资源的东西。

Spring Cache 在Spring-context包下,所以引入了Spring Framework就可以使用Spring cache了,在maven项目中引入spring-context

<!-- https://mvnrepository.com/artifact/org.springframework/spring-context -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-context</artifactId>
    <version>4.3.7.RELEASE</version>
</dependency>

web.xml中引入SpringFramework

    <servlet>
        <servlet-name>appServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/app.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>appServlet</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>

spring配置文件cache配置项

<?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:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
   http://www.springframework.org/schema/beans/spring-beans.xsd 
     http://www.springframework.org/schema/cache 
     http://www.springframework.org/schema/cache/spring-cache.xsd">

    <cache:annotation-driven />

    <bean id="cacheService" class="com.lagersoft.cache.CacheService" />
    <!-- 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>
</beans>

写一个模拟数据库操作的 Service CacheService.java

public class CacheService {
    @Cacheable(value = "accountCache") // 使用了一个缓存名叫 accountCache
    public Bean getAccountByName(String userName) {
        // 方法内部实现不考虑缓存逻辑,直接实现业务
        System.out.println("real query account." + userName);
        return getFromDB(userName);
    }

    private Bean getFromDB(String acctName) {
        System.out.println("real querying db..." + acctName);
        return new Bean(acctName);
    }
}

class Bean{
    private String beanName;

    public void setBeanName(String beanName) {
        this.beanName = beanName;
    }
    public Bean(String name){
        this.beanName = name;
    }
    public String getBeanName() {
        return beanName;
    }

}

测试

public class Test {

    public static void main(String[] args) {
        ApplicationContext context = new ClassPathXmlApplicationContext("classpath:app.xml");// 加载spring配置文件
        CacheService s = (CacheService) context.getBean("cacheService");
        // 第一次查询,应该走数据库
        Bean bean1 = s.getAccountByName("a");
        System.out.println("hashCode1:" + bean1.hashCode());
        // 第二次查询,应该不查数据库,直接返回缓存的值
        Bean bean2 = s.getAccountByName("a");
        System.out.println("hashCode2:" + bean2.hashCode());
        // 查没有查询过的,应该走数据库
        Bean bean3 = s.getAccountByName("c");
        System.out.println("hashCode3:" + bean3.hashCode());
        // 查询已经查询过的,应该返回缓存的值
        Bean bean4 = s.getAccountByName("a");
        System.out.println("hashCode4:" + bean4.hashCode());
    }

}

输出

real query account.a
real querying db...a
hashCode1:1279309678
hashCode2:1279309678
real query account.c
real querying db...c
hashCode3:107456312
hashCode4:1279309678

可以看出 hashCode1、 hashCode2 和 hashCode4 的值是一样的,也就是说这里查过的数据spring帮我们缓存住了,不必再去数据库里面取数据

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值