Spring actual method level cache usage example
•
Java
This article describes the method level cache usage of spring practice. Share with you for your reference, as follows:
One configuration file
<?xml version="1.0" encoding="GBK"?> <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: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-4.0.xsd http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <context:component-scan base-package="org.crazyit.app.service" /> <cache:annotation-driven cache-manager="cacheManager" /> <!-- 配置EhCache的CacheManager 通过configLocation指定ehcache.xml文件的位置 --> <bean id="ehCacheManager" class="org.springframework.cache.ehcache.EhCacheManagerfactorybean" p:configLocation="classpath:ehcache.xml" p:shared="false" /> <!-- 配置基于EhCache的缓存管理器 并将EhCache的CacheManager注入该缓存管理器Bean --> <bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager" p:cacheManager-ref="ehCacheManager"> </bean> </beans>
II. Attribute file
<?xml version="1.0" encoding="gbk"?> <ehcache> <diskStore path="java.io.tmpdir" /> <!-- 配置默认的缓存区 --> <defaultCache maxElementsInMemory="10000" eternal="false" timeToIdleSeconds="120" timeToLiveSeconds="120" maxElementsOnDisk="10000000" diskExpiryThreadIntervalSeconds="120" memoryStoreEvictionPolicy="LRU"/> <!-- 配置名为users1的缓存区 --> <cache name="users1" maxElementsInMemory="10000" eternal="false" overflowToDisk="true" timeToIdleSeconds="300" timeToLiveSeconds="600" /> <cache name="users2" maxElementsInMemory="10000" eternal="false" overflowToDisk="true" timeToIdleSeconds="300" timeToLiveSeconds="600" /> </ehcache>
Three domain model
package org.crazyit.app.domain; public class User { private String name; private int age; public User() {} public User(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; } }
IV. service
1 interface class
package org.crazyit.app.service; import org.crazyit.app.domain.User; public interface UserService { User getUsersByNameAndAge(String name,int age); User getAnotherUser(String name,int age); }
2 implementation class
package org.crazyit.app.service.impl; import org.crazyit.app.service.UserService; import org.crazyit.app.domain.User; import org.springframework.stereotype.Service; import org.springframework.cache.annotation.Cacheable; import org.springframework.context.annotation.Scope; @Service("userService") public class UserServiceImpl implements UserService { @Cacheable(value = "users1") public User getUsersByNameAndAge(String name,int age) { System.out.println("--正在执行findUsersByNameAndAge()查询方法--"); return new User(name,age); } @Cacheable(value = "users2") public User getAnotherUser(String name,int age) { System.out.println("--正在执行findAnotherUser()查询方法--"); return new User(name,age); } }
Five test categories
package lee; import org.crazyit.app.service.UserService; import org.crazyit.app.domain.User; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class SpringTest { public static void main(String[] args) { ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml"); UserService us = ctx.getBean("userService",UserService.class); // 第一次调用us对象的方法时会执行该方法,并缓存方法的结果 User u1 = us.getUsersByNameAndAge("孙悟空",500); // 由于getAnotherUser()方法使用另一个缓存区, // 因此无法使用getUsersByNameAndAge()方法缓存区的数据。 User u2 = us.getAnotherUser("孙悟空",500); System.out.println(u1 == u2); // 输出false // getAnotherUser("孙悟空",500)已经执行过一次,故下面代码使用缓存 User u3 = us.getAnotherUser("孙悟空",500); System.out.println(u2 == u3); // 输出true } }
Vi. test results
Readers interested in more Java related content can view the topics on this site: introduction and advanced tutorial of spring framework, tutorial of Java data structure and algorithm, summary of Java DOM node operation skills, summary of java file and directory operation skills, and summary of Java cache operation skills
I hope this article will be helpful to you in Java programming.
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码