博客
关于我
业务逻辑层缓存应该设计
阅读量:453 次
发布时间:2019-03-06

本文共 3176 字,大约阅读时间需要 10 分钟。

在业务制定时很少会切入缓存设计,这通常是一种过渡设计。毕竟,在指标不明确的情况下,这属于一种过渡设计。然而,缓存切入有很多手段,在很多时候直接在WEB进行页面缓存就能带来非常高的收益效果。缓存是一种横向的数据处理应用,通常在设计中引入AOP、ICO等应用组件都可以后期切入添加。但在没有比较丰富的经验的情况下引入AOP、ICO会直接增加应用的复杂度和风险。因此,在设计时引入一种简单的缓存方式,而不带来复杂的工作成本,是一种更优的选择。

一个简单示例

以下是一个不考虑缓存应用的常见开发方式:

public class BlogService : Interfaces.IBlogService{    public IList
List(string category, int size, int index, out int pages) { Expression exp = new Expression(); if (!string.IsNullOrEmpty(category)) exp &= Blog.iD == BlogLinkCategory.blog[BlogLinkCategory.category == category]; int count = exp.Count(); pages = count / size; if (count % size > 0) pages++; return exp.List(new Region(index, size), Blog.createTime.Desc); } public IList
ListCategories() { return new Expression().List(); } public Blog Get(string id) { return (Blog.iD == id).ListFirst(); }}

简单加入缓存

以List方法为例,加入缓存处理方式:

public IList
List(string category, int size, int index, out int pages){ var result = redis.Get("key"); if (result == null) { Expression exp = new Expression(); if (!string.IsNullOrEmpty(category)) exp &= Blog.iD == BlogLinkCategory.blog[BlogLinkCategory.category == category]; int count = exp.Count(); pages = count / size; if (count % size > 0) pages++; result = exp.List(new Region(index, size), Blog.createTime.Desc); redis.Set("key", result); } return result;}

这种方式在开发中比较常见,但在前期阶段没有性能要求时会增加较大的工作量。此外,缓存产品的偶合性可能达不到理想的效果,因为开发人员还需要学习具体缓存产品的API。

缓存实现的解偶

可以在设计时制定一个简单的缓存存取接口:

public interface ICached{    T Get(Func
); void Set(string key, object data);}

在设计时引入到接口定义中:

public interface IBlogService{    IList
List(string category, int size, int index, out int pages); Blog Get(string id); Blog Save(string id, string title, string keywords, string data, params string[] categories); IList
ListCategories(); ICached Cached { get; set; }}

通过这种方式,可以在逻辑层或其他层面引入缓存设计,而不会对整个代码带来较大的复杂度变化。刚开始可以引用一个完全不做任何实现的ICached,待有需要时再更换具体实现。

缓存应用简化

可以在接口中定义一些简单的委托参数,使得缓存在应用时的复用度更高:

public interface ICached{    T Get(Func
); void Set(string key, object data);}

在编写逻辑时就可以简化为:

public IList
List(string category, int size, int index, out int pages){ var cached = Cached.Get(() => { var result = new ExpandoObject(); Expression exp = new Expression(); if (!string.IsNullOrEmpty(category)) exp &= Blog.iD == BlogLinkCategory.blog[BlogLinkCategory.category == category]; int count = exp.Count(); result.Pages = count / size; if (count % size > 0) result.Pages++; result.Blogs = exp.List(new Region(index, size), Blog.createTime.Desc); return result; }, "ListKey"); pages = cached.Pages; return cached.Blogs;}

这种方式的原理很简单:Get方法内部会检查是否有对应的key,如果存在则直接返回缓存数据,否则执行Func方法获取数据并设置到缓存中。这种方式适用于处理一些条件和分页返回等场景,代码量相对较少。

总结

通过这种设计方式,在逻辑层或其他层面引入缓存设计不会对整个代码带来较大的复杂度变化。刚开始可以引用一个完全不做任何实现的ICached,待有需要时再更换具体实现。这种设计方式的优点在于降低了开发人员对缓存产品依赖的程度,但在实际业务场景中可能需要更多的参数和配置。总之,通过这种方式可以在不影响现有功能的前提下,灵活地引入缓存设计。

转载地址:http://gzxfz.baihongyu.com/

你可能感兴趣的文章
Python基础学习参考(五):字符串和编码
查看>>
Python基础学习
查看>>
python基础变量之---集合
查看>>
python基础变量之---字典
查看>>
python基础变量之---列表
查看>>
python基础变量之---元组
查看>>
python基础内容_Python基础入门必须了解的
查看>>
Python基础之面向对象2(封装)
查看>>
Python在def函数中使用for循环
查看>>
Python图像处理:从内存加载jpeg
查看>>
python固定后缀(名物化词汇)词频统计:抽取+统计+可视化
查看>>
python商品数据分析可视化系统(带爬虫)京东销售数据分析 计算机毕业设计 源码下载
查看>>
python商品库存管理系统 django框架 商品网站 MySQL数据库 源码下载 计算机毕业设计
查看>>
Python哪个版本最稳定好用2023.10.19
查看>>
python命名空间更改_Python模块xml.etree.ElementTree自动修改xml命名空间键
查看>>
Python命名中尾随下划线有什么好处?
查看>>
Python启动脚本
查看>>
python吊打Excel?屁!那是你不会用!
查看>>
Python合并两张图片 | 先叠透明度再合并 (附Demo)
查看>>
python各进制的表述与转换
查看>>