需要使用Spring来实现一个Cache简单的解决方案,具体需求如下:使用任意一个现有开源Cache Framework,要求可以Cache系统中Service或则DAO层的get/find等方法返回结果,如果数据更新(使用Create/update/delete方法),则刷新cache中相应的内容。 



根据需求,计划使用Spring AOP + ehCache来实现这个功能,采用ehCache原因之一是Spring提供了ehCache的支持,至于为何仅仅支持ehCache而不支持osCache和JBossCache无从得知(Hibernate???),但毕竟Spring提供了支持,可以减少一部分工作量:)。二是后来实现了OSCache和JBoss Cache的方式后,经过简单测试发现几个Cache在效率上没有太大的区别(不考虑集群),决定采用ehCahce。 



AOP嘛,少不了拦截器,先创建一个实现了MethodInterceptor接口的拦截器,用来拦截Service/DAO的方法调用,拦截到方法后,搜索该方法的结果在cache中是否存在,如果存在,返回cache中的缓存结果,如果不存在,返回查询数据库的结果,并将结果缓存到cache中。 



MethodCacheInterceptor.java

Java代码 
  1. package com.co.cache.ehcache;
  2. import java.io.Serializable;
  3. import net.sf.ehcache.Cache;
  4. import net.sf.ehcache.Element;
  5. import org.aopalliance.intercept.MethodInterceptor;
  6. import org.aopalliance.intercept.MethodInvocation;
  7. import org.apache.commons.logging.Log;
  8. import org.apache.commons.logging.LogFactory;
  9. import org.springframework.beans.factory.InitializingBean;
  10. import org.springframework.util.Assert;
  11. public class MethodCacheInterceptor implements MethodInterceptor, InitializingBean
  12. {
  13. private static final Log logger = LogFactory.getLog(MethodCacheInterceptor.class);
  14. private Cache cache;
  15. public void setCache(Cache cache) {
  16. this.cache = cache;
  17. }
  18. public MethodCacheInterceptor() {
  19. super();
  20. }
  21. /**
  22. * 拦截Service/DAO的方法,并查找该结果是否存在,如果存在就返回cache中的值,
  23. * 否则,返回数据库查询结果,并将查询结果放入cache
  24. */
  25. public Object invoke(MethodInvocation invocation) throws Throwable {
  26. String targetName = invocation.getThis().getClass().getName();
  27. String methodName = invocation.getMethod().getName();
  28. Object[] arguments = invocation.getArguments();
  29. Object result;
  30. logger.debug("Find object from cache is " + cache.getName());
  31. String cacheKey = getCacheKey(targetName, methodName, arguments);
  32. Element element = cache.get(cacheKey);
  33. if (element == null) {
  34. logger.debug("Hold up method , Get method result and create cache........!");
  35. result = invocation.proceed();
  36. element = new Element(cacheKey, (Serializable) result);
  37. cache.put(element);
  38. }
  39. return element.getValue();
  40. }
  41. /**
  42. * 获得cache key的方法,cache key是Cache中一个Element的唯一标识
  43. * cache key包括 包名+类名+方法名,如com.co.cache.service.UserServiceImpl.getAllUser
  44. */
  45. private String getCacheKey(String targetName, String methodName, Object[] arguments) {
  46. StringBuffer sb = new StringBuffer();
  47. sb.append(targetName).append(".").append(methodName);
  48. if ((arguments != null) && (arguments.length != 0)) {
  49. for (int i = 0; i < arguments.length; i++) {
  50. sb.append(".").append(arguments[i]);
  51. }
  52. }
  53. return sb.toString();
  54. }
  55. /**
  56. * implement InitializingBean,检查cache是否为空
  57. */
  58. public void afterPropertiesSet() throws Exception {
  59. Assert.notNull(cache, "Need a cache. Please use setCache(Cache) create it.");
  60. }
  61. }
[java] view
plain
copy

  1. package com.co.cache.ehcache;
  2. import java.io.Serializable;
  3. import net.sf.ehcache.Cache;
  4. import net.sf.ehcache.Element;
  5. import org.aopalliance.intercept.MethodInterceptor;
  6. import org.aopalliance.intercept.MethodInvocation;
  7. import org.apache.commons.logging.Log;
  8. import org.apache.commons.logging.LogFactory;
  9. import org.springframework.beans.factory.InitializingBean;
  10. import org.springframework.util.Assert;
  11. public class MethodCacheInterceptor implements MethodInterceptor, InitializingBean
  12. {
  13. private static final Log logger = LogFactory.getLog(MethodCacheInterceptor.class);
  14. private Cache cache;
  15. public void setCache(Cache cache) {
  16. this.cache = cache;
  17. }
  18. public MethodCacheInterceptor() {
  19. super();
  20. }
  21. /**
  22. * 拦截Service/DAO的方法,并查找该结果是否存在,如果存在就返回cache中的值,
  23. * 否则,返回数据库查询结果,并将查询结果放入cache
  24. */
  25. public Object invoke(MethodInvocation invocation) throws Throwable {
  26. String targetName = invocation.getThis().getClass().getName();
  27. String methodName = invocation.getMethod().getName();
  28. Object[] arguments = invocation.getArguments();
  29. Object result;
  30. logger.debug("Find object from cache is " + cache.getName());
  31. String cacheKey = getCacheKey(targetName, methodName, arguments);
  32. Element element = cache.get(cacheKey);
  33. if (element == null) {
  34. logger.debug("Hold up method , Get method result and create cache........!");
  35. result = invocation.proceed();
  36. element = new Element(cacheKey, (Serializable) result);
  37. cache.put(element);
  38. }
  39. return element.getValue();
  40. }
  41. /**
  42. * 获得cache key的方法,cache key是Cache中一个Element的唯一标识
  43. * cache key包括 包名+类名+方法名,如com.co.cache.service.UserServiceImpl.getAllUser
  44. */
  45. private String getCacheKey(String targetName, String methodName, Object[] arguments) {
  46. StringBuffer sb = new StringBuffer();
  47. sb.append(targetName).append(".").append(methodName);
  48. if ((arguments != null) && (arguments.length != 0)) {
  49. for (int i = 0; i < arguments.length; i++) {
  50. sb.append(".").append(arguments[i]);
  51. }
  52. }
  53. return sb.toString();
  54. }
  55. /**
  56. * implement InitializingBean,检查cache是否为空
  57. */
  58. public void afterPropertiesSet() throws Exception {
  59. Assert.notNull(cache, "Need a cache. Please use setCache(Cache) create it.");
  60. }
  61. }

上面的代码中可以看到,在方法public Object invoke(MethodInvocation invocation) 中,完成了搜索Cache/新建cache的功能。

Java代码 
  1. Element element = cache.get(cacheKey);
[java] view
plain
copy

  1. Element element = cache.get(cacheKey);

这句代码的作用是获取cache中的element,如果cacheKey所对应的element不存在,将会返回一个null值

Java代码 
  1. result = invocation.proceed();
[java] view
plain
copy

  1. result = invocation.proceed();

这句代码的作用是获取所拦截方法的返回值,详细请查阅AOP相关文档。 



随后,再建立一个拦截器MethodCacheAfterAdvice,作用是在用户进行create/update/delete操作时来刷新/remove相关cache内容,这个拦截器实现了AfterReturningAdvice接口,将会在所拦截的方法执行后执行在public void afterReturning(Object arg0, Method arg1, Object[] arg2, Object arg3)方法中所预定的操作

Java代码 
  1. package com.co.cache.ehcache;
  2. import java.lang.reflect.Method;
  3. import java.util.List;
  4. import net.sf.ehcache.Cache;
  5. import org.apache.commons.logging.Log;
  6. import org.apache.commons.logging.LogFactory;
  7. import org.springframework.aop.AfterReturningAdvice;
  8. import org.springframework.beans.factory.InitializingBean;
  9. import org.springframework.util.Assert;
  10. public class MethodCacheAfterAdvice implements AfterReturningAdvice, InitializingBean
  11. {
  12. private static final Log logger = LogFactory.getLog(MethodCacheAfterAdvice.class);
  13. private Cache cache;
  14. public void setCache(Cache cache) {
  15. this.cache = cache;
  16. }
  17. public MethodCacheAfterAdvice() {
  18. super();
  19. }
  20. public void afterReturning(Object arg0, Method arg1, Object[] arg2, Object arg3) throws Throwable {
  21. String className = arg3.getClass().getName();
  22. List list = cache.getKeys();
  23. for(int i = 0;i<list.size();i++){
  24. String cacheKey = String.valueOf(list.get(i));
  25. if(cacheKey.startsWith(className)){
  26. cache.remove(cacheKey);
  27. logger.debug("remove cache " + cacheKey);
  28. }
  29. }
  30. }
  31. public void afterPropertiesSet() throws Exception {
  32. Assert.notNull(cache, "Need a cache. Please use setCache(Cache) create it.");
  33. }
  34. }
[java] view
plain
copy

  1. package com.co.cache.ehcache;
  2. import java.lang.reflect.Method;
  3. import java.util.List;
  4. import net.sf.ehcache.Cache;
  5. import org.apache.commons.logging.Log;
  6. import org.apache.commons.logging.LogFactory;
  7. import org.springframework.aop.AfterReturningAdvice;
  8. import org.springframework.beans.factory.InitializingBean;
  9. import org.springframework.util.Assert;
  10. public class MethodCacheAfterAdvice implements AfterReturningAdvice, InitializingBean
  11. {
  12. private static final Log logger = LogFactory.getLog(MethodCacheAfterAdvice.class);
  13. private Cache cache;
  14. public void setCache(Cache cache) {
  15. this.cache = cache;
  16. }
  17. public MethodCacheAfterAdvice() {
  18. super();
  19. }
  20. public void afterReturning(Object arg0, Method arg1, Object[] arg2, Object arg3) throws Throwable {
  21. String className = arg3.getClass().getName();
  22. List list = cache.getKeys();
  23. for(int i = 0;i<list.size();i++){
  24. String cacheKey = String.valueOf(list.get(i));
  25. if(cacheKey.startsWith(className)){
  26. cache.remove(cacheKey);
  27. logger.debug("remove cache " + cacheKey);
  28. }
  29. }
  30. }
  31. public void afterPropertiesSet() throws Exception {
  32. Assert.notNull(cache, "Need a cache. Please use setCache(Cache) create it.");
  33. }
  34. }

上面的代码很简单,实现了afterReturning方法实现自AfterReturningAdvice接口,方法中所定义的内容将会在目标方法执行后执行,在该方法中

Java代码 
  1. String className = arg3.getClass().getName();
[java] view
plain
copy

  1. String className = arg3.getClass().getName();

的作用是获取目标class的全名,如:com.co.cache.test.TestServiceImpl,然后循环cache的key list,remove cache中所有和该class相关的element。 



随后,开始配置ehCache的属性,ehCache需要一个xml文件来设置ehCache相关的一些属性,如最大缓存数量、cache刷新的时间等等. 

ehcache.xml

Java代码 
  1. <ehcache>
  2. <diskStore path="c://myapp//cache"/>
  3. <defaultCache
  4. maxElementsInMemory="1000"
  5. eternal="false"
  6. timeToIdleSeconds="120"
  7. timeToLiveSeconds="120"
  8. overflowToDisk="true"
  9. />
  10. <cache name="DEFAULT_CACHE"
  11. maxElementsInMemory="10000"
  12. eternal="false"
  13. timeToIdleSeconds="300000"
  14. timeToLiveSeconds="600000"
  15. overflowToDisk="true"
  16. />
  17. </ehcache>
[java] view
plain
copy

  1. <ehcache>
  2. <diskStore path="c://myapp//cache"/>
  3. <defaultCache
  4. maxElementsInMemory="1000"
  5. eternal="false"
  6. timeToIdleSeconds="120"
  7. timeToLiveSeconds="120"
  8. overflowToDisk="true"
  9. />
  10. <cache name="DEFAULT_CACHE"
  11. maxElementsInMemory="10000"
  12. eternal="false"
  13. timeToIdleSeconds="300000"
  14. timeToLiveSeconds="600000"
  15. overflowToDisk="true"
  16. />
  17. </ehcache>

配置每一项的详细作用不再详细解释,有兴趣的请google下 ,这里需要注意一点defaultCache标签定义了一个默认的Cache,这个Cache是不能删除的,否则会抛出No default cache is configured异常。另外,由于使用拦截器来刷新Cache内容,因此在定义cache生命周期时可以定义较大的数值,timeToIdleSeconds="300000" timeToLiveSeconds="600000",好像还不够大? 



然后,在将Cache和两个拦截器配置到Spring,这里没有使用2.0里面AOP的标签。 

cacheContext.xml

Java代码 
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
  3. <beans>
  4. <!-- 引用ehCache的配置 -->
  5. <bean id="defaultCacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
  6. <property name="configLocation">
  7. <value>ehcache.xml</value>
  8. </property>
  9. </bean>
  10. <!-- 定义ehCache的工厂,并设置所使用的Cache name -->
  11. <bean id="ehCache" class="org.springframework.cache.ehcache.EhCacheFactoryBean">
  12. <property name="cacheManager">
  13. <ref local="defaultCacheManager"/>
  14. </property>
  15. <property name="cacheName">
  16. <value>DEFAULT_CACHE</value>
  17. </property>
  18. </bean>
  19. <!-- find/create cache拦截器 -->
  20. <bean id="methodCacheInterceptor" class="com.co.cache.ehcache.MethodCacheInterceptor">
  21. <property name="cache">
  22. <ref local="ehCache" />
  23. </property>
  24. </bean>
  25. <!-- flush cache拦截器 -->
  26. <bean id="methodCacheAfterAdvice" class="com.co.cache.ehcache.MethodCacheAfterAdvice">
  27. <property name="cache">
  28. <ref local="ehCache" />
  29. </property>
  30. </bean>
  31. <bean id="methodCachePointCut" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
  32. <property name="advice">
  33. <ref local="methodCacheInterceptor"/>
  34. </property>
  35. <property name="patterns">
  36. <list>
  37. <value>.*find.*</value>
  38. <value>.*get.*</value>
  39. </list>
  40. </property>
  41. </bean>
  42. <bean id="methodCachePointCutAdvice" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
  43. <property name="advice">
  44. <ref local="methodCacheAfterAdvice"/>
  45. </property>
  46. <property name="patterns">
  47. <list>
  48. <value>.*create.*</value>
  49. <value>.*update.*</value>
  50. <value>.*delete.*</value>
  51. </list>
  52. </property>
  53. </bean>
  54. </beans>
[java] view
plain
copy

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
  3. <beans>
  4. <!-- 引用ehCache的配置 -->
  5. <bean id="defaultCacheManager" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
  6. <property name="configLocation">
  7. <value>ehcache.xml</value>
  8. </property>
  9. </bean>
  10. <!-- 定义ehCache的工厂,并设置所使用的Cache name -->
  11. <bean id="ehCache" class="org.springframework.cache.ehcache.EhCacheFactoryBean">
  12. <property name="cacheManager">
  13. <ref local="defaultCacheManager"/>
  14. </property>
  15. <property name="cacheName">
  16. <value>DEFAULT_CACHE</value>
  17. </property>
  18. </bean>
  19. <!-- find/create cache拦截器 -->
  20. <bean id="methodCacheInterceptor" class="com.co.cache.ehcache.MethodCacheInterceptor">
  21. <property name="cache">
  22. <ref local="ehCache" />
  23. </property>
  24. </bean>
  25. <!-- flush cache拦截器 -->
  26. <bean id="methodCacheAfterAdvice" class="com.co.cache.ehcache.MethodCacheAfterAdvice">
  27. <property name="cache">
  28. <ref local="ehCache" />
  29. </property>
  30. </bean>
  31. <bean id="methodCachePointCut" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
  32. <property name="advice">
  33. <ref local="methodCacheInterceptor"/>
  34. </property>
  35. <property name="patterns">
  36. <list>
  37. <value>.*find.*</value>
  38. <value>.*get.*</value>
  39. </list>
  40. </property>
  41. </bean>
  42. <bean id="methodCachePointCutAdvice" class="org.springframework.aop.support.RegexpMethodPointcutAdvisor">
  43. <property name="advice">
  44. <ref local="methodCacheAfterAdvice"/>
  45. </property>
  46. <property name="patterns">
  47. <list>
  48. <value>.*create.*</value>
  49. <value>.*update.*</value>
  50. <value>.*delete.*</value>
  51. </list>
  52. </property>
  53. </bean>
  54. </beans>

上面的代码最终创建了两个"切入点",methodCachePointCut和methodCachePointCutAdvice,分别用于拦截不同方法名的方法,可以根据需要任意增加所需要拦截方法的名称。 

需要注意的是

Java代码 
  1. <bean id="ehCache" class="org.springframework.cache.ehcache.EhCacheFactoryBean">
  2. <property name="cacheManager">
  3. <ref local="defaultCacheManager"/>
  4. </property>
  5. <property name="cacheName">
  6. <value>DEFAULT_CACHE</value>
  7. </property>
  8. </bean>
[java] view
plain
copy

  1. <bean id="ehCache" class="org.springframework.cache.ehcache.EhCacheFactoryBean">
  2. <property name="cacheManager">
  3. <ref local="defaultCacheManager"/>
  4. </property>
  5. <property name="cacheName">
  6. <value>DEFAULT_CACHE</value>
  7. </property>
  8. </bean>

如果cacheName属性内设置的name在ehCache.xml中无法找到,那么将使用默认的cache(defaultCache标签定义). 



事实上到了这里,一个简单的Spring + ehCache Framework基本完成了,为了测试效果,举一个实际应用的例子,定义一个TestService和它的实现类TestServiceImpl,里面包含 



两个方法getAllObject()和updateObject(Object Object),具体代码如下 

TestService.java

Java代码 
  1. package com.co.cache.test;
  2. import java.util.List;
  3. public interface TestService {
  4. public List getAllObject();
  5. public void updateObject(Object Object);
  6. }
[java] view
plain
copy

  1. package com.co.cache.test;
  2. import java.util.List;
  3. public interface TestService {
  4. public List getAllObject();
  5. public void updateObject(Object Object);
  6. }

TestServiceImpl.java

Java代码 
  1. package com.co.cache.test;
  2. import java.util.List;
  3. public class TestServiceImpl implements TestService
  4. {
  5. public List getAllObject() {
  6. System.out.println("---TestService:Cache内不存在该element,查找并放入Cache!");
  7. return null;
  8. }
  9. public void updateObject(Object Object) {
  10. System.out.println("---TestService:更新了对象,这个Class产生的cache都将被remove!");
  11. }
  12. }
[java] view
plain
copy

  1. package com.co.cache.test;
  2. import java.util.List;
  3. public class TestServiceImpl implements TestService
  4. {
  5. public List getAllObject() {
  6. System.out.println("---TestService:Cache内不存在该element,查找并放入Cache!");
  7. return null;
  8. }
  9. public void updateObject(Object Object) {
  10. System.out.println("---TestService:更新了对象,这个Class产生的cache都将被remove!");
  11. }
  12. }

使用Spring提供的AOP进行配置 

applicationContext.xml

Java代码 
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
  3. <beans>
  4. <import resource="cacheContext.xml"/>
  5. <bean id="testServiceTarget" class="com.co.cache.test.TestServiceImpl"/>
  6. <bean id="testService" class="org.springframework.aop.framework.ProxyFactoryBean">
  7. <property name="target">
  8. <ref local="testServiceTarget"/>
  9. </property>
  10. <property name="interceptorNames">
  11. <list>
  12. <value>methodCachePointCut</value>
  13. <value>methodCachePointCutAdvice</value>
  14. </list>
  15. </property>
  16. </bean>
  17. </beans>
[java] view
plain
copy

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE beans PUBLIC "-//SPRING//DTD BEAN//EN" "http://www.springframework.org/dtd/spring-beans.dtd">
  3. <beans>
  4. <import resource="cacheContext.xml"/>
  5. <bean id="testServiceTarget" class="com.co.cache.test.TestServiceImpl"/>
  6. <bean id="testService" class="org.springframework.aop.framework.ProxyFactoryBean">
  7. <property name="target">
  8. <ref local="testServiceTarget"/>
  9. </property>
  10. <property name="interceptorNames">
  11. <list>
  12. <value>methodCachePointCut</value>
  13. <value>methodCachePointCutAdvice</value>
  14. </list>
  15. </property>
  16. </bean>
  17. </beans>

这里一定不能忘记import cacheContext.xml文件,不然定义的两个拦截器就没办法使用了。 



最后,写一个测试的代码 

MainTest.java

Java代码 
  1. package com.co.cache.test;
  2. import org.springframework.context.ApplicationContext;
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;
  4. public class MainTest{
  5. public static void main(String args[]){
  6. String DEFAULT_CONTEXT_FILE = "/applicationContext.xml";
  7. ApplicationContext context =  new ClassPathXmlApplicationContext(DEFAULT_CONTEXT_FILE);
  8. TestService testService = (TestService)context.getBean("testService");
  9. System.out.println("1--第一次查找并创建cache");
  10. testService.getAllObject();
  11. System.out.println("2--在cache中查找");
  12. testService.getAllObject();
  13. System.out.println("3--remove cache");
  14. testService.updateObject(null);
  15. System.out.println("4--需要重新查找并创建cache");
  16. testService.getAllObject();
  17. }
  18. }
[java] view
plain
copy

  1. package com.co.cache.test;
  2. import org.springframework.context.ApplicationContext;
  3. import org.springframework.context.support.ClassPathXmlApplicationContext;
  4. public class MainTest{
  5. public static void main(String args[]){
  6. String DEFAULT_CONTEXT_FILE = "/applicationContext.xml";
  7. ApplicationContext context =  new ClassPathXmlApplicationContext(DEFAULT_CONTEXT_FILE);
  8. TestService testService = (TestService)context.getBean("testService");
  9. System.out.println("1--第一次查找并创建cache");
  10. testService.getAllObject();
  11. System.out.println("2--在cache中查找");
  12. testService.getAllObject();
  13. System.out.println("3--remove cache");
  14. testService.updateObject(null);
  15. System.out.println("4--需要重新查找并创建cache");
  16. testService.getAllObject();
  17. }
  18. }

运行,结果如下

Java代码 
  1. 1--第一次查找并创建cache
  2. ---TestService:Cache内不存在该element,查找并放入Cache!
  3. 2--在cache中查找
  4. 3--remove cache
  5. ---TestService:更新了对象,这个Class产生的cache都将被remove!
  6. 4--需要重新查找并创建cache
  7. ---TestService:Cache内不存在该element,查找并放入Cache!
[java] view
plain
copy

  1. 1--第一次查找并创建cache
  2. ---TestService:Cache内不存在该element,查找并放入Cache!
  3. 2--在cache中查找
  4. 3--remove cache
  5. ---TestService:更新了对象,这个Class产生的cache都将被remove!
  6. 4--需要重新查找并创建cache
  7. ---TestService:Cache内不存在该element,查找并放入Cache!

大功告成 .可以看到,第一步执行getAllObject(),执行TestServiceImpl内的方法,并创建了cache,在第二次执行getAllObject()方法时,由于cache有该方法的缓存,直接从cache中get出方法的结果,所以没有打印出TestServiceImpl中的内容,而第三步,调用了updateObject方法,和TestServiceImpl相关的cache被remove,所以在第四步执行时,又执行TestServiceImpl中的方法,创建Cache。 



网上也有不少类似的例子,但是很多都不是很完备,自己参考了一些例子的代码,其实在spring-modules中也提供了对几种cache的支持,ehCache,OSCache,JBossCache这些,看了一下,基本上都是采用类似的方式,只不过封装的更完善一些,主要思路也还是Spring的AOP,有兴趣的可以研究一下。

Spring + EHcache配置的更多相关文章

  1. 转Spring+Hibernate+EHcache配置(三)

    配置每一项的详细作用不再详细解释,有兴趣的请google下 ,这里需要注意一点defaultCache标签定义了一个默认的Cache,这个Cache是不能删除的,否则会抛出No default cac ...

  2. 在 JPA、Hibernate 和 Spring 中配置 Ehcache 缓存

    jpa, hibernate 和 spring 时配置 ehcache 二级缓存的步骤. 缓存配置 首先在 persistence.xml 配置文件中添加下面内容: <property name ...

  3. spring中配置缓存—ehcache

    常用的缓存工具有ehcache.memcache和redis,这里介绍spring中ehcache的配置. 1.在pom添加依赖: <!-- ehcache 相关依赖 --> <de ...

  4. 转载:Spring+EhCache缓存实例

    转载来自:http://www.cnblogs.com/mxmbk/articles/5162813.html 一.ehcahe的介绍 EhCache 是一个纯Java的进程内缓存框架,具有快速.精干 ...

  5. Spring+EhCache缓存实例

    一.ehcahe的介绍 EhCache 是一个纯Java的进程内缓存框架,具有快速.精干等特点,是Hibernate中默认的CacheProvider.Ehcache是一种广泛使用的开源Java分布式 ...

  6. springMVC用法 以及一个简单的基于springMVC hibernate spring的配置

    替代struts 1  web.xml中配置springmvc中央控制器 <?xml version="1.0" encoding="UTF-8"?> ...

  7. Spring+EhCache缓存实例(详细讲解+源码下载)(转)

    一.ehcahe的介绍 EhCache 是一个纯Java的进程内缓存框架,具有快速.精干等特点,是Hibernate中默认的CacheProvider.Ehcache是一种广泛使用的开源Java分布式 ...

  8. Hibernate4+EhCache配置二级缓存

    本文主要讲一讲Hibernate+EhCache配置二级缓存的基本使用方法 (有关EhCache的基础介绍可参见:http://sjsky.iteye.com/blog/1288257 ) Cache ...

  9. Spring+EhCache缓存实例(详细讲解+源码下载)

    一.ehcahe的介绍 EhCache 是一个纯Java的进程内缓存框架,具有快速.精干等特点,是Hibernate中默认的CacheProvider.Ehcache是一种广泛使用的开源Java分布式 ...

  10. Spring+Ehcache

    这里记录一下Spring+Ehcache的结合使用 1.添加依赖 <dependency> <groupId>org.springframework</groupId&g ...

随机推荐

  1. Python自动复制Excel数据:将各行分别重复指定次数

      本文介绍基于Python语言,读取Excel表格文件数据,并将其中符合我们特定要求的那一行加以复制指定的次数,而不符合要求的那一行则不复制:并将所得结果保存为新的Excel表格文件的方法.   这 ...

  2. 安卓系统使用chrome插件(以yandex安装油猴为例)

    以tampermonkey为代表的Chrome插件广受好评,但由于Chrome在安卓系统并不支持令人遗憾.所以带来安卓手机使用Chrome插件的教程. 一,首先下载安卓开源浏览器(个人推荐yandex ...

  3. CSS & JS Effect – Virtual Scrolling

    前言 我正在写 Angular CDK Scrolling 教程,它里面有一个 Virtual Scrolling 功能.借此机会,我想顺便写一篇纯 Sass & TS 的版本作为学习. Vi ...

  4. ASP.NET Core – Web API JSON Patch

    前言 依据 Restful 的方式, 修改 resource 要用 PUT, 然后把完整的 resource 发出去, resource 的所有信息都将被更新. 但很多时候我们希望只做局部更新, 而且 ...

  5. manim边学边做--空心多边形

    空心的多边形Cutout是一种比较特殊的多边形,主要用于解决与形状.大小.位置等相关的数学问题. Cutout多边形可以定义物体表面的空洞或凹陷部分,从而更准确地模拟现实世界中的复杂形状. 比如,在P ...

  6. Kubernetes集群证书过期解决办法

    问题现象 K8S集群证书过期后,会导无法创建Pod,通过kubectl get nodes也无法获取信息,甚至dashboard也无法访问. 一.确认K8S证书过期时间 查看k8s某一证书过期时间: ...

  7. foobar2000 v2.1.3 汉化版(更新日期:2024.04.02)

    foobar2000 v2.1.3 汉化版 -----------------------[软件截图]---------------------- -----------------------[软件 ...

  8. php获取支付宝用户信息

    php获取支付宝用户信息 一:创建应用 要在您的应用中使用支付宝开放产品的接口能力: 您需要先去蚂蚁金服开放平台(open.alipay.com),在开发者中心创建登记您的应用,此时您将获得应用唯一标 ...

  9. element的图片上传预处理函数

    /** 图片格式和大小的控制 */ beforeAvatarUpload (file) { // 允许上传 jpg 和 png 格式的图片 const isJPG = file.type === &q ...

  10. yarn : 无法加载文件 C:\Users\zhulo\AppData\Roaming\npm\yarn.ps1,因为在此系统上禁止运行脚本。有关详细信息,请参阅 https:/go.microsoft.com/fwlink/?Li nkID=135170 中的 about_Execution_Policies。 所在位置 行:1 字符: 1 + yarn serve

    powershell的执行策略问题: 解决办法: 管理员身份打开powershell 输入  set-ExecutionPolicy RemoteSigned  然后选择 a or  Y :