使用Spring缓存的简单Demo

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用Spring缓存的简单Demo相关的知识,希望对你有一定的参考价值。

使用Spring缓存的简单Demo

1. 首先创建Maven工程,在Pom中配置

                <dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-core</artifactId>
			<version>4.1.2.RELEASE</version>
		</dependency>
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>4.1.2.RELEASE</version>
		</dependency>

2. 创建Student类和StudentServer 类

package my.testcache;

public class Student {
	private String name;
	private int age;
	
	
	public Student(String name, int age) {
		super();
		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;
	}
	
	@Override
	public String toString() {
		return "Student [name=" + name + ", age=" + age + "]";
	}
	
	
	
}

  StudentServer

package my.testcache;

import java.util.HashMap;
import java.util.Map;

import org.springframework.cache.annotation.Cacheable;

public class StudentService {
	
	private Map<Integer, Student> stus = new HashMap<Integer, Student>();
	{
		stus.put(1, new Student("zhangsan",100));
		stus.put(2, new Student("lisi", 106));
	};
	
	/**
	 * 根据参数对返回值进行缓存
	 */
	@Cacheable(value="students")
	public Student getStudent(int id){
		System.out.println("getStudent: " + id);
		return stus.get(id);
	};
}

注意:getStudent方法要定义为Public才能使用缓存。

有条件的缓存 condition = "#id < 2",表示只缓存id<2的数据。

	@Cacheable(value="students", condition = "#id < 2")
	public Student getStudent(int id){
		System.out.println("getStudent: " + id);
		return stus.get(id);
	};

 

2. 在src/main/srsources中创建applicationContext.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"
  xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
    http://www.springframework.org/schema/cache
    http://www.springframework.org/schema/cache/spring-cache-4.0.xsd">
     
    <!-- 应用Bean类或者其方法的注解完成缓存的配置 -->
    <cache:annotation-driven/>
    <bean id="studentServer" class="my.testcache.StudentService" />
    
    <!-- cacheManager使用JDK中的ConcurrentMap作为存储器 -->
    <bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager" >
    	<property name="caches">
    		<set>
    			<bean id="students" class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean"/>
    		</set>
    	</property>
    </bean>
</beans>

 

注意: 此时命名的bean students要与 StudentServer中的getStudents 的注解alue (@Cacheable(value="students"))一致

 

4. 执行Main方法

public static void main(String[] args) {
		ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
											
		StudentService studentService = context.getBean(StudentService.class);
		Student student1 = studentService.getStudent(1);
		System.out.println(student1);
		
		Student student2 = studentService.getStudent(1);
		System.out.println(student2);
	}

5. 显示结果

getStudent: 1
Student [name=zhangsan, age=100]
Student [name=zhangsan, age=100]

说明第二次调用使用了缓存。

 

以上是关于使用Spring缓存的简单Demo的主要内容,如果未能解决你的问题,请参考以下文章

spring中添加google的guava缓存(demo)

spring-data-jpa+hibernate 各种缓存的配置演示

Spring Boot 缓存的基本用法

Android获取各个应用程序的缓存文件代码小片段(使用AIDL)

[死磕 Spring 17/43] --- IOC 之从单例缓存中获取单例 bean

Spring Cachable Key的定义及应用