StackExchange.Redis 的正确使用方法 [英] The correct way of using StackExchange.Redis

查看:50
本文介绍了StackExchange.Redis 的正确使用方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这个想法是使用更少的连接和更好的性能.连接是否随时会过期?

The idea is to use less connection and better performance. Does the connection expire at any time?

另外一个问题,_redis.GetDatabase() 是否会打开新连接?

And for another question, does _redis.GetDatabase() open new connection?

private static ConnectionMultiplexer _redis;
private static IDatabase _db;

public RedisCacheProvider(string configuration)
{
    if (_redis == null)
        lock (myLock)
            if (_redis == null)
            {
                _redis = ConnectionMultiplexer.Connect(configuration);
                _db = _redis.GetDatabase();
            }
}

public async Task<string> GetString(string key)
{
    string result = null;

    RedisValue val = await _db.StringGetAsync(key);

    if (val.HasValue)
        result = val;

    return result;
}

推荐答案

不,多路复用器不会过期.否 GetDatabase 不会打开新连接.这在 basics.md 中都有介绍 - 特别是:

No, a multiplexer doesn't expire. No GetDatabase doesn't open a new connection. This is all covered in basics.md - in particular:

GetDatabase 返回的对象是廉价的 pass-thru 对象,不需要存储.

The object returned from GetDatabase is a cheap pass-thru object, and does not need to be stored.

这篇关于StackExchange.Redis 的正确使用方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆