StackExchange에서 항목을 모두 제거/삭제합니다.다시 캐시
저는 StackExchange를 사용하고 있습니다.Azure Redis Cache Service를 사용하는 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
}
}
업데이트: Marc의 도움으로, 마지막 수업입니다.
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();
}
}
}
이제 rediscache에서 모든 항목 또는 단일 항목을 제거하는 방법을 모르겠습니다.
단일 항목 제거하기
_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()멀티플렉서)
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);
}
@Rasi와 @Marc Gravell의 답변에는 모두 필요한 코드 조각이 포함되어 있습니다.위의 내용을 바탕으로 서버가 하나만 있다고 가정할 경우 다음과 같은 작업 코드가 표시됩니다.
redis에 연결해야 합니다.allowAdmin=true이러한 옵션을 얻는 한 가지 방법은 AllowAdmin을 이미 구문 분석된 문자열에 할당하는 것입니다.
var options = ConfigurationOptions.Parse("server:6379");
options.AllowAdmin = true;
var redis = ConnectionMultiplexer.Connect(options);
그런 다음 모든 데이터베이스를 플러시합니다.
var endpoints = redis.GetEndPoints();
var server = redis.GetServer(endpoints[0]);
server.FlushAllDatabases();
위의 내용은 Azure뿐만 아니라 모든 재배포에 적용됩니다.
캐시된 목록에서 특정 값을 지우려는 경우 해시도 삭제할 수 있습니다.예를 들어, 다른 부서가 캐시된 엠프리스트와 내부가 있습니다.
public static void DeleteHash(string key, string cacheSubKey)
{
if (string.IsNullOrEmpty(key))
throw new ArgumentNullException("key");
Cache.HashDelete(key, cacheSubKey);
}
키 이름과 캐시 하위 키도 전달할 수 있습니다.
언급URL : https://stackoverflow.com/questions/24531421/remove-delete-all-one-item-from-stackexchange-redis-cache
'programing' 카테고리의 다른 글
| GUID의 문자열 길이는 얼마입니까? (0) | 2023.05.04 |
|---|---|
| Bash의 파일에서 마지막 줄 제거 (0) | 2023.05.04 |
| ASP.NET MS11-100: 게시된 양식 값의 최대 수에 대한 제한을 변경하려면 어떻게 해야 합니까? (0) | 2023.05.04 |
| Xcode 10, 명령 코드 서명이 0이 아닌 종료 코드로 실패했습니다. (0) | 2023.05.04 |
| SQL에 if-then-else 논리가 있습니까? (0) | 2023.05.04 |