Azure 受管理快取服務的並行存取模型

重要

Microsoft 建議使用 Azure Redis 快取的所有新開發。 如需選擇 Azure 快取供應專案的目前檔和指引,請參閱 哪一個 Azure 快取供應專案適合我?

快取架構讓具有適當的網路存取權與組態設定的任何快取用戶端,可以存取任何已快取的資料。 這帶來了並行存取的挑戰。

為協助您的應用程式處理並行存取問題,支援開放式與封閉式並行存取模型。

開放式並行存取模型

在開放式並行存取模型中,對於快取物件的更新並不會取得鎖定。 相反地,當快取用戶端從快取取得物件時,它也會取得並儲存該物件的最新版本。 當需要更新時,快取用戶端會傳送物件的新值與已儲存版本物件。 只有當傳送的版本與快取中物件的最新版本相符時,系統才會更新物件。 每次物件更新時都會變更它的版本號碼,這樣可防止更新程序覆寫其他人所做的變更。

本主題的範例說明開放式並行存取如何維持資料一致性。

範例

在此範例中,兩個快取用戶端 (cacheClientAcacheClientB) 會嘗試更新相同的快取物件,並具有相同的索引鍵 RadioInventory

時間零:兩個用戶端擷取相同的物件

在時間 0 (T0) 時,這兩個快取用戶端會具現化 DataCacheItem 類別,以擷取它們要更新的快取物件和與該快取物件關聯的其他資訊 (例如,版本和標記資訊)。 下列程式碼範例說明此情形。

'cacheClientA pulls the FM radio inventory from cache
Dim strACSKey = "[Authentication token]"
Dim clientAFactorySecurity = New DataCacheSecurity(strAcsKey)

Dim clientAFactoryConfiguration = New DataCacheFactoryConfiguration()
'Set up desired parameters for Cache Factory
clientAFactoryConfiguration.SecurityProperties = clientAFactorySecurity

Dim clientACacheFactory As DataCacheFactory = New DataCacheFactory(clientAFactoryConfiguration)
Dim cacheClientA As DataCache = _
        clientACacheFactory.GetCache("catalog")
Dim radioInventoryA As DataCacheItem = _
        cacheClientA.GetCacheItem("RadioInventory")

'cacheClientB pulls the same FM radio inventory from cache
'clientBFactorySecurity and clientBFactoryConfiguration omitted for space
Dim clientBCacheFactory As DataCacheFactory = New DataCacheFactory(clientBFactoryConfiguration)
Dim cacheClientB As DataCache = _
       clientBCacheFactory.GetCache("catalog")
Dim radioInventoryB As DataCacheItem = _
        cacheClientB.GetCacheItem("RadioInventory")
//cacheClientA pulls the FM radio inventory from cache
string strACSKey = "[Authentication token]";
DataCacheSecurity clientAFactorySecurity = new DataCacheSecurity(strAcsKey);

DataCacheFactoryConfiguration clientAFactoryConfiguration = new DataCacheFactoryConfiguration();
// Set up desired parameters for Cache Factory
clientAFactoryConfiguration.SecurityProperties = clientAFactorySecurityDataCacheFactory;

clientACacheFactory = new DataCacheFactory(clientAFactoryConfiguration);
DataCache cacheClientA = clientACacheFactory.GetCache("catalog");
DataCacheItem radioInventoryA = 
    cacheClientA.GetCacheItem("RadioInventory");

//cacheClientB pulls the same FM radio inventory from cache
// clientBFactorySecurity and clientBFactoryConfiguration omitted for space
DataCacheFactory clientBCacheFactory = new DataCacheFactory();
DataCache cacheClientB = clientBCacheFactory.GetCache("catalog");
DataCacheItem radioInventoryB= 
    cacheClientB.GetCacheItem("RadioInventory");

注意

雖然此範例使用 GetCacheItem 方法來擷取 DataCacheItem 物件來取得版本資訊,但也可以使用 Get 方法來取得與擷取快取專案相關聯的 DataCacheItemVersion 物件。

第一次:第一次更新成功

在時間 1 (T1),cacheClientA 會以新值更新快取物件 RadioInventory。 當 cacheClientA 執行 Put 方法時,與 RadioInventory 快取項目關聯的版本會遞增。 此時,cacheClientB 擁有過時的快取項目。 下列範例會加以說明。

'at time T1, cacheClientA updates the FM radio inventory
Dim newRadioInventoryA As Integer = 155

cacheClientA.Put("RadioInventory", newRadioInventoryA, _
                 radioInventoryA.Version)
//at time T1, cacheClientA updates the FM radio inventory
int newRadioInventoryA = 155;

cacheClientA.Put("RadioInventory", newRadioInventoryA, 
    radioInventoryA.Version);

第二次:第二次更新失敗

在時間 2 (T2),cacheClientB 嘗試使用目前已過時的版本號碼來更新 RadioInventory 快取的物件。 為防止覆寫 cacheClientA 所做的變更,cacheClientBPut 方法呼叫會失敗。 快取用戶端會擲回 DataCacheException 物件, 並將 ErrorCode 屬性設定為 CacheItemVersionMismatch。 下列程式碼範例說明此情形。

'later, at time T2, cacheClientB tries to 
'update the FM radio inventory, receives DataCacheException with
'an error code equal to DataCacheErrorCode.CacheItemVersionMismatch.
Dim newRadioInventoryB As Integer = 130

cacheClientB.Put("RadioInventory", newRadioInventoryB, _
                 radioInventoryB.Version)
//later, at time T2, cacheClientB tries to 
//update the FM radio inventory, receives DataCacheException with
//an error code equal to DataCacheErrorCode.CacheItemVersionMismatch.
int newRadioInventoryB = 130;

cacheClientB.Put("RadioInventory", newRadioInventoryB,
    radioInventoryB.Version);

封閉式並行存取模型

在封閉式並行存取模型中,用戶端會明確地鎖定物件以執行操作。 要求鎖定的其他操作會被拒絕 (系統不會封鎖要求),直到鎖定被釋放。 當物件被鎖定時,會傳回鎖定控制控制代碼做為輸出參數。 必須有鎖定控制代碼才能將物件解除鎖定。 為避免用戶端應用程式在釋放已鎖定的物件之前結束,我們提供根據逾時來釋放鎖定的機制。 已鎖定的物件一律不會到期,但當那些物件在已經過到期時間的情況下被解除鎖定時,它們會立即到期。

如需與封閉式並行模型搭配使用的方法詳細資訊,請參閱 並行方法

注意

不支援跨操作的交易。

注意

使用快取的應用程式負責決定鎖定順序並偵測鎖死 (如果有的話)。

警告

任何用戶端都可以使用 Put 方法來取代快取中的已鎖定物件。 已啟用快取功能的應用程式必須一律針對使用封閉式並行存取模型的項目使用 PutAndUnlock

另請參閱

其他資源

Azure 受管理快取服務功能