spring缓存实例

来源:互联网 发布:sqlserver 统计表空间 编辑:程序博客网 时间:2024/06/05 06:53

这篇文章主要是对上一篇文章介绍的spring缓存的技术进行的实现,可以在这里下载源码。首先定义一个参数类,用来接收参数

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;}@Overridepublic 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;}@Overridepublic 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;@Servicepublic 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);    }}

具体的分析可以看上一篇文章

0 0
原创粉丝点击