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 2724 蒲公英 分块

    分块,预处理出每两个块范围内的众数,然后在暴力枚举块外的进行比较 那么怎么知道每一个数出现的次数呢?离散后,对于每一个数,维护一个动态数组就好了 #include<cstdio> #inc ...

  2. Keepalived + nginx实现高可用性和负载均衡

    在前面的一篇中讲到了Heartbeat作为高可用服务架构的解决方案,今天有试验了一种全新的解决方案,即采用Keepalived来实现这个功能. Keepalived 是一种高性能的服务器高可用或热备解 ...

  3. 【机器学习基础】对 softmax 和 cross-entropy 求导

    目录 符号定义 对 softmax 求导 对 cross-entropy 求导 对 softmax 和 cross-entropy 一起求导 References 在论文中看到对 softmax 和 ...

  4. Python之父重回决策层

    在Guido van Rossum(吉多·范罗苏姆)卸任BDFL(“终身仁慈独裁者”)一职半年多之后,Python社区迎来了新的治理新方案:指导委员会模式,而经过投票Guido van Rossum也 ...

  5. SqlServer 将纯数字的时间转换为DateTime

    由于数据库存的是整个字符串组到一起了,C#代码是这个样子的. public static string time(DateTime dt) { ) ? ) ? ) ? ) ? ) ? " + ...

  6. 初探机器学习之使用百度AI服务实现图片识别与相似图片

    一.百度云AI服务 最近在调研一些云服务平台的AI(人工智能)服务,了解了一下阿里云.腾讯云和百度云.其中,百度云提供了图像识别及图像搜索,而且还细分地提供了相似图片这项服务,比较符合我的需求,且百度 ...

  7. 《HelloGitHub》第 32 期

    公告 新加入了 2 位机器学期的小伙伴负责机器学习专栏.项目的首页增加合作组织一栏,如有开源组织有意合作可以点击联系我. 我们还在路上,不停地前行. <HelloGitHub>第 32 期 ...

  8. 刨根问底:if 后怎么就可以跟对象,变量交换写法是语法糖吗?

    1.万物皆可布尔 一般语言中的 if 语句语法是这样的: if (条件表达式){    执行语句} 而在 Python 中,if 后面不仅可以是条件表达式,还可以是任意对象.例如: my_list = ...

  9. Android版数据结构与算法(二):基于数组的实现ArrayList源码彻底分析

    版权声明:本文出自汪磊的博客,未经作者允许禁止转载. 本片我们分析基础数组的实现--ArrayList,不会分析整个集合的继承体系,这不是本系列文章重点. 源码分析都是基于"安卓版" ...

  10. webapi 跨域问题

    参考:http://www.cnblogs.com/chenxizhang/p/3821703.html 给自己做个笔记 HttpContext.Current.Response.AddHeader( ...