• ehcache 缓存使用


    1配置类

    @Configuration

    @EnableCaching
    public class EhcacheConfig {
    @Bean(name = "appEhCacheCacheManager")
    public EhCacheCacheManager ehCacheCacheManager(){
    EhCacheManagerFactoryBean bean=ehCacheManagerFactoryBean();
    return new EhCacheCacheManager (bean.getObject ());
    }
    @Bean
    public EhCacheManagerFactoryBean ehCacheManagerFactoryBean(){
    EhCacheManagerFactoryBean cacheManagerFactoryBean = new EhCacheManagerFactoryBean ();
    cacheManagerFactoryBean.setConfigLocation (new ClassPathResource("ehcache.xml"));
    cacheManagerFactoryBean.setShared (true);
    return cacheManagerFactoryBean;
    }
    }

    2调用service层
    @Controller
    @RequestMapping("/home/main")
    public class HomeController {
    @Autowired
    HelloService helloService;
    @RequestMapping("/show")
    @ResponseBody
    public String showHello(){
    System.out.println(helloService.cacheService("1"));
    System.out.println(helloService.cacheService("2"));
    System.out.println(helloService.cacheService("1"));
    System.out.println(helloService.cacheService("2"));
    return rtn;

    }
    }
    3service层方法加上@cacheable注解
    @Service
    public class HelloService {
    @Cacheable(value = "helloworld", key="#id")
    public String cacheService(String id){
    System.out.println("excuteing blllaaaaaa");
    return id+"_value";
    }
    }
    4运行结果

    excuteing blllaaaaaa
    1_value
    excuteing blllaaaaaa
    2_value
    1_value
    2_value

     
  • 相关阅读:
    Python列表、元组、字典、集合的方法
    Python字符串方法总结
    进程、线程、协和的对比
    案例:图片下载器
    使用gevent实现多任务
    使用greenle完成多任务
    使用yield完成多任务
    生成器总结
    生成器send函数
    生成器yield关键字
  • 原文地址:https://www.cnblogs.com/cindy-zhu/p/7146965.html
Copyright © 2020-2023  润新知