Skip to content
This repository was archived by the owner on Nov 16, 2022. It is now read-only.
This repository was archived by the owner on Nov 16, 2022. It is now read-only.

Implement ForceReconnect #56

@simonpinn

Description

@simonpinn

Hi @AliBazzi

We've seen some instances while running in Azure Redis when a Redis failover has caused the ConnectionMultiplexer to not retry the connection (instead requiring a restart of the service), this isn't a great situation!
To avoid this in other projects we use the ForceReconnect pattern from the Redis

https://gist.github.com/JonCole/925630df72be1351b21440625ff2671f#file-redis-bestpractices-stackexchange-redis-md from
https://docs.microsoft.com/en-gb/azure/azure-cache-for-redis/cache-best-practices

It does require changing the Get / Set to check and reconnect to something like:

public async Task<bool> AddAsync<T>(string key, T value, TimeSpan expiresIn)
        {
            try
            {
                try
                {
                    return await _cacheClient.AddAsync(key, value, expiresIn);
                }
                catch (RedisConnectionException)
                {
                    RedisConnection.ForceReconnect();
                }
                catch (SocketException)
                {
                    RedisConnection.ForceReconnect();
                }
                catch (WebSocketException)
                {
                    RedisConnection.ForceReconnect();
                }
            }
            catch (ObjectDisposedException)
            {
                //ForceReconnect can potentially throw ObjectDisposedException, nothing we can do, hopefully cache back on next call
                return false;
            }
            
            return false;
    }

Let me know if you'd consider adding this, or if a PR would be accepted.

thanks

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions