Spring与Ehcache整合

package cn.itcast.cache.ehcache;

import java.io.Serializable;

public class User implements Serializable{

    private String name;
    private int age;

    public User(String name, int age) {
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public int getAge() {
        return age;
    }
    public void setAge(int age) {
        this.age = age;
    }

}
package cn.itcast.cache.ehcache;

public interface UserService {

    User getUsersByNameAndAge(String name, int age);

    User getAnotherUser(String name, int age);
}
package cn.itcast.cache.ehcache;

import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

@Service("userService")
@Cacheable(value="users")
public class UserServiceImpl implements UserService {

    @Override
    public User getUsersByNameAndAge(String name, int age) {
        System.out.println("正在执行getUsersByNameAndAge()方法");
        return new User(name, age);
    }

    @Override
    public User getAnotherUser(String name, int age) {
        System.out.println("正在执行getAnotherUser()方法");
        return new User(name, age);
    }

}
<?xml version="1.0" encoding="UTF-8"?>
<ehcache>
    <!-- 
                        磁盘存储:将缓存中暂时不使用的对象,转移到硬盘,类似于Windows系统的虚拟内存
        path:指定在硬盘上存储对象的路径
     -->
    <diskStore path="java.io.tmpdir" />

    <!-- 
        defaultCache:默认的缓存配置信息,如果不加特殊说明,则所有对象按照此配置项处理
        maxElementsInMemory:设置了缓存的上限,最多存储多少个记录对象
        eternal:代表对象是否永不过期
        timeToIdleSeconds:最大的发呆时间
        timeToLiveSeconds:最大的存活时间
        overflowToDisk:是否允许对象被写入到磁盘
     -->
    <defaultCache maxElementsInMemory="10000" eternal="false"
        timeToIdleSeconds="120" timeToLiveSeconds="120" overflowToDisk="true" />

    <!-- 
        cache:为指定名称的对象进行缓存的特殊配置
        name:指定对象的完整名
     -->
    <cache name="users" maxElementsInMemory="10000" eternal="false"
        timeToIdleSeconds="300" timeToLiveSeconds="600" overflowToDisk="true" />

</ehcache>
<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xmlns="http://www.springframework.org/schema/beans" xmlns:aop="http://www.springframework.org/schema/aop"
  xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx"
  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-4.0.xsd
     http://www.springframework.org/schema/aop
     http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
     http://www.springframework.org/schema/context
     http://www.springframework.org/schema/context/spring-context-4.0.xsd
     http://www.springframework.org/schema/tx
     http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
     http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.0.xsd">

   <cache:annotation-driven cache-manager = "cacheManager"/>

   <!-- 要先开启缓存 -->
   <context:component-scan base-package = "cn.itcast.cache.ehcache"></context:component-scan>

   <bean id = "ehCacheManager" class = "org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
         p:configLocation = "classpath:ehcache.xml"
         p:shared = "false"/>

   <bean id = "cacheManager" class = "org.springframework.cache.ehcache.EhCacheCacheManager"
         p:cacheManager-ref = "ehCacheManager"/>

</beans>
package cn.itcast.cache.ehcache;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class CacheTest {

    public static void main(String[] args) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext_cache.xml");
        UserService us = ctx.getBean("userService", UserService.class);

        User u1 = us.getUsersByNameAndAge("李旺红", 13);
        User u2 = us.getAnotherUser("李旺红", 13);

        System.out.println(u1 == u2);
    }

}

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值