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. Ubuntu 18 安装chrome

    1.下载chrome文件 32位使用如下命令 wget https://dl.google.com/linux/direct/google-chrome-stable_current_i386.deb ...

  2. ArrayList的add(E e)方法与扩容

    ArrayList是Java开发中经常用到的集合类,它是List接口的实现类,具有很高的查询性能,但不是线程安全的.本文主要讲述了ArrayList的add(E e)方法及该方法中涉及到的容量扩容技术 ...

  3. MIPCache 域名升级

    一.MIPCache URL 是什么 举个例子,MIP 官网的 URL 为: https://www.mipengine.org 对应的 MIPCache 的 URL 为: https://mipca ...

  4. 在 EFCore 定义的实体中进行 FreeSql 开发

    EFCore 和 FreeSql 都是 ORM,在各自领域都有着独特的优势. 问题起源 假设某项目是使用 EFCore 开发的,且实体 特性或FluentApi 都配置好了,如: protected ...

  5. sql自动生成golang结构体struct实体类

    废话不多说直接上地址 使用地址 http://www.linkinstars.com:8090/auto-code 项目github https://github.com/LinkinStars/Au ...

  6. Java - 静态代理详讲

    Java - 静态代理详讲 作者 : Stanley 罗昊 [转载请注明出处和署名,谢谢!] 写在前面:*此章内容比较抽象,所以需要结合实际操作进行讲解*                   *需要有 ...

  7. .NET(C#、VB)APP开发——Smobiler平台控件介绍:SliderView控件

    SliderView控件 一.          样式一 我们要实现上图中的效果,需要如下的操作: 从工具栏上的“Smobiler Components”拖动一个SliderView控件到窗体界面上 ...

  8. ArcGIS消除图斑重叠错误

    在生产中,经常会遇见有图斑重叠这种拓扑错误的矢量,大部分情况下,需要人工比对影像处理.但是如果只需要用到这些矢量的形状.面积,可以在ArcMap中用以下方法,快速消除图斑重叠错误,不必手工处理. 如下 ...

  9. 底部导航栏-----FragmentTabHost

    [说明] 1.主界面上添加父容器:FragmentTabHost 属于v4兼容包 需要指定该id为android:id/tabhost,不能修改,表示由android系统来托管这个id. 本身是一个F ...

  10. Windows2008安装组件命令行工具ServerManagerCmd用法介绍

    转自:http://blog.sina.com.cn/s/blog_537de4b5010128al.html Windows2008 安装组件服务等内容比原来复杂的多,用鼠标点来点去,既繁琐也缓慢, ...