1.服务跟客户端初始化的时候需要添加缓存配置

            var host = new ServiceHostBuilder()
.RegisterServices(builder =>
{
builder.AddMicroService(option =>
{
6               option .AddCache()//缓存初始化
28            });
}).Configure(build =>
  build.AddCacheFile("cacheSettings.json", optional: false,reloadOnChange:true))
.UseStartup<Startup>()
.Build();

2.配置文件(服务端跟客户端都需要)

{
"CachingSettings": [
{
"Id": "ddlCache",
"Class": "Surging.Core.Caching.RedisCache.RedisContext,Surging.Core.Caching",
"InitMethod": "",
"Maps": null,
"Properties": [
{
"Name": "appRuleFile",
"Ref": "rule",
"Value": "",
"Maps": null
},
{
"Name": "dataContextPool",
"Ref": "ddls_sample",
"Value": "",
"Maps": [
{
"Name": "Redis",//redis配置
"Properties": [
{
"Name": null,
"Ref": null,
"Value": ":你的密码@你的ip:6379::1",//reids 内存数据库连接字符串传 后面的1 代表你当前连接的是哪个库
"Maps": null
}
]
},
{
"Name": "MemoryCache",//本机内存
"Properties": null
}
]
},
{
"Name": "defaultExpireTime",//默认超时时间
"Ref": "",
"Value": "",
"Maps": null
},
{
"Name": "connectTimeout",//连接超时时间
"Ref": "",
"Value": "",
"Maps": null
},
{
"Name": "minSize",
"Ref": "",
"Value": "",
"Maps": null
},
{
"Name": "maxSize",
"Ref": "",
"Value": "",
"Maps": null
}
]
}
]
}

3.服务端配置

[Command(RequestCacheEnabled = true)]
[InterceptMethod(CachingMethod.Get, Key = "GetUser_id_{0}", CacheSectionType = SectionType.ddlCache, Mode = CacheTargetType.Redis, Time = )]
Task<UserModel> GetUser(UserModel user);

(1)在容错规则里面配置开启缓存

(2)在缓存拦截器里面配置缓存的方法,key,类型,超时时间等等。。

(3)传递的方法参数如果是model类型,就需要设置 [CacheKey(1)]来标识缓存key, 比如传递UserModel,
设置UserId 为1,Name 为fanly, 设置的KEY为GetUserName_name_{1}
那么缓存的key就会生成GetUserName_name_fanly, key 如果设置为GetUserName_id_{0}
那么缓存的key就会生成GetUserName_id_1,传递的方法参数是string,int 类型就不需要设置 [CacheKey(1)]

(4)Remove模式下,移除的缓存是一个真个列表

 public class UserModel
{
[CacheKey()]
public int UserId { get; set; }
[CacheKey()]
public string Name { get; set; }
public int Age { get; set; }
}

4.客户端调用配置

客户端初始化的时候  需要添加.AddClientIntercepted(typeof(CacheProviderInterceptor)),其中CacheProviderInterceptor是作者给我们实现的一个实例,代码如下:

public class CacheProviderInterceptor : CacheInterceptor
{
public override async Task Intercept(ICacheInvocation invocation)
{
var attribute =
invocation.Attributes.Where(p => p is InterceptMethodAttribute)
.Select(p => p as InterceptMethodAttribute).FirstOrDefault();
var cacheKey = invocation.CacheKey == null ? attribute.Key :
string.Format(attribute.Key ?? "", invocation.CacheKey);
await CacheIntercept(attribute, cacheKey, invocation);
} private async Task CacheIntercept(InterceptMethodAttribute attribute, string key, ICacheInvocation invocation)
{
ICacheProvider cacheProvider = null;
switch (attribute.Mode)
{
case CacheTargetType.Redis:
{
cacheProvider = CacheContainer.GetService<ICacheProvider>(string.Format("{0}.{1}",
attribute.CacheSectionType.ToString(), CacheTargetType.Redis.ToString()));
break;
}
case CacheTargetType.MemoryCache:
{
cacheProvider = CacheContainer.GetService<ICacheProvider>(CacheTargetType.MemoryCache.ToString());
break;
}
}
if (cacheProvider != null) await Invoke(cacheProvider, attribute, key, invocation);
} private async Task Invoke(ICacheProvider cacheProvider, InterceptMethodAttribute attribute, string key, ICacheInvocation invocation)
{
switch (attribute.Method)
{
case CachingMethod.Get:
{
var retrunValue = await cacheProvider.GetFromCacheFirst(key, async () =>
{
await invocation.Proceed();
return invocation.ReturnValue;
}, invocation.ReturnType, attribute.Time);
invocation.ReturnValue = retrunValue;
break;
}
default:
{
await invocation.Proceed();
var keys = attribute.CorrespondingKeys.Select(correspondingKey => string.Format(correspondingKey, invocation.CacheKey)).ToList();
keys.ForEach(cacheProvider.RemoveAsync);
break;
}
}
}
}

找到InterceptMethodAttribute 的配置属性根据配置的缓存类型  初始化ICacheProvider接口,这个接口是缓存的一些常用方法,(当然我们也直接可以在代码中或者这个接口的实例,从而在缓存计算一些值)

然后在Invoke方法里面执行缓存的方法

5.其他

关于缓存拦截  我目前的版本是0.7.0.1 是只能在调用代理的时候用使用。因为在代理的时候才会根据容错规则开启缓存开关 来决定执行是否走缓存拦截。新版本的http支持 实现了缓存拦截。所以有需要的小伙伴可以升个级试试看。

关于缓存的连接  也是通过注册中心来检查它的健康状态。

最后运行程序,得到结果

(五)surging 微服务框架使用系列之缓存-reids的更多相关文章

  1. 一)surging 微服务框架使用系列之surging 的准备工作rabbitmq安装(转载 https://www.cnblogs.com/alangur/p/8339905.html)

    (一)surging 微服务框架使用系列之surging 的准备工作rabbitmq安装   (1)下载erlang: http://www.erlang.org/download/otp_win64 ...

  2. 转载 (三)surging 微服务框架使用系列之我的第一个服务(审计日志)

    (三)surging 微服务框架使用系列之我的第一个服务(审计日志)   前言:前面准备了那么久的准备工作,现在终于可以开始构建我们自己的服务了.这篇博客就让我们一起构建自己的第一个服务---审计日志 ...

  3. (三)surging 微服务框架使用系列之我的第一个服务(审计日志)

    前言:前面准备了那么久的准备工作,现在终于可以开始构建我们自己的服务了.这篇博客就让我们一起构建自己的第一个服务---审计日志. 首先我们先创建两个项目,一个控制台的服务启动项目,一个业务的实现项目. ...

  4. (一)surging 微服务框架使用系列之surging 的准备工作rabbitmq安装

    (1)下载erlang: http://www.erlang.org/download/otp_win64_17.3.exe 并安装 (2)下载RabbitMQ: http://www.rabbitm ...

  5. (四)surging 微服务框架使用系列之网关

    一.什么是API网关 API网关是一个服务器,是系统对外的唯一入口.API网关封装了系统内部架构,为每个客户端提供一个定制的API.API网关方式的核心要点是,所有的客户端和消费端都通过统一的网关接入 ...

  6. (二)surging 微服务框架使用系列之surging 的准备工作consul安装

    suging 的注册中心支持consul跟zookeeper.因为consul跟zookeeper的配置都差不多,所以只是consul的配置 consul下载地址:https://www.consul ...

  7. (四)surging 微服务框架使用系列之网关 转载

    一.什么是API网关 API网关是一个服务器,是系统对外的唯一入口.API网关封装了系统内部架构,为每个客户端提供一个定制的API.API网关方式的核心要点是,所有的客户端和消费端都通过统一的网关接入 ...

  8. surging 微服务框架使用系列之surging介绍

    首先,感谢surging的作者fanliang11为.net开源做出的贡献 其次, surging 的git地址:https://github.com/dotnetcore/surging surgi ...

  9. Surging 微服务框架使用入门

    原文:Surging 微服务框架使用入门 前言 本文非 Surging 官方教程,只是自己学习的总结.如有哪里不对,还望指正.  我对 surging 的看法 我目前所在的公司采用架构就是类似与Sur ...

随机推荐

  1. BZOJ_1833_[ZJOI2010]count 数字计数_数位DP

    BZOJ_1833_[ZJOI2010]count 数字计数_数位DP 题意: 给定两个正整数a和b,求在[a,b]中的所有整数中,每个数码(digit)各出现了多少次. 分析: 数位DP f[i][ ...

  2. 【爆料】-《西悉尼大学毕业证书》UWS一模一样原件

    ☞西悉尼大学毕业证书[微/Q:865121257◆WeChat:CC6669834]UC毕业证书/联系人Alice[查看点击百度快照查看][留信网学历认证&博士&硕士&海归&a ...

  3. 聚焦“云开发圆桌论坛”,大前端Serverless大佬们释放了这些讯号!

    4月14日,由云加社区举办的TVP&腾讯云技术交流日云开发专场,暨"腾讯云-云开发圆桌论坛"在北京.深圳两地同步举行. 当天下午,一场主题为"基于大前端和node ...

  4. Visual Studio Code 中文界面设置

    Visual Studio Code 中文界面设置 昨天,想要试一下用 VS Code 写 Markdown 格式的博客,下载下来发现是英文界面: 按照我以前的经验应该会自动提示切换语言的,但是这次等 ...

  5. Python的re模块

    什么是re模块,re模块有什么作用? re模块是Python提供的一个正则表达式相关的模块,主要是针对字符串进行模糊匹配,所以在字符串匹配这一功能上,re相当专业. 什么是模糊匹配? 之前的学习字符串 ...

  6. 文本离散表示(一):词袋模型(bag of words)

    一.文本表示 文本表示的意思是把字词处理成向量或矩阵,以便计算机能进行处理.文本表示是自然语言处理的开始环节. 文本表示按照细粒度划分,一般可分为字级别.词语级别和句子级别的文本表示.字级别(char ...

  7. asp.net core系列 55 IS4使用Identity密码保护API

    一.概述 OAuth 2.0资源(web api)所有者密码授权,允许客户端(Client项目)向令牌服务(IdentityServer项目)发送用户名和密码,并获取代表该用户的访问令牌.在官方文档中 ...

  8. LVS的DR模型配置

    LVS的DR模型配置 介绍 下图为DR模型的通信过程,图中的IP不要被扑结构中的IP迷惑,图里只是为了说明DR的通信原理,应用到本例中的拓扑上其工作原理不变. 拓扑结构 服务器 IP地址 角色 Srv ...

  9. [PHP]引用返回与节省内存

    PHP中的引用是什么:1.在 PHP 中引用意味着用不同的名字访问同一个变量内容2.引用可以被看作是 Unix 文件系统中的硬链接. 3.使用unset的话,只是删除他这个名字自身对内容的引用,并没有 ...

  10. openlayers4 入门开发系列之小区信号扇形图篇

    前言 openlayers4 官网的 api 文档介绍地址 openlayers4 api,里面详细的介绍 openlayers4 各个类的介绍,还有就是在线例子:openlayers4 官网在线例子 ...