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. JSON 数据重复 出现$ref

    JSONArray  类型  如果我们往里面add数据的时候 如果数据相同,那么就会被替换成 $ref:   也就是被简化了 因为数据一样所直接 指向上一条数据 循环引用:当一个对象包含另一个对象时, ...

  2. python3环境搭建(uWSGI+django+nginx+python+MySQL)

    1.系统环境,必要知识 #cat /etc/redhat-release CentOS Linux release (Core) #uname -r -.el7.x86_64 暂时关闭防护墙,关闭se ...

  3. The following untracked working tree files would be overwritten by merge

    git pull的时候遇到这样的问题: The following untracked working tree files would be overwritten by merge balabal ...

  4. 前端笔记之Vue(一)初识SPA和Vue&webpack配置和vue安装&指令

    一.单页面应用(SPA) 1.1 C/S到B/S页面架构的转变 C/S:客户端/服务器(Client/Server)架构的软件. C/S 软件的特点: ① 从window桌面双击打开 ② 更新的时候会 ...

  5. 怎么动态生成js变量

    动态生成全局变量: //简单的用字符串作为变量名 window['hello'] = "hello, world"; alert(hello);   //批量定义 for(var  ...

  6. python中报错"json.decoder.JSONDecodeError: Expecting value:"的解决

    在学习python语言中用json库解析网络数据时,我遇到了两个编译错误:json.decoder.JSONDecodeError: Expecting property name enclosed ...

  7. SVM分类器实现实例

    我正在做一个关于SVM的小项目,在我执行验证SVM训练后的模型的时候,得到的report分数总是很高,无论是召回率(查全率).精准度.还是f1-score都很高: 图1 分类器分数report 但是, ...

  8. Linux-误删apt-get以及把aptitude换回

    误删apt-get拯救我的linux 一.前言 先来说一下apt-get, 这个我们使用linux过程中最常用的命令之一. apt-get是一条linux命令,适用于deb包管理式的操作系统,主要用于 ...

  9. win10安装gitLab

    从控制面板选择hyper-V进行安装 1.打开控制面板选择程序=>选择启用或关闭windows功能=>选择Hyper-v 安装ubuntu 1.下载ubuntu系统(本次安装为18.04. ...

  10. Android之OkHttp详解

    文章大纲 一.OkHttp简介二.OkHttp简单使用三.OkHttp封装四.项目源码下载   一.OkHttp简介 1. 什么是OkHttp   一般在Java平台上,我们会使用Apache Htt ...