0

我尝试使用 StackExchange.Redis.Extension 库创建 CacheClient 对象。到目前为止,我知道我需要两个对象进行初始化:

_cacheClient = new StackExchangeRedisCacheClient(Serializer,ConnectionString)

序列化程序和 ConnectionString 。

我决定使用ConfigurationOptions.ToString()为 redis 缓存客户端检索 connectionString。我的问题是我不确定应该在这个类(属性)中设置什么值。我有来自 Azure 云的以下数据集:

<add key="RedisHostName"  value="myapp.cache.windows.net" />
<add key="RedisPrimary"   value="UW/ESgtf[...]RZYS="/>

问题:如何将这两项映射到 ConfigurationOptions 类以初始化缓存客户端对象。

4

1 回答 1

1

在查看堆栈类似问题后,我找到了解决方案:

private static ConfigurationOptions _configurationOptions;

public static  ConfigurationOptions ConfigurationOptions
{
    get
    {
        return _configurationOptions ??
               (new ConfigurationOptions()
               {
                   Ssl = true,
                   EndPoints = { { ConfigurationManager.AppSettings.Get("RedisHostName")}},
                   Password  = ConfigurationManager.AppSettings.Get("RedisPrimary"),
                   DefaultVersion = new Version("2.8.5"),
                   AllowAdmin = true,
                   KeepAlive = 180
               });
    }

    set { _configurationOptions = value; }
}

连接字符串符合预期

public static string `ConnectionString => ConfigurationOptions.ToString();

和 CacheClient 连接:

public static ICacheClient CacheClient
        {
            get { return _cacheClient ?? (_cacheClient = new StackExchangeRedisCacheClient(Serializer,ConnectionString));}

            set { _cacheClient = value; }
        }
于 2015-12-06T16:41:05.480 回答