使用Spring3.1.1的缓存示例

jopen 12年前

定义数据访问接口:

package org.kevin.cache.dao;    import java.util.Collection;    import org.kevin.cache.domain.Data;    public interface DataDAO {     public void add(Data data);     public void update(Data data);     public Data find(Integer id);     public void delete(Integer id);      public Collection<Data> getAll();  }


对接口方法进行实现,使用Map模拟数据库的表进行存储数据:

package org.kevin.cache.dao;    import java.util.Collection;  import java.util.HashMap;  import java.util.Map;    import org.kevin.cache.domain.Data;  import org.springframework.cache.annotation.CacheEvict;  import org.springframework.cache.annotation.Cacheable;    public class DataDAOImpl implements DataDAO {     private Map<Integer, Data> dataMap = new HashMap<>();     @Override   @CacheEvict(value = "data", allEntries = true)   public void add(Data data) {    System.out.println("Execute:public void add(Data data)");    dataMap.put(data.getId(), data);   }     @Override   @CacheEvict(value = "data", allEntries = true)   public void update(Data data) {    System.out.println("Execute:public void update(Data data) ");    dataMap.put(data.getId(), data);   }     @Override   @Cacheable(value = "data")   public Data find(Integer id) {    System.out.println("Execute:public Data find(Integer id)");    return dataMap.get(id);   }     @Override   @CacheEvict(value = "data", allEntries = true)   public void delete(Integer id) {    System.out.println("Execute:public void delete(Integer id)");    dataMap.remove(id);   }     @Override   @Cacheable(value = "data")   public Collection<Data> getAll() {    System.out.println("Execute:public Collection<Data> getAll()");    return dataMap.values();   }  }

如上,对数据进行增删改的时候清空缓存,查询时使用缓存。其中value为缓存区,allEntries表示清空缓存区中的所有数据。为了稍后验证缓存是否生效,打印出执行的方法签名。

定义领域模型:

package org.kevin.cache.domain;    public class Data {     private Integer id;   private String first;   private String second;     public Data(Integer id, String first, String second) {    this.id = id;    this.first = first;    this.second = second;   }     public String getFirst() {    return first;   }     public void setFirst(String first) {    this.first = first;   }     public String getSecond() {    return second;   }     public void setSecond(String second) {    this.second = second;   }     public void setId(Integer id) {    this.id = id;   }     public Integer getId() {    return id;   }  }


接下来编写spring配置文件:

<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"       xsi:schemaLocation="               http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd               http://www.springframework.org/schema/cache http://www.springframework.org/schema/cache/spring-cache-3.1.xsd">      <cache:annotation-driven />      <bean id="cacheManager" class="org.springframework.cache.support.SimpleCacheManager">    <property name="caches">     <set>      <bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean" p:name="default" />      <bean class="org.springframework.cache.concurrent.ConcurrentMapCacheFactoryBean" p:name="data" />     </set>    </property>   </bean>     <bean id="dataDao" class="org.kevin.cache.dao.DataDAOImpl" />  </beans>

最后编写main方法验证缓存是否生效:

package org.kevin.cache;    import java.util.Collection;    import org.kevin.cache.dao.DataDAO;  import org.kevin.cache.domain.Data;  import org.springframework.context.support.ClassPathXmlApplicationContext;    public class CacheMain {     public static void main(String[] args) {        ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("spring-cache-demo.xml");    DataDAO dataDAO = context.getBean("dataDao", DataDAO.class);        dataDAO.add(new Data(1, "li", "wenkai"));    Collection<Data> dataCollection = dataDAO.getAll();    System.out.println(dataCollection.size());        System.out.println("=================================");        dataDAO.add(new Data(2, "jack", "lee"));    dataCollection = dataDAO.getAll();    System.out.println(dataCollection.size());;    Data data = dataDAO.find(1);    System.out.println(data.getFirst());    data = dataDAO.find(2);    System.out.println(data.getFirst());    data = dataDAO.find(2);    System.out.println(data.getFirst());;        System.out.println("====================================");    dataDAO.add(new Data(3, "kevin", "lee"));    data = dataDAO.find(2);    System.out.println(data.getFirst());    data = dataDAO.find(3);    System.out.println(data.getFirst());    data = dataDAO.find(2);    System.out.println(data.getFirst());   }  }


打印接过如下,表示已经生效:

Execute:public void add(Data data)  Execute:public Collection<Data> getAll()  1  =================================  Execute:public void add(Data data)  Execute:public Collection<Data> getAll()  2  Execute:public Data find(Integer id)  li  Execute:public Data find(Integer id)  jack  jack  ====================================  Execute:public void add(Data data)  Execute:public Data find(Integer id)  jack  Execute:public Data find(Integer id)  kevin  jack


 

最后,spring支持的缓存机制,是方法级的缓存,而不关注底层是否使用了数据库以及通过什么方式访问的数据库;因此这种缓存不止可以放到DAO层,也可以放置到Service层,甚至可以对各种代码数据进行缓存。

如果应用中同时使用了Hibernate和Ibatis,或者这种方式是一种好的缓存解决方案。


  转自:http://blog.csdn.net/scorpio3k/article/details/7628894