zoukankan      html  css  js  c++  java
  • 57

    一)示例
    从Demo开始,先感受一把

    @Controller
    public class CacheController {
    /**
    * 执行结果缓存10秒,并添加 test_KaTeX parse error: Expected group after '_' at position 53: …he(tags = "test_̲{label},test1" , seconds = 10)
    @Mapping("/cache/")
    public Object test(int label) {
    return new Date();
    }

    /**
     * 执行后,清除 标签为 test  的缓存(不过,目前没有 test 的示签...)
     * */
    @CacheRemove(tags = "test")
    @Mapping("/cache/clear")
    public String clear() {
        return "清除成功(其实无效)-" + new Date();
    }
    
    /**
     * 执行后,清除 标签为 test_${label}  的缓存
     * */
    @CacheRemove(tags = "test_${label}")
    @Mapping("/cache/clear2")
    public String clear2(int label) {
        return "清除成功-" + new Date();
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    }
    (二)定制分布式缓存
    Solon 的缓存标签,是通过CacheService接口提供支持的。定制起来也相当的方便,比如:对接Memcached…

    1. 了解 CacheService 接口:
      public interface CacheService {
      //保存
      void store(String key, Object obj, int seconds);

      //获取
      Object get(String key);

      //移除
      void remove(String key);
      }

    2. 定制基于 Memcached 缓存服务:
      public class MemCacheService implements CacheService{
      private MemcachedClient _cache = null;
      public MemCacheService(Properties props){
      //略…
      }

      @Override
      public void store(String key, Object obj, int seconds) {
      if (_cache != null) {
      _cache.set(key, seconds, obj);
      }
      }

      @Override
      public Object get(String key) {
      if (_cache != null) {
      return _cache.get(key);
      } else {
      return null;
      }
      }

      @Override
      public void remove(String key) {
      if (_cache != null) {
      _cache.delete(key);
      }
      }
      }

    3. 通过配置换掉默认的缓存服务:
      @Configuration
      public class Config {
      //此缓存,将替代默认的缓存服务
      @Bean
      public CacheService cache(@Inject("${cache}") Properties props) {
      return new MemCacheService(props);
      }
      }
      (三)注解说明
      @Cache 注解:

    属性 说明
    service() 缓存服务
    seconds() 缓存时间
    tags() 缓存标签,多个以逗号隔开(为当前缓存块添加标签,用于清除)
    @CachePut 注解:

    属性 说明
    service() 缓存服务
    seconds() 缓存时间
    tags() 缓存标签,多个以逗号隔开(为当前缓存块添加标签,用于清除)
    @CacheRemove 注解:

    属性 说明
    service() 缓存服务
    tags() 清除缓存标签,多个以逗号隔开

  • 相关阅读:
    Redis Cluster笔记
    http协议之 COOKIE
    lor框架代码分析
    PHP--SPL扩展学习笔记
    lua协程----ngx-lua线程学习笔记
    设计模式
    eclipse中使用maven创建springMVC项目
    一般处理程序里使用session对象
    .Dot NET Cored简介
    Spring 依赖注入(基本注入和自动适配注入)
  • 原文地址:https://www.cnblogs.com/gd11/p/14217388.html
Copyright © 2011-2022 走看看