gpt4 book ai didi

c# - StackExchange.Redis 与 Azure Redis 速度慢得无法使用或引发超时错误

转载 作者:IT王子 更新时间:2023-10-29 04:12:57 26 4
gpt4 key购买 nike

我将所有现有的 Azure In-Role 缓存使用转移到 Redis,并决定使用 Azure Redis 预览版和 StackExchange.Redis 库 ( https://github.com/StackExchange/StackExchange.Redis )。我为它编写了所有代码,没有太大问题,但是运行时速度绝对慢得无法使用,并且不断抛出超时错误(我的超时时间设置为 15 秒)。

以下是我如何设置 Redis 连接并使用它进行简单操作的相关代码:

    private static ConnectionMultiplexer _cacheService;
private static IDatabase _database;
private static object _lock = new object();

private void Initialize()
{
if (_cacheService == null)
{
lock (_lock)
{
if (_cacheService == null)
{
var options = new ConfigurationOptions();
options.EndPoints.Add("{my url}", 6380);
options.Ssl = true;
options.Password = "my password";
// needed for FLUSHDB command
options.AllowAdmin = true;

// necessary?
options.KeepAlive = 30;
options.ConnectTimeout = 15000;
options.SyncTimeout = 15000;

int database = 0;

_cacheService = ConnectionMultiplexer.Connect(options);
_database = _cacheService.GetDatabase(database);
}
}
}

}

public void Set(string key, object data, TimeSpan? expiry = null)
{
if (_database != null)
{
_database.Set(key, data, expiry: expiry);
}
}

public object Get(string key)
{
if (_database != null)
{
return _database.Get(key);
}
return null;
}

执行非常简单的命令(例如 Get 和 Set)经常会超时或需要 5-10 秒才能完成。如果它比实际从数据库中获取真实数据慢得多,那么似乎就否定了将其用作缓存的全部目的:)

我做了什么明显不正确的事情吗?

编辑:以下是我从服务器(使用 Redis 桌面管理器)提取的一些统计数据,以防有所启发。

Server
redis_version:2.8.12
redis_mode:standalone
os:Windows
arch_bits:64
multiplexing_api:winsock_IOCP
gcc_version:0.0.0
process_id:2876

tcp_port:6379
uptime_in_seconds:109909
uptime_in_days:1
hz:10
lru_clock:16072421
config_file:C:\Resources\directory\xxxx.Kernel.localStore\1\redis_2092_port6379.conf

Clients
connected_clients:5
client_longest_output_list:0
client_biggest_input_buf:0
client_total_writes_outstanding:0
client_total_sent_bytes_outstanding:0
blocked_clients:0

Memory
used_memory:4256488
used_memory_human:4.06M
used_memory_rss:67108864
used_memory_rss_human:64.00M
used_memory_peak:5469760
used_memory_peak_human:5.22M
used_memory_lua:33792
mem_fragmentation_ratio:15.77
mem_allocator:dlmalloc-2.8

Persistence
loading:0
rdb_changes_since_last_save:72465
rdb_bgsave_in_progress:0
rdb_last_save_time:1408471440
rdb_last_bgsave_status:ok
rdb_last_bgsave_time_sec:-1
rdb_current_bgsave_time_sec:-1
aof_enabled:0
aof_rewrite_in_progress:0
aof_rewrite_scheduled:0
aof_last_rewrite_time_sec:-1
aof_current_rewrite_time_sec:-1
aof_last_bgrewrite_status:ok
aof_last_write_status:ok

Stats
total_connections_received:25266
total_commands_processed:123389
instantaneous_ops_per_sec:10
bytes_received_per_sec:275
bytes_sent_per_sec:65
bytes_received_per_sec_human:

编辑 2:以下是我用于 Get/Set 的扩展方法 - 它们是非常简单的方法,只需将对象转换为 JSON 并调用 StringSet .

    public static object Get(this IDatabase cache, string key)
{
return DeserializeJson<object>(cache.StringGet(key));
}

public static void Set(this IDatabase cache, string key, object value, TimeSpan? expiry = null)
{
cache.StringSet(key, SerializeJson(value), expiry: expiry);
}

编辑 3:以下是一些错误消息示例:

    A first chance exception of type 'System.TimeoutException' occurred in StackExchange.Redis.dll
Timeout performing GET MyCachedList, inst: 11, queue: 1, qu=1, qs=0, qc=0, wr=0/1, in=0/0

A first chance exception of type 'System.TimeoutException' occurred in StackExchange.Redis.dll
Timeout performing GET MyCachedList, inst: 1, queue: 97, qu=0, qs=97, qc=0, wr=0/0, in=3568/0

最佳答案

这是推荐的模式,来自 Azure Redis Cache documentation :

private static Lazy<ConnectionMultiplexer> lazyConnection = new Lazy<ConnectionMultiplexer>(() => {
return ConnectionMultiplexer.Connect("mycache.redis.cache.windows.net,abortConnect=false,ssl=true,password=...");
});

public static ConnectionMultiplexer Connection {
get {
return lazyConnection.Value;
}
}

几个要点:

  • 它使用 Lazy 处理线程安全初始化
  • 它设置“abortConnect=false”,这意味着如果初始连接尝试失败,ConnectionMultiplexer 将在后台静默重试,而不是抛出异常。
  • 检查 IsConnected 属性,因为如果连接断开,ConnectionMultiplexer 会自动在后台重试。

关于c# - StackExchange.Redis 与 Azure Redis 速度慢得无法使用或引发超时错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25416562/

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