名前付きタグをキャッシュ アイテムに関連付けるには、名前付きタグのリストを提供する必要があります。各名前付きタグには、次の XNUMX つのパラメータがあります。 キー タグの名前である文字列として、および 値 キーに割り当てられた値であるプリミティブ型として。 NCache これにより、オブジェクトをこれらの名前付きタグに関連付けることができます。名前付きタグを使用してデータ キャッシュに項目を追加し、後で、以前に追加した名前付きタグを使用してキャッシュからこれらの項目を取得できます。
アイテムが特定の名前付きタグとともにキャッシュにすでに追加されている場合、 NCache データ キャッシュに既に存在する名前付きタグを更新するオプションをユーザーに提供します。
前提条件
NCache カスタムクラスを提供し、 CacheItem
データをデータ キャッシュに追加するために使用できます。このクラスは、データを値プロパティとしてカプセル化します。 CacheItem
また、オブジェクトに関連付けられた追加の仕様を property
NamedTagsクラスの。
次の例では、名前付きタグを property
of CacheItem
.
// Precondition: Cache is already connected
// In an online-store, VIP members are offered 10% discount
// A customer with VIP membership has signed in to the website
string customerKey = $"Customer:ALFKI";
Customer customer = cache.Get<Customer>(customerKey);
// Get customer from database if not found in cache
if (customer == null)
{
// Get customer from the database
customer = FetchCustomerFromDB("ALFKI");
// Create a new CacheItem
var cacheItem = new CacheItem(customer);
// Creating a Named Tags Dictionary
var namedTags = new NamedTagsDictionary();
/* Adding Named Tags to the Dictionary where
Keys are the names of the tags as string type and Values are of primitive type*/
namedTags.Add("VIP_Membership_Discount", 0.10 );
// Setting the named tag property of the cacheItem
cacheItem.NamedTags = namedTags;
cache.Add(customerKey, cacheItem);
}
String customerKey = "Customer:ALFKI";
Customer customer = cache.get(customerKey, Customer.class);
if (customer == null) {
// Fetch customer from database and create a com.alachisoft.ncache.sample.NamedTags.Customer object
customer = Customer.fetchCustomerFromDB("Customer:ALFKI");
}
NamedTagsDictionary namedTags = new NamedTagsDictionary();
namedTags.add("VIP_Membership_Discount", 0.10);
CacheItem cacheItem = new CacheItem(customer);
cacheItem.setNamedTags(namedTags);
cache.insert(customerKey, cacheItem);
System.out.println("Customer added to cache: " + customerKey);
// This is an async method
// Precondition: Cache is already connected
// Get product from database against given productID
var product = this.fetchProductFromDB(1001);
// Create a unique cache key for this product
var key = "Product:" + product.getProductID();
// Creating a Named Tags Dictionary
var productNamedTag = new ncache.NamedTagsDictionary();
// Adding Named Tags to the Dictionary where
// Keys are the names of the tags as string type
// Values are the instances of data, of primitive data structures.
productNamedTag.add("FlashSaleDiscount", 0.5);
// Create a new CacheItem
// You alose need to specify the FQN(Fully Qualified Name) of the class
var cacheItem = new ncache.CacheItem(product,"FQN.Product");
// Setting the named tag property of the CacheItem
cacheItem.setNamedTags(productNamedTag);
// Add product object to the cache
var version = await this.cache.add(key, cacheItem);
// CacheItem added successfully
# Precondition: Cache is already connected
# Get product from database against given productID
product = fetch_product_from_db()
# Create a unique cache key for this product
key = "Product:" + product.get_product_id()
# Creating a Named Tags Dictionary
product_named_tag = ncache.NamedTagsDictionary()
# Adding Named Tags to the Dict where
# Keys are the names of the tags as string type
# Values are the instances of data, of primitive data structures.
product_named_tag.add("FlashSaleDiscount", 0.5)
# Create a new CacheItem
cache_item = ncache.CacheItem(product)
# Setting the named tag property of the CacheItem
cache_item.set_named_tags(product_named_tag)
# Add product object to the cache
version = cache.add(key, cache_item)
# CacheItem added successfully
Note
操作がフェイルセーフであることを保証するために、で説明されているように、アプリケーション内の潜在的な例外を処理することをお勧めします。 失敗の処理.
による名前付きタグの更新 CacheItem
最初に項目をフェッチし、その名前付きタグを変更してから、その項目を使用してキャッシュに再挿入する必要があります。 インセット 方法。
次の例では、最初に CacheItem
、新しく名前付きタグを含むリストを作成し、 CacheItem
変更された名前付きタグ リストとともにキャッシュに保存されます。 したがって、既存の名前付きタグの値が上書きされます。
警告
提供 Null
名前付きタグ配列は ArgumentNullException をスローします。
// Store Owner has increased VIP membership discount to 12%
string customerKey = $"Customer:ALFKI";
Customer customer = cache.Get<Customer>(customerKey);
// Get the customer from database if not found in the cache
if (customer == null)
{
customer = FetchCustomerFromDB("ALFKI");
}
// Create a new CacheItem
var cacheItem = new CacheItem(customer);
// Creating a Named Tags Dictionary
var newNamedTags = new NamedTagsDictionary();
// Add Named Tag with the updated discount value to the Dictionary
newNamedTags.Add("VIP_Membership_Discount", 0.12);
// Updates the Named Tags with the new Named Tags
// Overrides the value of the existing Named Tags
cacheItem.NamedTags = newNamedTags;
// Reinserts the updated cacheItem into the cache with the modified Named Tags
cache.Insert(customerKey, cacheItem);
String customerKey = "Customer:ALFKI";
Customer customer = cache.get(customerKey, Customer.class);
if (customer == null) {
// Fetch customer from database and create a com.alachisoft.ncache.sample.NamedTags.Customer object
customer = Customer.fetchCustomerFromDB("Customer:ALFKI");
}
// Create a new CacheItem
CacheItem cacheItem = new CacheItem(customer);
NamedTagsDictionary namedTags = new NamedTagsDictionary();
namedTags.add("VIP_Membership_Discount", 0.50);
cacheItem.setNamedTags(namedTags);
cache.insert(customerKey, cacheItem);
System.out.println("Customer with updated named tags inserted into cache: " + customerKey);
// This is an async method
// Precondition: Cache is already connected
// Get cache item from cache against given key
var key = "Product:1001";
var cacheItem = await this.cache.getCacheItem(key);
// Create a Named Tags Dictionary
var newProductNamedTag = new ncache.NamedTagsDictionary();
// Adding Named Tags to the Dictionary where
// Keys are the names of the tags as string type
// Values are the updated instances of data, of primitive data types.
newProductNamedTag.add("FlashSaleDiscount", 0.7);
// Updates the Named Tags with the new Named Tags
// Overrides the value of the existing Named Tags
cacheItem.setNamedTags(newProductNamedTag);
//Re-inserts the cacheItem into cache with modified Named Tags
var version = await this.cache.insert(key, cacheItem);
// cacheItem is successfully added in cache with modified Named Tags
# Precondition: Cache is already connected
# Get cache item from cache against given key
key = "Product:1001"
cache_item = cache.get_cacheitem(key)
# Create a Named Tags Dictionary
new_product_named_tag = ncache.NamedTagsDictionary()
# Adding Named Tags to the Dict where
# Keys are the names of the tags as string type
# Values are the updated instances of data, of primitive data types.
new_product_named_tag.add("FlashSaleDiscount", 0.7)
# Updates the Named Tags with the new Named Tags
# Overrides the value of the existing Named Tags
cache_item.set_named_tags(new_product_named_tag)
# Re-insert the cacheItem into cache with modified Named Tags
version = cache.insert(key, cache_item)
# cacheItem is successfully added in cache with modified Named Tags
その他のリソース
NCache 名前付きタグのサンプル アプリケーションを提供します。 GitHubの.
も参照してください
タグキャッシュデータ
NamedTagsを使用したSQLクエリ
NamedTagsを使用したSQL削除
データの有効期限