gpt4 book ai didi

c# - 如何使 Redis 缓存在部署在 IIS Express 上但在本地主机上工作的 asp.net 核心 Web 上工作

转载 作者:可可西里 更新时间:2023-11-01 11:47:48 25 4
gpt4 key购买 nike

我正在使用“Redis-x64-3.0.504”,我正在尝试在本地 IIS Express 上托管我的 asp.net core 2.2 Web 应用程序,但 Redis 缓存在 IIS 上无法正常运行,因为我似乎卡住了在访问或保存在缓存中时加载。但是 - 当我以本地主机身份运行 Visual Studio 时,它运行良好。

我在 ConfigureServices(IServiceCollection service) 中添加了 Microsoft.Extentions.Caching.StackExchangeRedis (2.2.5) nuget 和代码以进行配置。

代码如下所示:

services.AddStackExchangeRedisCache(options =>
{
options.Configuration = "localhost";
options.InstanceName = "SampleInstance";
});

然后我创建了一个 CacheHelper 类作为访问缓存的方法的泛型:

public static class CacheHelper
{
public static void SaveInCache<T>(IDistributedCache memoryCache, string key, T value)
{
var graphJSON = JsonConvert.SerializeObject(value);
memoryCache.SetStringAsync(key, graphJSON);
}

public static T GetFromCache<T>(IDistributedCache memoryCache, string key)
{
var cacheValue = memoryCache.GetStringAsync(key);
return JsonConvert.DeserializeObject<T>(cacheValue.Result != null ? cacheValue.Result : string.Empty);
}

public static T GetMatchFromCache<T>(IDistributedCache memoryCache, string matchKey)
{
var redis = StackExchange.Redis.ConnectionMultiplexer.Connect("localhost");
var db = redis.GetDatabase();
var serverEndpoints = redis.GetEndPoints()[0];
var keys = redis.GetServer(serverEndpoints).Keys(pattern: "*");

foreach(var key in keys)
{
if (key.ToString().Contains(matchKey))
{
var trimmedKey = key.ToString().Substring(key.ToString().IndexOf(matchKey));
return GetFromCache<T>(memoryCache, trimmedKey);
}
}

return default(T);
}

public static List<T> GetAllMatchFromCache<T>(IDistributedCache memoryCache, string matchKey)
{
List<T> contentList = new List<T>();
var redis = StackExchange.Redis.ConnectionMultiplexer.Connect("localhost");
var db = redis.GetDatabase();
var serverEndpoints = redis.GetEndPoints()[0];
var keys = redis.GetServer(serverEndpoints).Keys(pattern: "*");

foreach (var key in keys)
{
if (key.ToString().Contains(matchKey))
{
var trimmedKey = key.ToString().Substring(key.ToString().IndexOf(matchKey));
var cacheReturn = GetFromCache<T>(memoryCache, trimmedKey);
if (cacheReturn != null)
{
contentList.Add(cacheReturn);
}
}
}

return contentList;
}
}

我很困惑为什么这不能在 IIS 上工作,但在我的本地主机上工作。我怎样才能使它在 IIS 主机上工作呢?

最佳答案

在建立连接时尝试如下操作:

using (var muxer = ConnectionMultiplexer.Connect("localhost,resolvedns=1"))
{
muxer.PreserveAsyncOrder = preserveOrder;
RedisKey key = "MBOA";
var conn = muxer.GetDatabase();
muxer.Wait(conn.PingAsync());

Action<Task> nonTrivial = delegate
{
Thread.SpinWait(5);
};
var watch = Stopwatch.StartNew();
for (int i = 0; i <= AsyncOpsQty; i++)
{
var t = conn.StringSetAsync(key, i);
if (withContinuation) t.ContinueWith(nonTrivial);
}
int val = (int)muxer.Wait(conn.StringGetAsync(key));
watch.Stop();

Console.WriteLine("After {0}: {1}", AsyncOpsQty, val);
Console.WriteLine("({3}, {4})\r\n{2}: Time for {0} ops: {1}ms; ops/s: {5}", AsyncOpsQty, watch.ElapsedMilliseconds, Me(),
withContinuation ? "with continuation" : "no continuation", preserveOrder ? "preserve order" : "any order",
AsyncOpsQty / watch.Elapsed.TotalSeconds);
}

希望对您有所帮助。

关于c# - 如何使 Redis 缓存在部署在 IIS Express 上但在本地主机上工作的 asp.net 核心 Web 上工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56288476/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com