1024programmer Blog spring boot access ehcache_spring ehcache access_This person must be extraordinary blog with strange muscles and bones

spring boot access ehcache_spring ehcache access_This person must be extraordinary blog with strange muscles and bones

1. Add pom dependencies
dependency
    groupIdorg.springframework</groupId
    artifactIdspring-context-support</artifactId
 </dependency>
 dependency>
    groupIdnet.sf.ehcache</groupId
    artifactIdehcache</artifactId
    version2.8.3</version
 </dependency

 
2. Configure ehcahe.xml file

 
<?xml version="1.0" encoding="UTF-8"?>
 ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-  instance"
  xsi:noNamespaceSchemaLocation="http://ehcache.org/  ehcache.xsd"
  updateCheckmaxElementsInMemory="  1000"
  overflowToDisk="false"
  diskPersistent="false"
  timeToIdleSeconds="0"
  timeToLiveSeconds="600"
  memoryStoreEvictionPolicy="LRU"   />

     cache
  name="demo"
  eternal="false"
  maxElementsInMemory="100"
  overflowToDisk="false"
  diskPersistent="false"
  timeToIdleSeconds="0"
  timeToLiveSeconds="300"
  memoryStoreEvictionPolicy="LRU"   />

 </ehcache

3. Add configuration class:  
@Configuration
 @EnableCaching
 public class EhCacheConfiguration {

     /*
  * ehcache main manager
  */
  @Bean
  public EhCacheCacheManager ehCacheCacheManager(EhCacheManagerFactoryBean bean){
         return new EhCacheCacheManager (bean. getObject ());
     }

     /*
  * According to the setting of shared or not, Spring creates an ehcache base through CacheManager.create() or new CacheManager() respectively.
  */
  @Bean
  public EhCacheManagerFactoryBean ehCacheManagerFactoryBean(){
         EhCacheManagerFactoryBean cacheManagerFactoryBean = new EhCacheManagerFactoryBean ();
         cacheManagerFactoryBean.setConfigLocation (new ClassPathResource("cache/ehcache.xml"  ));
         cacheManagerFactoryBean.setShared (true);
         return cacheManagerFactoryBean;
     }
 }


 
4. Use
@Cacheable

@CachePut


 
@Service
 public class CacheService {

     @Cacheable(value = "demo",key="#methodName+#argus")
     public Object getResult(String methodName,String argus,ProceedingJoinPoint thisJoinPoint){
         try {
             System.out.printf("  Method Execution...");
             Object result = thisJoinPoint. proceed();
             return result;
         } catch (Throwable throwable) {
             throwable. printStackTrace();
         }
         return null;
     }
 }


 

color:#000080″>public EhCacheCacheManager ehCacheCacheManager(EhCacheManagerFactoryBean bean){
return new EhCacheCacheManager (bean. getObject ());
}

/*
* According to the setting of shared or not, Spring creates an ehcache base through CacheManager.create() or new CacheManager() respectively.
*/
@Bean
public EhCacheManagerFactoryBean ehCacheManagerFactoryBean(){
EhCacheManagerFactoryBean cacheManagerFactoryBean = new EhCacheManagerFactoryBean ();
cacheManagerFactoryBean.setConfigLocation (new ClassPathResource(“cache/ehcache.xml” ));
cacheManagerFactoryBean.setShared (true);
return cacheManagerFactoryBean;
}
}


 
4. Use
@Cacheable

@CachePut


 
@Service
 public class CacheService {

     @Cacheable(value = "demo",key="#methodName+#argus")
     public Object getResult(String methodName,String argus,ProceedingJoinPoint thisJoinPoint){
         try {
             System.out.printf("  Method Execution...");
             Object result = thisJoinPoint. proceed();
             return result;
         } catch (Throwable throwable) {
             throwable. printStackTrace();
         }
         return null;
     }
 }


 

This article is from the internet and does not represent1024programmerPosition, please indicate the source when reprinting:https://www.1024programmer.com/spring-boot-access-ehcache_spring-ehcache-access_this-person-must-be-extraordinary-blog-with-strange-muscles-and-bones/

author: admin

Previous article
Next article

Leave a Reply

Your email address will not be published. Required fields are marked *

The latest and most comprehensive programming knowledge, all in 1024programmer.com

© 2023 1024programmer - Encyclopedia of Programming Field
Contact Us

Contact us

181-3619-1160

Online consultation: QQ交谈

E-mail: [email protected]

Working hours: Monday to Friday, 9:00-17:30, holidays off

Follow wechat
Scan wechat and follow us

Scan wechat and follow us

首页
微信
电话
搜索