【发布时间】:2015-06-02 16:28:14
【问题描述】:
在将数据保存到 Redis 缓存时性能非常差。
场景:
1) 使用 Redis 缓存服务(Microsoft 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
【问题讨论】:
-
您能提供更多信息吗? Redis 的大小和定价层,与 VM 相同吗?可以使用连接池(PooledRedisClientManager)吗?
-
@Panagiotis Kefalidis,Redis 缓存:标准 1 GB VM:标准,A2(2 核,3.5 GB 内存)我第一次使用 Redis 缓存,我对 PooledRedisClientManager 不太了解。你能给它任何很好的参考吗?如果可能,我可以在多个线程上执行这些操作。
-
关闭 SSL 会有不同吗?尝试将 SSL 设置为 false
标签: caching azure redis azure-caching servicestack.redis