2

According to this guide: https://github.com/mspnp/azure-guidance/blob/master/Retry-Service-Specific.md

They say:

Note that the StackExchange.Redis client uses multiplexing through a single connection. The recommended usage is to create an instance of the client at application startup and use this instance for all operations against the cache. For this reason, the connection to the cache is made only once, and so all of the guidance in this section is related to the retry policy for this initial connection—and not for each operation that accesses the cache.

Right now I have something like this:

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);

I can move the connection line to global.asax

protected void Application_Start()
        {

            IDatabase cache = CacheConnectionHelper.Connection.GetDatabase();

        }

If I move that line, then how can I get that instance on the other methods where I need to use it?

this is the cache connection helper

public class CacheConnectionHelper
    {
        private static Lazy<ConnectionMultiplexer> lazyConnection = new Lazy<ConnectionMultiplexer>(() =>
        {
            return ConnectionMultiplexer.Connect(SettingsHelper.AzureRedisCache);
        });

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

AS I dont know much about patterns, maybe this is a singleton? but I have no idea how to implement it!

2

If you declare the variable in the local context of Application_Start() as you have in your example, it is only accessible in that method.

One solution could be to declare the variable as a global variable, outside of the "Application_Start()" procedure.

Example:

public class Global : System.Web.HttpApplication {

  public static IDatabase DBCache;

  protected void Application_Start(object sender, EventArgs e) {
    DBCache = CacheConnectionHelper.Connection.GetDatabase();
  }
}

then you are able to access it from your other classes by calling it like this:

public class test {
  public myFunction() {    
    var mycache = Global.DBCache;
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.