1
创造一次根据这个指南: https://github.com/mspnp/azure-guidance/blob/master/Retry-Service-Specific.md如何访问一个变量,我只需要在Application.Start
他们说:
注意,StackExchange.Redis客户通过单一连接使用复用。建议的用法是在应用程序启动上创建客户端的实例,并针对缓存使用此实例进行所有操作。出于这个原因,与缓存的连接只进行一次,因此本节中的所有指导都与此初始连接的重试策略相关,而不是针对访问缓存的每个操作。
现在,我有这样的事情:
public static Models.UserProfile GetUserProfile(string identityname)
{
/// It needs to be cached for every user because every user can have different modules enabled.
try
{
var cachekeyname = "UserProfileInformation|" + identityname;
IDatabase cache = CacheConnectionHelper.Connection.GetDatabase();
Models.UserProfile userProfile = new Models.UserProfile();
object obj = cache.Get(cachekeyname);
我可以移动的连接线的Global.asax
protected void Application_Start()
{
IDatabase cache = CacheConnectionHelper.Connection.GetDatabase();
}
如果我提出这条线,那么如何才能得到该实例上我需要使用它的其他方法?
这是缓存连接助手
public class CacheConnectionHelper
{
private static Lazy<ConnectionMultiplexer> lazyConnection = new Lazy<ConnectionMultiplexer>(() =>
{
return ConnectionMultiplexer.Connect(SettingsHelper.AzureRedisCache);
});
public static ConnectionMultiplexer Connection
{
get
{
return lazyConnection.Value;
}
}
}