首页 > 代码库 > spring mvc + mybatis 的ehcache 的简单实现

spring mvc + mybatis 的ehcache 的简单实现

 由于这两天用的springmvc  和 mybatis 的搭建的web 框架  然后准备用缓存数据,就简单记录下

准备:

googlecode 的ehcache

 这个可以在https://code.google.com/p/ehcache-spring-annotations/  下载,下载之后拿出来要用到的jar包

    

下载的压缩包中的注解包ehcache-spring-annotations-1.2.0.jar  下载的压缩包中lib 目录下的ehcache-core-2.4.5.jarguava-r09.jar 

  由于其他的一些包都在spring mvc   中已经有了,只需要添加这几个进去。

配置ehcache.xml

在src  目录下新增配置 ehcache.xml  如下:

<?xml version="1.0" encoding="UTF-8"?><!-- /** *  * 缓存配置 * @author yq  * @date 2014.9.10 *  */ --> <ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"   updateCheck="false">   <diskStore path="java.io.tmpdir" />   <defaultCache eternal="false"      maxElementsInMemory="1000"     overflowToDisk="false"      diskPersistent="false"      timeToIdleSeconds="0"     timeToLiveSeconds="600"      memoryStoreEvictionPolicy="LRU" />     <cache name="CustomerCache"      eternal="false"     maxElementsInMemory="100"     overflowToDisk="false"     diskPersistent="false"      timeToIdleSeconds="0"     timeToLiveSeconds="300"     memoryStoreEvictionPolicy="LRU" />    </ehcache>  

此配置中 可以有多个cache ,方便管理我们程序中的cache。

配置spring.xml

1  <ehcache:annotation-driven cache-manager="ehCacheManager" />   2  <bean id="ehCacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">    3        <property name="configLocation" value="classpath:ehcache.xml" />    4    </bean>  

 

接下来就到我们的代码 Service 层 试用缓存了:

@Cacheable(cacheName = "CustomerCache")    public Customer GetCustomerByCid(int cid){        System.out.print("----------------------------------------------------------");        return cd.GetCustomerByCid(cid);    }    //修改客户    @TriggersRemove(cacheName ={"CustomerCache"},removeAll=true)    public int UpdateCustomer(Customer cus){        return cd.UpdateCustomer(cus);    }

 

注意包的import  ,是这两个

import com.googlecode.ehcache.annotations.Cacheable;import com.googlecode.ehcache.annotations.TriggersRemove;

 

以上就已经搭建好了。 

当我们两次调用 service 中的 GetCustomerByCid 方法时候控制台只有一次sql打印表示成功了

 

DEBUG - DispatcherServlet with name ‘myproject-dispatcher‘ processing GET request for [/jxc/customer/GetCustomerByCid]DEBUG - Looking up handler method for path /customer/GetCustomerByCidDEBUG - Returning handler method [public java.lang.String com.ly.jxc.controller.CustomerController.GetCustomerByCid(java.lang.String,org.springframework.ui.ModelMap)]DEBUG - Returning cached instance of singleton bean ‘customerController‘DEBUG - Last-Modified value for [/jxc/customer/GetCustomerByCid] is: -1DEBUG - Generated key ‘369036164508828‘ for invocation: ReflectiveMethodInvocation: public com.ly.jxc.entity.Customer com.ly.jxc.service.CustomerService.GetCustomerByCid(int); target is of class [com.ly.jxc.service.CustomerService]----------------------------------------------------------DEBUG - Creating a new SqlSessionDEBUG - SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@d213c] was not registered for synchronization because synchronization is not activeDEBUG - Fetching JDBC Connection from DataSourceDEBUG - JDBC Connection [jdbc:mysql://192.168.1.6:3306/lyjxc?useUnicode=true&characterEncoding=utf-8, UserName=root@7-PC, MySQL-AB JDBC Driver] will not be managed by SpringDEBUG - ooo Using Connection [jdbc:mysql://192.168.1.6:3306/lyjxc?useUnicode=true&characterEncoding=utf-8, UserName=root@7-PC, MySQL-AB JDBC Driver]DEBUG - ==>  Preparing: select * from Customer where cid=? DEBUG - ==> Parameters: 18(Integer)DEBUG - Closing non transactional SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@d213c]DEBUG - Returning JDBC Connection to DataSourceDEBUG - Rendering view [com.ly.jxc.util.FreeMarkerPath: name ‘Settings/addCustomer‘; URL [Settings/addCustomer.html]] in DispatcherServlet with name ‘myproject-dispatcher‘DEBUG - Added model object ‘Customer‘ of type [com.ly.jxc.entity.Customer] to request in view with name ‘Settings/addCustomer‘DEBUG - Added model object ‘springMacroRequestContext‘ of type [org.springframework.web.servlet.support.RequestContext] to request in view with name ‘Settings/addCustomer‘DEBUG - Added model object ‘webRoot‘ of type [java.lang.String] to request in view with name ‘Settings/addCustomer‘DEBUG - Added model object ‘org.springframework.validation.BindingResult.Customer‘ of type [org.springframework.validation.BeanPropertyBindingResult] to request in view with name ‘Settings/addCustomer‘DEBUG - Rendering FreeMarker template [Settings/addCustomer.html] in FreeMarkerView ‘Settings/addCustomer‘DEBUG - "Settings/addCustomer.html"["zh_CN",GBK,parsed] using cached since E:\vss\Project\Java\ly_Smbf\WebRoot\Settings\addCustomer.html didn‘t change.DEBUG - Successfully completed requestDEBUG - Returning cached instance of singleton bean ‘sqlSessionFactory‘DEBUG - DispatcherServlet with name ‘myproject-dispatcher‘ processing GET request for [/jxc/customer/GetCustomerByCid]DEBUG - Looking up handler method for path /customer/GetCustomerByCidDEBUG - Returning handler method [public java.lang.String com.ly.jxc.controller.CustomerController.GetCustomerByCid(java.lang.String,org.springframework.ui.ModelMap)]DEBUG - Returning cached instance of singleton bean ‘customerController‘DEBUG - Last-Modified value for [/jxc/customer/GetCustomerByCid] is: -1DEBUG - Generated key ‘369036164508828‘ for invocation: ReflectiveMethodInvocation: public com.ly.jxc.entity.Customer com.ly.jxc.service.CustomerService.GetCustomerByCid(int); target is of class [com.ly.jxc.service.CustomerService]DEBUG - Rendering view [com.ly.jxc.util.FreeMarkerPath: name ‘Settings/addCustomer‘; URL [Settings/addCustomer.html]] in DispatcherServlet with name ‘myproject-dispatcher‘DEBUG - Added model object ‘Customer‘ of type [com.ly.jxc.entity.Customer] to request in view with name ‘Settings/addCustomer‘DEBUG - Added model object ‘springMacroRequestContext‘ of type [org.springframework.web.servlet.support.RequestContext] to request in view with name ‘Settings/addCustomer‘DEBUG - Added model object ‘webRoot‘ of type [java.lang.String] to request in view with name ‘Settings/addCustomer‘DEBUG - Added model object ‘org.springframework.validation.BindingResult.Customer‘ of type [org.springframework.validation.BeanPropertyBindingResult] to request in view with name ‘Settings/addCustomer‘DEBUG - Rendering FreeMarker template [Settings/addCustomer.html] in FreeMarkerView ‘Settings/addCustomer‘DEBUG - "Settings/addCustomer.html"["zh_CN",GBK,parsed] cached copy not yet stale; using cached.DEBUG - Successfully completed requestDEBUG - Returning cached instance of singleton bean ‘sqlSessionFactory‘

  

以上就是简单记录下。

 

spring mvc + mybatis 的ehcache 的简单实现