1.web.xml中增加配置文件: classpath:spring-memcache.xml
2.引入配置文件: spring-memcache.xml memcache.properties(memcache的各项指标)
3.引入jar:commons-pool-1.3.jar
ehcache-core-2.5.0.jar
java_memcached-release_2.6.6.jar
spymemcached-2.8.4.jar
4.使用JUnit测试看是否能正常读取spring-memcache.xml配置文件
5.DataDicController.java使用了memcache(line:81-92)
spring-memcache.xml:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.2.xsd ">
<!--memcache配置 db配置-->
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>classpath:memcache.properties</value>
</list>
</property>
</bean>
<!-- 客户端:java_memcached-release_2.6.3 -->
<bean id="sockIOPool" class="com.danga.MemCached.SockIOPool" factory-method="getInstance" init-method="initialize" lazy-init="false" destroy-method="shutDown">
<constructor-arg>
<value>memcachedPool</value>
</constructor-arg>
<!-- 可以设置多个mememcached服务器 -->
<property name="servers">
<list>
<value>${memcache.server}</value>
</list>
</property>
<!-- 每个服务器初始连接数 -->
<property name="initConn">
<value>${memcache.initConn}</value>
</property>
<!-- 每个服务器最小连接数 -->
<property name="minConn">
<value>${memcache.minConn}</value>
</property>
<!-- 每个服务器最大连接数 -->
<property name="maxConn">
<value>${memcache.maxConn}</value>
</property>
<!-- 主线程睡眠时间 -->
<property name="maintSleep">
<value>${memcache.maintSleep}</value>
</property>
<!-- TCP/Socket的参数,如果是true在写数据时不缓冲,立即发送出去参数 -->
<property name="nagle">
<value>${memcache.nagle}</value>
</property>
<!-- 连接超时/阻塞读取数据的超时间是 -->
<property name="socketTO">
<value>${memcache.socketTO}</value>
</property>
</bean>
<bean id="memcachedClient" class="com.danga.MemCached.MemCachedClient" >
<constructor-arg>
<value>memcachedPool</value>
</constructor-arg>
</bean>
</beans>
重点来啦:Customer要进行序列化
@SuppressWarnings("unchecked")
@RequestMapping("/list")
public String list(@RequestParam(value="page",required=false)String page,@RequestParam(value="rows",required=false)String rows,Customer s_customer,HttpServletResponse response)throws Exception{
PageBean pageBean=new PageBean(Integer.parseInt(page),Integer.parseInt(rows));
Map<String,Object> map=new HashMap<String,Object>();
map.put("khno", StringUtil.formatLike(s_customer.getKhno()));
map.put("name", StringUtil.formatLike(s_customer.getName()));
map.put("start", pageBean.getStart());
map.put("size", pageBean.getPageSize());
//加上缓存
// List<Customer> customerList=customerService.find(map);
Long total=customerService.getTotal(map);
List<Customer> customerList=new ArrayList<Customer>();
//当缓存数据存在并且查询条件一样时,才读取缓存数据
if(memCachedClient.get("customerList")!=null&&map.equals(memCachedClient.get("customerquerymap"))){
customerList = (List<Customer>) memCachedClient.get("customerList");
System.out.println("customerList缓存中分页查询客户");
}else{
customerList=customerService.find(map);
memCachedClient.set("customerList", customerList, 60000);
memCachedClient.set("customerquerymap", map, 60000);
System.out.println("customerList数据库中分页查询客户");
}
JSONObject result=new JSONObject();
JSONArray jsonArray=JSONArray.fromObject(customerList);
result.put("rows", jsonArray);
result.put("total", total);
ResponseUtil.write(response, result);
return null;
}
具体的配置可以@我,可以发源代码