sql >> Base de Datos >  >> NoSQL >> Redis

Azure Redis Cache:varios errores TimeoutException:tiempo de espera al ejecutar GET {key}

Algunos puntos que mejoraron nuestra situación:

Protobuf-net en lugar de BinaryFormatter

Recomiendo usar protobuf-net ya que reducirá el tamaño de los valores que desea almacenar en su caché.

public interface ICacheDataSerializer
    {
        byte[] Serialize(object o);
        T Deserialize<T>(byte[] stream);
    }

public class ProtobufNetSerializer : ICacheDataSerializer
    {
        public byte[] Serialize(object o)
        {
            using (var memoryStream = new MemoryStream())
            {
                Serializer.Serialize(memoryStream, o);

                return memoryStream.ToArray();
            }
        }

        public T Deserialize<T>(byte[] stream)
        {
            var memoryStream = new MemoryStream(stream);

            return Serializer.Deserialize<T>(memoryStream);
        }
    }

Implementar estrategia de reintento

Implemente esta RedisCacheTransientErrorDetectionStrategy para manejar los problemas de tiempo de espera.

using Microsoft.Practices.TransientFaultHandling;

public class RedisCacheTransientErrorDetectionStrategy : ITransientErrorDetectionStrategy
    {
        /// <summary>
        /// Custom Redis Transient Error Detenction Strategy must have been implemented to satisfy Redis exceptions.
        /// </summary>
        /// <param name="ex"></param>
        /// <returns></returns>
        public bool IsTransient(Exception ex)
        {
            if (ex == null) return false;

            if (ex is TimeoutException) return true;

            if (ex is RedisServerException) return true;

            if (ex is RedisException) return true;

            if (ex.InnerException != null)
            {
                return IsTransient(ex.InnerException);
            }

            return false;
        }
    }

Instancia así:

private readonly RetryPolicy _retryPolicy;

// CODE
var retryStrategy = new FixedInterval(3, TimeSpan.FromSeconds(2));
            _retryPolicy = new RetryPolicy<RedisCacheTransientErrorDetectionStrategy>(retryStrategy);

Usar así:

var cachedString = _retryPolicy.ExecuteAction(() => dataCache.StringGet(fullCacheKey));

Revisa tu código para minimizar las llamadas de caché y los valores que está almacenando en su caché. Reduje muchos errores almacenando valores de manera más eficiente.

Si nada de esto ayuda. Mover a un caché superior (terminamos usando C3 en lugar de C1).