在将数据保存到 Redis 缓存时,我的性能非常差。
设想 :
1)利用Redis缓存服务(微软Azure提供)。
2) 在 Azure 上创建的虚拟机中运行代码。
3)VM和缓存服务都创建在同一个位置
代码片段:
public void MyCustomFunction()
{
Stopwatch totalTime = Stopwatch.StartNew();
RedisEndpoint config = new RedisEndpoint();
config.Ssl = true;
config.Host = "redis.redis.cache.windows.net";
config.Password = Form1.Password;
config.Port = 6380;
RedisClient client = new RedisClient(config);
int j = 0;
for (int i = 0; i < 500; i++)
{
var currentStopWatchTime = Stopwatch.StartNew();
var msgClient = client.As<Message>();
List<string> dataToUpload = ClientData.GetRandomData();
string myCachedItem_1 = dataToUpload[1].ToString();
Random ran = new Random();
string newKey = string.Empty;
newKey = Guid.NewGuid().ToString();
Message newItem = new Message
{
Id = msgClient.GetNextSequence(), // Size : Long variable
//Id = (long)ran.Next(),
Key = j.ToString(), // Size: Int32 variable
Value = newKey, // Size : Guid string variable
Description = myCachedItem_1 // Size : 5 KB
};
string listName = ran.Next(1, 6).ToString();
msgClient.Lists[listName].Add(newItem);
//msgClient.Store(newItem);
Console.WriteLine("Loop Count : " + j++ + " , Total no. of items in List : " + listName + " are : " + msgClient.Lists[listName].Count);
Console.WriteLine("Current Time: " + currentStopWatchTime.ElapsedMilliseconds + " Total time:" + totalTime.ElapsedMilliseconds);
Console.WriteLine("Cache saved");
}
}
性能(保存时):
注意:(所有时间以毫秒为单位)
循环次数:0,总次数。列表中的项目数:2 是:1 当前时间:310 总时间:342 缓存保存循环计数:1,总数。列表中的项目数:3 是:1 当前时间:6 总时间:349 缓存保存循环计数:2,总数。列表中的项目数:5 是:1 当前时间:3 总时间:353 缓存保存循环计数:3,总数。列表中的项目数:5 是:2 当前时间:3 总时间:356 缓存保存循环计数:4,总数。列表中的项目数:5 是:3 当前时间:3 总时间:360 缓存已保存
. . . . .
循环次数:330,总次数。列表中的项目数:4 是:69 当前时间:2 总时间:7057 缓存保存
循环计数:331,总数。列表中的项目数:4 是:70 当前时间:3 总时间:7061 缓存保存
循环计数:332,总数。列表中的项目数:4 是:71 当前时间:2 总时间:7064 缓存已保存
性能(获取时)
清单 : 1 项目数量 : 110 时间 : 57
清单 : 2 项目数量 : 90 时间 : 45
清单 : 3 件数 : 51 时间 : 23
清单 : 4 件数 : 75 时间 : 32
清单 : 5 件数 : 63 时间 : 33