드디어 시간이 나서 Garnet 을 시작해보기로 했습니다.
Cache db 를 사실 거의 안써봐서 굉장히 긴장했습니다.
Site
github : GitHub - microsoft/garnet: Garnet is a remote cache-store from Microsoft Research that offers strong performance (throughput and latency), scalability, storage, recovery, cluster sharding, key migration, and replication features. Garnet can work with existing Redis clients.
official Site : Hello from Garnet | Garnet
해당 사이트에서 받으시고 Site에서 보고 따라하시면 됩니다.
설치
cd garnet
dotnet restore
dotnet build -c Release
Test Suite 실행
dotnet test -c Release -f net8.0 -l “console;verbosity=detailed”
대략이러면 뭔가 엄청난것이 PC에 설치합니다.
설치가 끝나고 Instance 를 올립니다 ( 이때 default port 6379를 오픈하세요)
실행
cd main/GarnetServer
dotnet run -c Release -f net8.0
뭐 이러면 garnet 이 실행중이라는데 .. 앞으로 어떻게 해야할지 몰랐습니다.
접속을 위해서는 사이트에
Redis-cli 나 c#에서는 StackExchange.Redis Nuget 에서 참조를 하면 된다고 합니다.
여기서 garnet 을 설치했는데 도구는 redis를 쓰라고 하고 있군요 ;;;
그렇습니다. 그냥 redis 쓰시듯 그대로 쓰시면 된다고 합니다. ;;;
Redis 입장에서는 약간 얄미울것 같군요
그래서 c# console 프로젝트를 만들고
stackexchange를 Nuget 에서 참조했습니다.
sample 소스를 못찾아서 redis c# client 소스를 그대로 참고했습니다.
public class GarnetStore
{
private ConnectionMultiplexer _redis;
private IServer _server;
private IDatabase _database;
private readonly JsonSerializerOptions _jsonSerializerOptions = new JsonSerializerOptions()
{
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
};
public GarnetStore(string connectionString)
{
_redis = ConnectionMultiplexer.Connect(connectionString);
if (_redis == null)
{
return;
}
if (!_redis.IsConnected)
{
return;
}
_database = _redis.GetDatabase();
var endpoint = _redis.GetEndPoints().Single();
_server = _redis.GetServer(endpoint);
}
public List<RedisKey> GetKeys(string pattern)
{
return _server.Keys(pattern: pattern).ToList();
}
public void SetValue(RedisKey key, RedisValue value)
{
_database.StringSet(key, value);
}
public string GetValue(RedisKey key)
{
return _database.StringGet(key);
}
public bool JsonSet(string key, object value)
{
string json = JsonSerializer.Serialize(value, _jsonSerializerOptions);
return _database.StringSet(key, json);
}
public T JsonGet<T>(string key)
{
RedisValue redisValue = _database.StringGet(key);
if (redisValue.IsNullOrEmpty)
{
return default(T);
}
return JsonSerializer.Deserialize<T>(redisValue, _jsonSerializerOptions);
}
}
public class MyClass
{
public int value1 { get; set; }
public string value2 { get; set; }
}
뭐 대략 이런식으로 하고
GarnetStore redis = new GarnetStore("192.168.0.11:6379");
redis.SetValue("test1", "ans-test1");
// key로 value를 찾는다.
Console.WriteLine(redis.GetValue("test1"));
// value로 사용자 정의 클래스를 입력
redis.JsonSet("test2", new MyClass()
{
value1 = 1,
value2 = "test2"
});
// key로 사용자 정의 클래스를 찾는다.
var resultMyClass = redis.JsonGet<MyClass>("test2");
Console.WriteLine($"{resultMyClass.value1}, {resultMyClass.value2}");
}
이렇게 하시면

garnet 에 데이타 잘빼고 가져옵니다 .
하기 전에는 굉장히 어렵고 두려웠는데 너무 쉽게 되서 의외였습니다.
그냥 기존 redis 사용자들은 도메인만 바꾸셔도 대충 될것 같습니다.


