Implement Cache in Azure Service Fabric

与世无争的帅哥 提交于 2019-12-07 18:41:15

问题


I am working on a project in which I need a set of data frequently and currently for getting that data I have to make call to 3rd party Service which is taking lot of time.So what I want is to maintain a local cache.The Data is modified very infrequently or is almost constant.What is the best way of implementing this in Azure Service Fabric.I am currently thinking of making the Microservice stateful. Is is the best way to do this?When node goes down it should copy its local cache to other node.If making it stateful is good than How should i go on implementing this?


回答1:


Well, you have two options: If you need performance and geografical cache data replication, you can use a redis cache.

The other option is use reliable dictionary's. It's a service fabric feature, and reliable dictionary's are replicated to other nodes.

You can only access the reliable dictionary in a service fabric statefull context.

Example bellow:

IReliableDictionary<string, string> Dictionary = await this.StateManager.GetOrAddAsync<IReliableDictionary<string, string>>("stringlistcache");
    using (var tx = this.StateManager.CreateTransaction())
    {   
        await pingDictionary.AddOrUpdateAsync(tx, "testkey", "testvalue", (key, value) => "testvalue");
        cachedValue = await Dictionary.TryGetValueAsync(tx, "testkey");
        await Dictionary.TryRemoveAsync(tx, "testkey");
        await tx.CommitAsync();     
    }



回答2:


I know this post is quite old, but for those looking for a solution today, you can use this open source project:

http://service-fabric-distributed-cache.socreate.it

you can also find it on GitHub here: https://github.com/SoCreate/service-fabric-distributed-cache



来源:https://stackoverflow.com/questions/44699720/implement-cache-in-azure-service-fabric

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!