【发布时间】:2017-04-02 15:39:20
【问题描述】:
我为某些服务发送查询并返回结果。我想知道我过去是否已经得到相同的“答案”。所以,我打算使用 Azure Table 作为缓存机制。
我做这个小 POC:
TableBatchOperation batchOperation = new TableBatchOperation();
CachedUrl customer1 = new CachedUrl(Guid.Empty, "test1");
CachedUrl customer2 = new CachedUrl(Guid.Empty, "test2");
batchOperation.Insert(customer1);
batchOperation.Insert(customer2);
table.ExecuteBatch(batchOperation);
当我第一次运行此代码时,它运行良好。最后,我在表中有 2 行。
问题出在第二次运行。当我执行这段代码时:
TableBatchOperation batchOperation = new TableBatchOperation();
CachedUrl customer1 = new CachedUrl(Guid.Empty, "test1");
CachedUrl customer2 = new CachedUrl(Guid.Empty, "test2");
CachedUrl customer3 = new CachedUrl(Guid.Empty, "test3");
batchOperation.Insert(customer1);
batchOperation.Insert(customer2);
batchOperation.Insert(customer3);
table.ExecuteBatch(batchOperation);
(注意添加customer3)
我期望得到的是一条消息:
- customer1 - 存在
- customer2 - 存在
- customer3 - 添加
我实际得到的是这个异常(在ExecuteBatch() 方法上):
请求信息RequestID:5116ee8a-0002-0024-7ac1-415787000000 请求日期:2016 年 11 月 18 日星期五 17:33:08 GMT 状态消息:0: 指定的实体已经存在。错误代码:EntityAlreadyExists
服务器发现 #1 实体存在,因此,跳过整个任务。
我怎样才能得到预期的答案?
天真的解决方案是尝试将所有 N 项逐个添加。但是这个解决方案是最慢的一个(N 个 HTTP 请求而不是 1 个请求)。
【问题讨论】:
标签: c# azure azure-storage azure-table-storage