Springboot min -Solon 详解系列文章:
Springboot mini - Solon详解(一)- 快速入门
Springboot mini - Solon详解(二)- Solon的核心
Springboot mini - Solon详解(三)- Solon的web开发
Springboot mini - Solon详解(四)- Solon的事务传播机制
Springboot mini - Solon详解(五)- Solon扩展机制之Solon Plugin
Springboot mini - Solon详解(六)- Solon的校验框架使用、定制与扩展
Springboot mini - Solon详解(七)- Solon Ioc 的注解对比Spring及JSR330
Springboot mini - Solon详解(八)- Solon的缓存框架使用和定制
Springboot mini - Solon详解(九)- 渲染控制之定制统一的接口输出
Springboot mini - Solon详解(十)- 怎么用 Solon 开发基于 undertow jsp tld 的项目?html
solon.extend.data
框加在完成 @Tran 注解的支持同时,还提供了 @Cache、@CachePut、@CacheRemove 注解的支持;能够为业务开发提供良好的便利性java
Solon 的缓存注解只支持:Controller 、Service 、Dao 类下的方法。且借签了Weed3的简洁设计方案。git
从Demo开始,先感觉一把github
@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...web
public interface CacheService { //保存 void store(String key, Object obj, int seconds); //获取 Object get(String key); //移除 void remove(String key); }
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); } } }
@Configuration public class Config { //此缓存,将替代默认的缓存服务 @Bean public CacheService cache(@Inject("${cache}") Properties props) { return new MemCacheService(props); } }
@Cache 注解:缓存
属性 | 说明 |
---|---|
service() | 缓存服务 |
seconds() | 缓存时间 |
tags() | 缓存标签,多个以逗号隔开(为当前缓存块添加标签,用于清除) |
@CachePut 注解:app
属性 | 说明 |
---|---|
service() | 缓存服务 |
seconds() | 缓存时间 |
tags() | 缓存标签,多个以逗号隔开(为当前缓存块添加标签,用于清除) |
@CacheRemove 注解:框架
属性 | 说明 |
---|---|
service() | 缓存服务 |
tags() | 清除缓存标签,多个以逗号隔开 |