spring 缓存

先上pom




  4.0.0

  spring-about
  spring-about
  1.0-SNAPSHOT
  war

  spring-about Maven Webapp
  
  http://www.example.com

  
    UTF-8
    1.8
    1.8
    1.8
    
    3.1.1.RELEASE
    
    1.6.1
     1.2.17
  

  
    
      junit
      junit
      4.11
      test
    
    
      org.springframework
      spring-webmvc
      ${spring.version}
      
        
          commons-logging
          commons-logging
        
      
    
    
      org.springframework
      spring-jdbc
      ${spring.version}
    
    
    
    
      mysql
      mysql-connector-java
      5.1.48
    
    
    
      com.alibaba
      druid
      1.1.20
    
    
    
      org.codehaus.jackson
      jackson-mapper-asl
      1.9.12
    
    
      cglib
      cglib
      2.2.2
    
    
      org.projectlombok
      lombok
      1.18.10
    
      
          net.sf.ehcache
          ehcache-core
          2.6.6
      
  
  
    
      org.slf4j
      slf4j-nop
      ${slf4j.version}
      runtime
    
  
  

  
    spring-about
  

配置

@Configuration
@EnableCaching(proxyTargetClass = true)
public class CacheConfig implements CachingConfigurer {

    @Value("#{'${cacheManager.names}'.split(',')}")
    private List cacheManagerNames;

    @Bean
    @Override
    public CacheManager cacheManager() {
        SimpleCacheManager cacheManager = new SimpleCacheManager();
        List list = new ArrayList<>();
        for (String name : cacheManagerNames) {
            list.add(new ConcurrentMapCache(name));
        }
        cacheManager.setCaches(list);
        return cacheManager;
    }

    @Bean
    @Override
    public KeyGenerator keyGenerator() {
        return new DefaultKeyGenerator();
    }
}

使用

    @Cacheable(value = "propConfigCache",key = "#root.method.toString()+#key ",condition = "#key==null or ''.equals(#key.trim())")
    @Override
    public Map get(String key) {
        Map map = new HashMap<>();
        map.put("date", new Date());
        return map;
    }

源码地址:https://gitee.com/mijin/selfmvc/tree/master/springmvc-about

你可能感兴趣的:(spring 缓存)