我發送某個服務的查詢並取回結果。我想知道我過去是否已經得到相同的「答案」。所以,我打算使用Azure Table作爲緩存機制。Azure存儲表 - 插入批次行並檢查它們是否存在
我做這個小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);
當我運行在第一時間將此代碼,它的正常工作。在這最後,我在桌上有兩排。
問題出在第二次運行。當我執行此代碼:
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 - exists
- customer3 - 添加
我其實得到的是這個異常(在ExecuteBatch()
法):
索取資料請求ID:5116ee8a-0002-0024-7ac1-415787000000 RequestDate:星期五,2016年11月18日17:33:08 GMT狀態消息:0:指定實體 已存在。 ErrorCode:EntityAlreadyExists
服務器發現#1實體存在,因此,跳過整個任務。
我該如何得到預期的答案?
天真的解決方案,是嘗試添加所有N個項目,一個接一個。但是這個解決方案是最慢的一個(N個HTTP請求,而不是1個請求)。
?通過這種方式,在執行命令後,我無法知道這些項目是新添加的還是剛剛被替換的... – No1Lives4Ever
我不相信有方法可以判斷實體是被替換還是隻是被激活。也許表格存儲不適合你正在嘗試做的事情。你考慮過其他選擇嗎? –