要将命名标签与任何缓存项相关联,需要提供命名标签列表,每个命名标签有两个参数: 键 作为标签名称的字符串,以及 折扣值 作为任何原始类型,它是键的赋值。 NCache 然后允许您将对象与这些命名标签关联起来。您可以使用命名标签在数据缓存中添加项目,然后使用之前添加的命名标签从缓存中检索这些项目。
当项目已添加到具有某些命名标签的缓存中时, NCache 为用户提供更新数据缓存中已存在的命名标签的选项。
先决条件
NCache 提供自定义类, CacheItem
可用于将数据添加到数据缓存。此类将数据封装为其 value 属性。 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
备注
为确保操作是故障安全的,建议处理应用程序中的任何潜在异常,如中所述 处理故障.
通过更新命名标签 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上.
参见
标记缓存数据
带有 NamedTag 的 SQL 查询
使用 NamedTags 删除 SQL
数据过期