Redis 服务器并与多个客户端连接
本文关键字:客户端 连接 服务器 Redis | 更新日期: 2023-09-27 18:37:02
我在Windows 2008机器上运行了一个Redis服务器。 我有多个网站,每个网站都使用 BasicRedisClientManager.
此实例在应用启动时设置一次,并设置到Application[]
存储中。这样,它可以在所有用户中重复使用。我遇到的问题是,一个站点在专用服务器上运行,而其他站点在运行 Redis 服务器的本地主机上运行。一个是生产,另一个是新工作/演示的测试地点。远程客户端工作正常,但本地主机客户端不会与服务器通信,并且似乎处于锁定/死状态。当我运行站点时,我只是得到堆栈溢出,并且站点会自行回收。 下面是我拥有的设置的一些示例代码。我看到有一些关于在客户端之间创建锁的信息,但我不确定这是否是我需要采取的路线。
http://www.servicestack.net/docs/redis-client/distributed-locking-with-redis
示例代码:
protected override void Application_Start(object sender, EventArgs e)
{
/*
* Create basicRedisClientManager for redis.
*/
var redisHost = ConfigurationManager.AppSettings["RedisHostIp"].ToString();
BasicRedisClientManager basicRedisClientManager = new BasicRedisClientManager(redisHost);
Application["BasicRedisClientManager"] = basicRedisClientManager;
.....
}
然后在类构造函数中使用
public CacheManager()
{
if (Application["BasicRedisClientManager"] != null)
{
/*local variable*/
basicRedisClientManager = (BasicRedisClientManager)Application["BasicRedisClientManager"];
}
}
在课堂上使用管理器。
if (basicRedisClientManager != null)
{
using (var redisClient = (RedisClient)basicRedisClientManager.GetClient())
{
if (!saveToInProcOnly && redisClient != null)
{
using (var redis = redisClient.GetTypedClient<T>())
{
redis.SetEntry(key, value);
}
}
}
}
此逻辑在所有站点中都是相同的。
任何见解都非常感谢!
>我不知道BasicRedisClientManager
但我建议你使用BookSleeve客户端库。关于连接重用,答案是不要重用。也就是说,每次需要时都重新创建连接,正如BookSleeve的作者所建议的那样。
请参阅此问题和接受的答案,了解如何创建防弹 Redis 客户端连接。