[HttpGet] [ActionTitle(Name = "测试Garnet")] [Route("test.svc")] publicvoidStart() { // Redis connection var redis = ConnectionMultiplexer.Connect("localhost"); var redisDb = redis.GetDatabase();
// Garnet connection var garnet = new GarnetClient("localhost", 3278);
// Test data string key = "test_key"; stringvalue = "test_value";
// Test with Redis var redisStartTime = DateTime.Now; for( int i = 0; i < 1000; i++ ) { redisDb.StringSet(key + i, value + i); } var redisEndTime = DateTime.Now; Console.WriteLine($"StringSet Time taken by Redis: {(redisEndTime - redisStartTime).TotalMilliseconds} ms"); // Test with Garnet var garnetStartTime = DateTime.Now; for( int i = 0; i < 1000; i++ ) { garnet.StringSet(key + i, value + i, null); } var garnetEndTime = DateTime.Now; Console.WriteLine($"StringSet Time taken by Garnet: {(garnetEndTime - garnetStartTime).TotalMilliseconds} ms"); // Test with Redis redisStartTime = DateTime.Now; for( int i = 0; i < 1000; i++ ) { string a = redisDb.StringGet(key + i).ToString(); } redisEndTime = DateTime.Now; Console.WriteLine($"StringGet Time taken by Redis: {(redisEndTime - redisStartTime).TotalMilliseconds} ms"); // Test with Garnet garnetStartTime = DateTime.Now; for( int i = 0; i < 1000; i++ ) { garnet.StringGet(key + i, null); } garnetEndTime = DateTime.Now; Console.WriteLine($"StringGet Time taken by Garnet: {(garnetEndTime - garnetStartTime).TotalMilliseconds} ms"); }
// 测试自定义命令 server.Register.NewTransactionProc("TEST", 0, () => new TestCustomCommand());
sealed class TestCustomCommand : CustomTransactionProcedure { /// <summary> /// No transactional phase, skip Prepare /// </summary> public override bool Prepare<TGarnetReadApi>(TGarnetReadApi api, ArgSlice input) => false;
/// <summary> /// Main will not be called because Prepare returns false /// </summary> public override void Main<TGarnetApi>(TGarnetApi api, ArgSlice input, ref MemoryResult<byte> output) => throw new InvalidOperationException();
/// <summary> /// Finalize reads two keys (non-transactionally) and return their values as an array of bulk strings /// </summary> public override void Finalize<TGarnetApi>(TGarnetApi api, ArgSlice input, ref MemoryResult<byte> output) { // Return the two keys as an array of bulk strings WriteSimpleString(ref output, "Hello Garnet!"); } }
[HttpGet] [ActionTitle(Name = "测试Garnet 自定义命令")] [Route("custom-command.svc")] public async void Test() { // Garnet connection var garnet = new GarnetClient("localhost", 3278); garnet.Connect(); // Test data string result = await garnet.ExecuteForStringResultAsync("TEST");