データキャッシュ内のグループの追加/更新
NCache ユーザーが特定のグループのデータ キャッシュにアイテムを追加できるようにします。 1 つのグループに関連付けられるアイテムは 1 つだけです。 CacheItem
によって提供されるカスタムクラスです NCache これは、キャッシュにデータを追加するために使用できます。 CacheItem
また、オブジェクトに関連付けられた追加の仕様を property
グループの。
CacheItem
を使用してキャッシュに追加されます Insert
メソッド。指定されたキーを持つ項目がキャッシュ内にまだ存在しない場合に、グループを使用する推奨アプローチです。
一方、 Add
キーがすでに存在する場合、操作は失敗し、例外がスローされます。
前提条件 データ キャッシュ内のグループの追加/更新
次の例では、データ グループを 財産 of CacheItem
オブジェクトを含む 顧客.
// Preconditions: Cache is already connected
// In an online-store, customers from different regions need to be grouped accordingly
// A customer has signed in to the website from the East Coast region
// Create a unique cache key for this customer.
string customerKey = $"Customer:ALFKI";
// Get customer from cache
Customer customer = cache.Get<Customer>(customerKey);
// Get customer from database if not found in cache
if (customer == null)
{
customer = FetchCustomerFromDB("ALFKI");
// Create a new CacheItem
var cacheItem = new CacheItem(customer);
// Specify the group
cacheItem.Group = "East Coast Customers";
// Add customer object to cache with group
cache.Add(customerKey, cacheItem);
}
// Pre-condition: Cache is already connected
// Create a unique cache key for this customer.
String customerKey = "Customer:ALFKI";
// If customer is not found in the cache, fetch from the database and add to cache
if (customer == null) {
Customer customer = fetchCustomerFromDB("Customer:ALFKI");
// Create a new CacheItem to store the customer with metadata
CacheItem cacheItem = new CacheItem(customer);
// Specify the group for the cache item
cacheItem.setGroup("East Coast Customers");
System.out.println("Group Set Successfully");
// Add customer object to cache with group and metadata
cache.insert(customerKey, cacheItem);
System.out.println("Item Added Successfully");
}
// This is an async method
// Precondition: Cache is already connected
// Get customer from database against given customer ID
var customer = this.fetchCustomerFromDB("ALFKI");
// Create a unique cache key for this customer.
var key = "Customers:" + customer.getCustomerID();
// Create a new CacheItem
var cacheItem = new ncache.CacheItem(customer);
// Specify the group
cacheItem.setGroup("Important Customers");
// Add customer object to cache with group
var version = await this.cache.insert(key, cacheItem);
// CacheItem is successfully added in the cache with group
# Precondition: Cache is already connected
# Get customer from database
customer = fetch_customer_from_db()
# Create a unique cache key for this customer.
key = "Customers:" + customer.get_customer_id()
# Create a new CacheItem
cache_item = ncache.CacheItem(customer)
# Specify the group
cache_item.set_group("Important Customers")
# Add customer object to cache with group
version = cache.insert(key, cache_item)
# CacheItem is successfully added in the cache with group
Note
操作がフェイルセーフであることを保証するために、で説明されているように、アプリケーション内の潜在的な例外を処理することをお勧めします。 失敗の処理.
CacheItemのグループを更新します
のデータグループ CacheItem
更新することもできます。 あ CacheItem
によって提供されるカスタムクラスです NCache データをデータ キャッシュに追加するために使用できます。の CacheItem
グループを property
CacheItem
.
次の例では、グループを更新します。 CacheItem
.
// A customer has signed in to the website from a different region, i.e., West Coast
string customerKey = $"Customer:ALFKI";
// Retrieve the cacheItem from the cache
var cacheItem = cache.GetCacheItem(customerKey);
// Specify the new group to be updated
cacheItem.Group = "West Coast Customers";
// Reinsert the cacheItem in the cache with updated groups
cache.Insert(customerKey, cacheItem);
// A customer has signed in to the website from a different region i.e. West Coast
String customerKey = "Customer:ALFKI";
// Retrieve the cacheItem from the cache
CacheItem cacheItem = null;
cacheItem = cache.getCacheItem(customerKey);
// Specify the new group to be updated
cacheItem.setGroup("West Coast Customers");
System.out.println("Group Set Successfully");
// Reinsert the cacheItem in the cache with updated groups
cache.insert(customerKey, cacheItem);
System.out.println("Item Updated Successfully");
// This is an async method
// Precondtion: CacheItem is added with the group
// Retrieve the cacheItem from the cache
var key = "ALFKI";
var cacheItem = this.cache.getCacheItem(key);
// Re-insert the item in the cache after the removal of group
await this.cache.insert(key, cacheItem);
// Get cacheitem with the updated version
cacheItem = this.cache.getCacheItem(key);
// Specify the new group to be updated
cacheItem.setGroup("US Customers");
// Re-insert the cacheItem in the cache with updated groups
await this.cache.insert(key, cacheItem);
// CacheItem is successfully added in the cache with updated group
# Precondition: CacheItem is added with the group
# Retrieve the CacheItem with old group from the cache
key = "ALFKI"
cache_item = cache.get_cacheitem(key)
# Specify the new group to be updated
cache_item.set_group("US Customers")
# Re-insert the cacheItem in the cache with updated groups
cache.insert(key, cache_item)
# CacheItem is successfully added in the cache with updated group
その他のリソース
NCache 上のグループのサンプルアプリケーションを提供します GitHubの.
も参照してください
。ネット: Alachisoft.NCache。クライアント 名前空間
Java: comの。alachisoft.ncache。クライアント 名前空間
Node.js: キャッシュ とに提供されます。
Python: ncache。クライアント とに提供されます。