标签:style blog http color io os java ar for
由于这两天用的springmvc 和 mybatis 的搭建的web 框架 然后准备用缓存数据,就简单记录下
准备:
这个可以在https://code.google.com/p/ehcache-spring-annotations/ 下载,下载之后拿出来要用到的jar包
下载的压缩包中的注解包 ehcache-spring-annotations-1.2.0.jar 下载的压缩包中lib 目录下的 ehcache-core-2.4.5.jar guava-r09.jar
由于其他的一些包都在spring mvc 中已经有了,只需要添加这几个进去。
在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。
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/GetCustomerByCid DEBUG - 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: -1 DEBUG - 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 SqlSession DEBUG - SqlSession [org.apache.ibatis.session.defaults.DefaultSqlSession@d213c] was not registered for synchronization because synchronization is not active DEBUG - Fetching JDBC Connection from DataSource DEBUG - 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 Spring DEBUG - 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 DataSource 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] using cached since E:\vss\Project\Java\ly_Smbf\WebRoot\Settings\addCustomer.html didn‘t change. DEBUG - Successfully completed request DEBUG - 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/GetCustomerByCid DEBUG - 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: -1 DEBUG - 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 request DEBUG - Returning cached instance of singleton bean ‘sqlSessionFactory‘
以上就是简单记录下。
spring mvc + mybatis 的ehcache 的简单实现
标签:style blog http color io os java ar for
原文地址:http://www.cnblogs.com/yqweber/p/3977641.html