50

Azure Redis Cache Service で StackExchange.Redis クライアントを使用しています。これが私のクラスです。

public class RedisCacheService : ICacheService
{
    private readonly ISettings _settings;
    private readonly IDatabase _cache;

    public RedisCacheService(ISettings settings)
    {
        _settings = settings;
        var connectionMultiplexer = ConnectionMultiplexer.Connect(settings.RedisConnection);
        _cache = connectionMultiplexer.GetDatabase();
    }

    public bool Exists(string key)
    {
        return _cache.KeyExists(key);
    }

    public void Save(string key, string value)
    {
        var ts = TimeSpan.FromMinutes(_settings.CacheTimeout);
        _cache.StringSet(key, value, ts);
    }

    public string Get(string key)
    {
        return _cache.StringGet(key);
    }

    public void Remove(string key)
    {
        // How to remove one
    }

    public void Clear()
    {
        // How to remove all
    }
}

更新: マークの助けを借りて、これが私の最終クラスです

public class RedisCacheService : ICacheService
{
    private readonly ISettings _settings;
    private readonly IDatabase _cache;
    private static ConnectionMultiplexer _connectionMultiplexer;

    static RedisCacheService()
    {
        var connection = ConfigurationManager.AppSettings["RedisConnection"];
        _connectionMultiplexer = ConnectionMultiplexer.Connect(connection);
    }

    public RedisCacheService(ISettings settings)
    {
        _settings = settings;
        _cache = _connectionMultiplexer.GetDatabase();
    }

    public bool Exists(string key)
    {
        return _cache.KeyExists(key);
    }

    public void Save(string key, string value)
    {
        var ts = TimeSpan.FromMinutes(_settings.CacheTimeout);
        _cache.StringSet(key, value, ts);
    }

    public string Get(string key)
    {
        return _cache.StringGet(key);
    }

    public void Remove(string key)
    {
        _cache.KeyDelete(key);
    }

    public void Clear()
    {
        var endpoints = _connectionMultiplexer.GetEndPoints(true);
        foreach (var endpoint in endpoints)
        {
            var server = _connectionMultiplexer.GetServer(endpoint);
            server.FlushAllDatabases();    
        }
    }
}

現在、redis キャッシュからすべてのアイテムまたは単一のアイテムを削除する方法がわかりません。

4

4 に答える 4

70

1 つのアイテムを削除するには:

_cache.KeyDelete(key);

すべてを削除するには、FLUSHDBまたはFLUSHALLredis コマンドが必要です。どちらも StackExchange.Redis で利用できます。ただし、ここで説明する理由により、それらはIDatabaseAPI にはありません (論理データベースではなく、サーバーに影響するため)。

「では、どのように使用するのですか?」のとおりです。そのページで:

server.FlushDatabase(); // to wipe a single database, 0 by default
server.FlushAllDatabases(); // to wipe all databases

(おそらくGetEndpoints()マルチプレクサで使用した後)

于 2014-07-02T13:09:51.207 に答える
26

Azure Redis Cache でデータベースをフラッシュできませんでした。次のエラーが発生しました。

この操作は、管理者モードが有効になっていない限り使用できません: FLUSHDB

代わりに、すべてのキーを反復して削除します。

var endpoints = connectionMultiplexer.GetEndPoints();
var server = connectionMultiplexer.GetServer(endpoints.First());
//FlushDatabase didn't work for me: got error admin mode not enabled error
//server.FlushDatabase();
var keys = server.Keys();
foreach (var key in keys)
{
  Console.WriteLine("Removing Key {0} from cache", key.ToString());
  _cache.KeyDelete(key);
}
于 2014-07-07T11:46:23.260 に答える