Solon v2.7.5

五、缓存框架使用和定制

</> markdown

solon.data 插件在完成 @Tran 注解的支持同时,还提供了 @Cache、@CachePut、@CacheRemove 注解的支持;可以为业务开发提供良好的便利性

Solon 的缓存注解只支持:@Controller 、@Remoting 、@Component 注解类下的方法。相对于 Spring Boot ,功能类似;但提供了基于 key 和 tags 的两套管理方案。

  • key :相当于缓存的唯一标识,没有指定时会自动生成(要注意key冲突)
  • tags:相当于缓存的索引,可以有多个(用于批量删除)

1、示例

从Demo开始,先感受一把

@Controller
public class CacheController {
    /**
     * 执行结果缓存10秒,使用 key=test:${label} 并添加 test 标签
     * */
    @Cache(key="test:${label}", tags = "test" , seconds = 10)
    @Mapping("/cache/")
    public Object test(int label) {
        return new Date();
    }

    /**
     * 执行后,清除 标签为 test  的所有缓存
     * */
    @CacheRemove(tags = "test")
    @Mapping("/cache/clear")
    public String clear() {
        return "清除成功(其实无效)-" + new Date();
    }

    /**
     * 执行后,更新 key=test:${label}  的缓存
     * */
    @CachePut(key = "test:${label}")
    @Mapping("/cache/clear2")
    public Object clear2(int label) {
        return new Date();
    }
}

2、缓存 key 的模板支持

@Controller
public class CacheController {
    //使用入参
    @Cache(key="test:${label}", seconds = 10)
    @Mapping("/demo1/")
    public Object demo1(int label) {
        return new Date();
    }
    
    //使用入参的属性
    @Cache(key="test:${user.userId}:${map.label}",  seconds = 10)
    @Mapping("/demo2/")
    public Object demo2(UserDto user, Map<String,String> map) {
        return new Date();
    }
    
    //使用返回值
    @Cache(key="test:${.label}", seconds = 10)
    @Mapping("/demo1/")
    public Object demo1() {
        Map map = new HashMap();
        map.puth("label",1);
        return map;
    }
}

3、注解说明

@Cache 注解:

属性说明
service()缓存服务
seconds()缓存时间
key()缓存唯一标识
tags()缓存标签,多个以逗号隔开(为当前缓存块添加标签,用于清除)

@CachePut 注解:

属性说明
service()缓存服务
seconds()缓存时间
key()缓存唯一标识
tags()缓存标签,多个以逗号隔开(为当前缓存块添加标签,用于清除)

@CacheRemove 注解:

属性说明
service()缓存服务
keys()缓存唯一标识,多个以逗号隔开
tags()缓存标签,多个以逗号隔开(方便清除一批key)

4、定制分布式缓存

Solon 的缓存标签,是通过CacheService接口提供支持的。定制起来也相当的方便,比如:对接Memcached...

a. 了解 CacheService 接口:

public interface CacheService {
    //保存
    void store(String key, Object obj, int seconds);

    //获取
    Object get(String key);

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

b. 定制基于 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);
        }
    }
}

c. 通过配置换掉默认的缓存服务:

@Configuration
public class Config {
    //此缓存,将替代默认的缓存服务
    @Bean
    public CacheService cache(@Inject("${cache}") Properties props) {
        return new MemCacheService(props);
    }
}