spring缓存实例
2015-12-30 17:03
411 查看
这篇文章主要是对上一篇文章介绍的spring缓存的技术进行的实现,可以在这里下载源码。首先定义一个参数类,用来接收参数
Parametres
Repose用来接收参数
service实现
测试类
具体的分析可以看上一篇文章
Parametres
package org.springframework.democache.model; import java.io.Serializable; public class Parametres implements Serializable{ private Long param1; private String param2; private String param3; public Long getParam1() { return param1; } public void setParam1(Long param1) { this.param1 = param1; } public String getParam2() { return param2; } public void setParam2(String param2) { this.param2 = param2; } public Parametres() { super(); } public Parametres(Long param1, String param2, String param3) { super(); this.param1 = param1; this.param2 = param2; this.param3 = param3; } @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + ((param1 == null) ? 0 : param1.hashCode()); result = prime * result + ((param2 == null) ? 0 : param2.hashCode()); result = prime * result + ((param3 == null) ? 0 : param3.hashCode()); return result; } @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Parametres other = (Parametres) obj; if (param1 == null) { if (other.param1 != null) return false; } else if (!param1.equals(other.param1)) return false; if (param2 == null) { if (other.param2 != null) return false; } else if (!param2.equals(other.param2)) return false; if (param3 == null) { if (other.param3 != null) return false; } else if (!param3.equals(other.param3)) return false; return true; } }
Repose用来接收参数
package org.springframework.democache.model; import java.io.Serializable; public class Reponse implements Serializable{ private Long id; private String valeur; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getValeur() { return valeur; } public void setValeur(String valeur) { this.valeur = valeur; } public Reponse(Long id, String valeur) { super(); this.id = id; this.valeur = valeur; } public Reponse() { super(); } public boolean equals(Object other) { if(!(other instanceof Reponse)) { return false; } Reponse otherReponse = (Reponse) other; return id == otherReponse.id && valeur == otherReponse.valeur; } public int hashCode() { return id.intValue(); } }
service实现
package org.springframework.democache.service; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.cache.annotation.CacheEvict; import org.springframework.cache.annotation.CachePut; import org.springframework.cache.annotation.Cacheable; import org.springframework.democache.model.Parametres; import org.springframework.democache.model.Reponse; import org.springframework.stereotype.Service; @Service public class ServiceCacher { private static final Logger logger = LoggerFactory.getLogger(ServiceCacher.class); @Cacheable("dataCache") public Reponse getDatas(Parametres params){ logger.info("execution getDatas()"); Reponse Reponse=null; if(params!=null){ Reponse=new Reponse(params.getParam1(), params.getParam2()); } return Reponse; } @Cacheable(value="dataCache",condition="#params.param1>100") public Reponse getDatasWithCondition(Parametres params){ logger.info("execution getDatasWithCondition()"); Reponse Reponse=null; if(params!=null){ Reponse=new Reponse(params.getParam1(), params.getParam2()); } return Reponse; } @Cacheable(value="dataCache",key="#params.param1") public Reponse getDatasWithKey(Parametres params){ logger.info("execution getDatasWithKey()"); Reponse Reponse=null; if(params!=null){ Reponse=new Reponse(params.getParam1(), params.getParam2()); } return Reponse; } @CacheEvict(value="dataCache",allEntries=true) public void viderCacheData(){ } @CachePut("dataCache") public Reponse getDatasWithPut(Parametres params){ logger.info("execution getDatasWithPut()"); Reponse Reponse=null; if(params!=null){ Reponse=new Reponse(params.getParam1(), params.getParam2()); } return Reponse; } }
测试类
package org.springframework.democache.tests; import static org.fest.assertions.Assertions.assertThat; import org.junit.Test; import org.junit.runner.RunWith; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.democache.model.Parametres; import org.springframework.democache.model.Reponse; import org.springframework.democache.service.ServiceCacher; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = {"/test-application-context.xml"}) public class ServiceCacheTest { public static final Logger logger = LoggerFactory.getLogger(ServiceCacheTest.class); @Autowired private ServiceCacher service; @Test public void testCache() { logger.info("Appel 1"); Reponse rep=service.getDatas(new Parametres(10L,"param1","param2")); assertThat(rep).isNotNull(); logger.info("Appel 2"); Reponse rep2=service.getDatas(new Parametres(10L,"param1","param2")); assertThat(rep2).isNotNull(); assertThat(rep).isSameAs(rep2); } @Test public void testCacheCondition() { logger.info("Appel 1"); Reponse rep1=service.getDatasWithCondition(new Parametres(90L,"param12","param22")); assertThat(rep1).isNotNull(); logger.info("Appel 2"); Reponse rep2=service.getDatasWithCondition(new Parametres(90L,"param12","param22")); assertThat(rep2).isNotNull(); assertThat(rep1).isNotSameAs(rep2); assertThat(rep1).isEqualTo(rep2); Reponse rep3=service.getDatasWithCondition(new Parametres(110L,"param13","param23")); assertThat(rep3).isNotNull(); logger.info("Appel 4"); Reponse rep4=service.getDatasWithCondition(new Parametres(110L,"param13","param23")); assertThat(rep4).isNotNull(); assertThat(rep3).isSameAs(rep4); } @Test public void testCacheKey() { logger.info("Appel 1"); Reponse rep1=service.getDatasWithKey(new Parametres(90L,"param12","param22")); assertThat(rep1).isNotNull(); logger.info("Appel 2"); Reponse rep2=service.getDatasWithKey(new Parametres(90L,"param123","param223")); assertThat(rep2).isNotNull(); assertThat(rep1).isSameAs(rep2); Reponse rep3=service.getDatasWithKey(new Parametres(110L,"param13","param23")); assertThat(rep3).isNotNull(); logger.info("Appel 4"); Reponse rep4=service.getDatasWithKey(new Parametres(30L,"param13","param23")); assertThat(rep4).isNotNull(); assertThat(rep3).isNotSameAs(rep4); assertThat(rep3).isNotEqualTo(rep4); } @Test public void testCacheEvict() { logger.info("Appel 1"); Reponse rep1=service.getDatas(new Parametres(90L,"param1","param2")); assertThat(rep1).isNotNull(); logger.info("Appel 2"); Reponse rep2=service.getDatas(new Parametres(90L,"param1","param2")); assertThat(rep2).isNotNull(); assertThat(rep1).isSameAs(rep2); service.viderCacheData(); logger.info("Appel 3"); Reponse rep3=service.getDatas(new Parametres(90L,"param1","param2")); assertThat(rep3).isNotNull(); assertThat(rep3).isNotSameAs(rep2); assertThat(rep3).isEqualTo(rep2); } @Test public void testCachePut() { logger.info("Appel 1"); Reponse rep1=service.getDatas(new Parametres(90L,"param1","param2")); assertThat(rep1).isNotNull(); logger.info("Appel 2"); Reponse rep2=service.getDatas(new Parametres(90L,"param1","param2")); assertThat(rep2).isNotNull(); assertThat(rep1).isSameAs(rep2); logger.info("Appel 3"); Reponse rep3=service.getDatasWithPut(new Parametres(90L,"param1","param2")); assertThat(rep3).isNotNull(); assertThat(rep3).isNotSameAs(rep2); assertThat(rep3).isEqualTo(rep2); } }
具体的分析可以看上一篇文章
相关文章推荐
- 【算法基础】java中的移位运算符和位运算符
- Java Web(9)struts 2 validation result设置
- JVM(Java虚拟机)优化大全和案例实战
- java内存模型的三个代
- Java 泛型通配符?解惑
- Java总结篇系列:Java泛型
- spring 与 mybatis整合,mybatis注解不起作用
- AIDL中使用java类
- CSDN博客文章之Java资源
- 关于 "java中常量定义在interface中好还是定义在class中好" 的一些思考
- JAVA常见问题总结
- Spring各jar包的作用(转载)
- Java设计模式_行为型_观察者模式_任意行为的监听
- Java中判断String不为空的问题
- Java类集学习(五)集合的输出
- java.io.IOException: Could not locate executable null\bin\winutils.exe in the Hadoop binaries
- ubuntu启动eclipse时出错cannot open display
- jsonp和java示例
- 希望能结识更多热爱Java的小伙伴
- Java 1.7 语法新特性