Springboot mini - Solon详解(八)- Solon的缓存框架使用和定制

Solon详解系列文章:
Solon详解(一)- 快速入门
Solon详解(二)- Solon的核心
Solon详解(三)- Solon的web开发
Solon详解(四)- Solon的事务传播机制
Solon详解(五)- Solon扩展机制之Solon Plugin
Solon详解(六)- Solon的校验框架使用、定制与扩展
Solon详解(七)- Solon Ioc 的注解对比Spring及JSR330
Solon详解(八)- Solon的缓存框架使用和定制
Solon详解(九)- 渲染控制之定制统一的接口输出
Solon详解(十)- 怎么用 Solon 开发基于 undertow jsp tld 的项目?

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

Solon 的缓存注解只支持:Controller 、Service 、Dao 类下的方法。且借签了Weed3的简洁设计方案。

(一)示例

从Demo开始,先感受一把

@Controller
public class CacheController {
    
    
    /**
     * 执行结果缓存10秒,并添加 test_${label} 和 test1 标签
     * */
    @Cache(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();
    }
}

(二)定制分布式缓存

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() 清除缓存标签,多个以逗号隔开

附:Solon项目地址

猜你喜欢

转载自blog.csdn.net/cwzb/article/details/108818561
今日推荐