0
votes

I am trying to write records to a Redis cache. But sometimes I get the below exception,

I have no clue why I am getting this error

An error has occurred.","ExceptionMessage":"No connection is available to service this operation: SETEX token; SocketFailure on xxx.redis.cache.windows.net:6380/Subscription, origin: CheckForStaleConnection, input-buffer: 0, outstanding: 4, last-read: 1s ago, last-write: 0s ago, unanswered-write: 1s ago, keep-alive: 60s, pending: 0, state: ConnectedEstablishing, last-heartbeat: never, last-mbeat: -1s ago, global: 1s ago, mgr: RecordConnectionFailed_ReportFailure, err: never; IOCP: (Busy=0,Free=1000,Min=4,Max=1000), WORKER: (Busy=1,Free=8190,Min=4,Max=8191), Local-CPU: n/a","ExceptionType":"StackExchange.Redis.RedisConnectionException

My Connection Multiplexer

private static Lazy<ConnectionMultiplexer> lazyConnection = new Lazy<ConnectionMultiplexer>(() =>
    {
        return ConnectionMultiplexer.Connect(ConfigurationManager.AppSettings["RedisConString"]);
    });


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


public bool Add(string key, string value, int expiryDays)
    {
        IDatabase cache = Connection.GetDatabase();

        cache.StringSet(key, value, TimeSpan.FromDays(expiryDays));

        return true;
    }
1

1 Answers

1
votes

Read here https://stackexchange.github.io/StackExchange.Redis/Configuration.html

I was having same problem, the following connection string worked for me. I added allowAdmin=True to my connection string and it worked. My Redis configuration was set to only allow SSL ports

redis-xxx-dev.redis.cache.windows.net:6380,allowAdmin=True,password=XXXX,ssl=True,abortConnect=False

This is my Redis factory,

public class RedisFactory
    {
        private static readonly Lazy<ConnectionMultiplexer> Connection;

#pragma warning disable CA1810 // Initialize reference type static fields inline
        static RedisFactory()
#pragma warning restore CA1810 // Initialize reference type static fields inline
        {
            var connectionString = ConfigHelper.Instance.RedisCacheConnectionString;
            var options = ConfigurationOptions.Parse(connectionString);

            Connection = new Lazy<ConnectionMultiplexer>(
                () => ConnectionMultiplexer.Connect(options)
            );
        }

        public static ConnectionMultiplexer GetConnection() => Connection.Value;
    }