Csharp/C#教程:Azure Redis缓存 – ConnectionMultiplexer对象池分享


Azure Redis缓存 – ConnectionMultiplexer对象池

我们在我们的应用程序中使用C1 Azure Redis Cache。 最近我们在GET操作上遇到了很多时间。

根据这篇文章 ,可能的解决方案之一是实现ConnectionMultiplexer对象池。

另一种可能的解决方案是在客户端中使用ConnectionMultiplexer对象池,并在发送新请求时选择“负载最小”的ConnectionMultiplexer。 这应该可以防止单个超时导致其他请求也超时。

如何使用C#实现一个ConnectionMultiplexer对象池?

编辑:

我最近问的相关问题。

如果您正在使用StackExchange.Redis,根据此github问题 ,您可以使用连接多路复用器对象上的TotalOutstanding属性。

这是我提出的一个实现,它正常工作:

上述就是C#学习教程:Azure Redis缓存 – ConnectionMultiplexer对象池分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!

public static int POOL_SIZE = 100; private static readonly Object lockPookRoundRobin = new Object(); private static Lazy[] lazyConnection = null; //Static initializer to be executed once on the first call private static void InitConnectionPool() { lock (lockPookRoundRobin) { if (lazyConnection == null) { lazyConnection = new Lazy[POOL_SIZE]; } for (int i = 0; i < POOL_SIZE; i++){ if (lazyConnection[i] == null) lazyConnection[i] = new Lazy(() => new Context("YOUR_CONNECTION_STRING", new CachingFramework.Redis.Serializers.JsonSerializer())); } } } private static Context GetLeastLoadedConnection() { //choose the least loaded connection from the pool /* var minValue = lazyConnection.Min((lazyCtx) => lazyCtx.Value.GetConnectionMultiplexer().GetCounters().TotalOutstanding); var lazyContext = lazyConnection.Where((lazyCtx) => lazyCtx.Value.GetConnectionMultiplexer().GetCounters().TotalOutstanding == minValue).First(); */ // UPDATE following @Luke Foust comment below Lazy lazyContext; var loadedLazys = lazyConnection.Where((lazy) => lazy.IsValueCreated); if(loadedLazys.Count()==lazyConnection.Count()){ var minValue = loadedLazys.Min((lazy) => lazy.Value.TotalOutstanding); lazyContext = loadedLazys.Where((lazy) => lazy.Value.TotalOutstanding == minValue).First(); }else{ lazyContext = lazyConnection[loadedLazys.Count()]; } return lazyContext.Value; } private static Context Connection { get { lock (lockPookRoundRobin) { return GetLeastLoadedConnection(); } } } public RedisCacheService() { InitConnectionPool(); } 

本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

ctvol管理联系方式QQ:251552304

本文章地址:https://www.ctvol.com/cdevelopment/936224.html

(0)
上一篇 2021年11月6日
下一篇 2021年11月6日

精彩推荐