๋๋์ด ์๊ฐ์ด ๋์ 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 ์ฌ์ฉ์๋ค์ ๋๋ฉ์ธ๋ง ๋ฐ๊พธ์
๋ ๋์ถฉ ๋ ๊ฒ ๊ฐ์ต๋๋ค.