Spring学习之缓存机制EhCache

整体代码结构:



User.java

package com.kinsey.woo.dto;

public class User {

	private String name;
	private int 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;
	}
	public User(String name, int age) {
		super();
		this.name = name;
		this.age = age;
	}
}


UserService.java

package com.kinsey.woo.service;

import com.kinsey.woo.dto.User;

public interface UserService {

	public User getUserByNameAndAge(String name,int age);
	
	public User getAntherUser(String name, int age);
	
}

UserServiceImpl.java

package com.kinsey.woo.service.impl;

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

import com.kinsey.woo.dto.User;
import com.kinsey.woo.service.UserService;

@Service("userService")
@Cacheable(value="users")
public class UserServiceImpl implements UserService {
	
	public User getUserByNameAndAge(String name,int age){
		System.out.print("getUserByNameAndAge is called...\n");
		return new User(name,age);
	}

	@Override
	public User getAntherUser(String name, int age) {
		System.out.println("getAntherUser is called...\n");
		return new User(name,age);
	}
}
ehcache.xml

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

 4     <!-- 
 5         磁盘存储:将缓存中暂时不使用的对象,转移到硬盘,类似于Windows系统的虚拟内存
 6         path:指定在硬盘上存储对象的路径
 7      -->
 8     <diskStore path="java.io.tmpdir" />
 9 
10     
11     <!-- 
12         defaultCache:默认的缓存配置信息,如果不加特殊说明,则所有对象按照此配置项处理
13         maxElementsInMemory:设置了缓存的上限,最多存储多少个记录对象
14         eternal:代表对象是否永不过期
15         timeToIdleSeconds:最大的发呆时间
16         timeToLiveSeconds:最大的存活时间
17         overflowToDisk:是否允许对象被写入到磁盘
18      -->
19     <defaultCache maxElementsInMemory="10000" eternal="false"  timeToIdleSeconds="120" 
			timeToLiveSeconds="120" 
			overflowToDisk="true"
			maxElementsOnDisk="120000"
			diskExpiryThreadIntervalSeconds="120"
			memoryStoreEvictionPolicy="LRU"
			 />
21     
22     <!-- 
23         cache:为指定名称的对象进行缓存的特殊配置
24         name:指定对象的完整名
25      -->
26     <cache name="users" maxElementsInMemory="10000"  eternal="false" timeToIdleSeconds="300" 
		    timeToLiveSeconds="600" 
		    overflowToDisk="true" />
28 
29 
30 </ehcache>


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


	<bean id="ehCacheManger" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean"
		p:configLocation="classpath:ehcache.xml"
	/>
	
	
	<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager" 
		p:cacheManager-ref="ehCacheManger"
	/>
	
	<context:component-scan base-package="com.kinsey.woo.service"></context:component-scan>
	
	<cache:annotation-driven cache-manager="cacheManager"/>

</beans>

RunMain.java

package com.kinsey.woo.main;

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

import com.kinsey.woo.dto.User;
import com.kinsey.woo.service.UserService;

public class RunMain {
	public static void main(String[] args) {

		
		@SuppressWarnings("resource")
		ApplicationContext ctx = new ClassPathXmlApplicationContext("application.xml");
		
		UserService userService = ctx.getBean("userService", UserService.class);
	
		User u1 = userService.getUserByNameAndAge("Godwin", 10);
		User u2 = userService.getAntherUser("Godwin", 10);
		
		if(u1==u2){
			System.out.print("u1=u2 \n");
		}
		
	}
}


jar:

ehcache-core-2.6.11.jar

slf4j-api-1.7.25.jar








  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值