• Facebook
  • Twitter
  • Youtube
  • LinkedIn
  • RSS
  • 文档
  • 比较
  • 博客
  • 尝试 NCache
  • 联系我们
尝试 NCache
显示/隐藏目录

关键数据依赖类型和用法

关键数据依赖性 NCache 是一种关系管理机制,它将依赖缓存项的生命周期与一个或多个“主”键关联起来。主要用于维护相关实体之间的数据完整性,例如确保…… 下单 如果其关联项被移除,则该关联项也会被移除。 产品 如果缓存被删除,此功能会自动清理缓存。

注意:

如果主密钥中有两种类型的修改,则从属项被删除:

  • 更新
  • 删除

Key(s) 可以有两种类型的依赖关系:

多级依赖

缓存项可以依赖于缓存中的任意数量的其他项,从而形成一条链。在这种情况下,它们之间的关系是一对一的。例如,考虑以下三种相关的项类型: 产品, 下单和 订单详情。 一个 下单 取决于 产品,而一个 订单详情 取决于对应的 下单。 如果 产品 物品已移除。 NCache 移除其依赖项 下单这反过来又使相关的 订单详情.

在中使用多级键依赖 NCache

多重依赖

单个缓存项可以依赖于缓存中的多个缓存项。同样,该缓存项也可能依赖于多个缓存项。这可能会导致缓存项之间存在“1:n”关系。例如,在缓存中,如果 下单 属于一个 客户 并包括一个 产品, 下单 取决于 客户 和 产品。 同样, 订单详细信息 取决于 订单管理。 下图直观地描述了整个场景。

在中使用多键依赖 NCache

如果 客户 被删除, 订单管理 和 订单详细信息 从缓存中移除。同样地,如果 产品 被删除, 订单管理 和 订单详细信息 已从缓存中移除。

注意:

您只能为 Key Dependency 指定缓存中现有的键。为 Key Dependency 指定不存在的键将导致 OperationFailedException. 而在添加时可以认为一个对象依赖于另一个键。

硬件需求

使用前 NCache 客户端 API 应确保满足以下先决条件:

  • 。NET
  • 爪哇岛
  • Python
  • Node.js
  • 遗留 API
  • 在您的 .NET 客户端应用程序中安装以下 NuGet 包:
    • NCache: Alachisoft.NCache.SDK
    • NCache 开源: Alachisoft.NCache.Opensource.SDK
  • 在您的应用程序中包含以下命名空间:
    • Alachisoft.NCache。客户
    • Alachisoft.NCache.Runtime.Exceptions
    • Alachisoft.NCache.运行时.依赖项
  • 缓存必须正在运行。
  • 确保要添加的数据是 可序列化.
  • 有关 API 详细信息,请参阅: 缓存, 缓存项, 缓存项属性, 依赖, 更新属性, 插页, 添加, 缓存依赖, 关键依赖.
  • 为您的 Java 客户端应用程序添加以下 Maven 依赖项: pom.xml 文件:
<dependency>
    <groupId>com.alachisoft.ncache</groupId>
    <!--for NCache-->
    <artifactId>ncache-client</artifactId>
    <version>x.x.x</version>
</dependency>
  • 在 Java 客户端应用程序中导入以下包:
    • com.alachisoft.ncache.client.*
    • com.alachisoft.ncache.runtime.exceptions.*
    • com.alachisoft.ncache.runtime.dependencies.*
  • 缓存必须正在运行。
  • 确保要添加的数据是 可序列化.
  • 有关 API 详细信息,请参阅: 缓存, 缓存项, 缓存项属性, 设置依赖, 更新属性, 插, 加, 缓存依赖, 关键依赖.
  • 在您的 Python 客户端应用程序中安装以下软件包:
    • NCache: ncache-客户端
  • 在您的应用程序中导入以下包:
    • ,来自 ncache.client 导入*
    • ,来自 ncache.runtime.dependencies 导入*
  • 缓存必须正在运行。
  • 确保要添加的数据是 可序列化.
  • 有关 API 详细信息,请参阅: 缓存, 缓存项, 缓存项属性, 设置依赖关系, 更新属性, 插, 加, 关键依赖.
  • 在您的Node.js客户端应用程序中安装并包含以下模块:
    • NCache: ncache-客户端
  • 在您的应用程序中包含以下类:
    • 缓存
    • 关键依赖
  • 缓存必须正在运行。
  • 确保要添加的数据是 可序列化.
  • 有关 API 详细信息,请参阅: 缓存, 缓存项, 缓存项属性, 设置依赖, 更新属性, 插页, 加, 关键依赖.
  • 创建一个新的控制台应用程序。
  • 在您的 .NET 客户端应用程序中安装以下任一 NuGet 包:
    • NCache: Install-Package Alachisoft.NCache.SDK -Version 4.9.1.0
  • 确保要添加的数据是 可序列化.
  • 添加 NCache 案例 通过定位 %NCHOME%\NCache\bin\assembly\4.0 并添加 Alachisoft.NCache.Web 和 Alachisoft.NCache.Runtime 作为适当的。
  • 包括 Alachisoft.NCache.Runtime.Dependencies 应用程序中的命名空间。
  • 要了解更多关于 NCache 旧版 API,请下载 NCache 4.9 可作为 邮编 文件在 Alachisoft 官方网站.

使用键依赖关系将 CacheItem 添加到缓存

此 CacheItem 是由提供的自定义类 NCache 它可用于向缓存添加数据,并允许您设置与此类对象关联的其他元数据。此元数据定义了项属性,例如依赖项、过期时间等。 添加 该方法会添加一个带有依赖元数据的新项,而 插页 如果键不存在,则该方法添加项;如果键已存在,则覆盖现有项。 CacheItem 缓存中已存在,它会覆盖其属性。

重要提示

请注意,此 API 还指定 CacheItem 驱逐和到期的优先级,其中该参数的值传递为 Default.

以下示例将客户添加到缓存中,并创建一个依赖于客户键的订单,这意味着一旦客户被更新或删除,该客户的订单也会从缓存中删除。

  • 。NET
  • 爪哇岛
  • Python
  • Node.js
  • 遗留 API
try
{
      // Precondition: Cache is already connected
      // Get customer from database
      Customer customer = FetchCustomerFromDB("ALFKI");

      // Add customer in cache
      cache.Add(customer.CustomerID, customer);

      // Get order from customer
      Order order = new Order
      {
            OrderID = 10248,
            OrderDate = DateTime.Now,
            ShipAddress = "Carrera 22 con Ave. Carlos Soublette #8-35"
      };

      // Save order to database
      bool isOrderSaved = SaveOrderToDB(order);

      // Generate order Id
      string orderKey = $"Order:{order.OrderID}";

      if (isOrderSaved)
      {
            // Generate an instance of Key Dependency
            CacheDependency dependency = new KeyDependency(customer.CustomerID);

            // Create CacheItem to your desired object
            CacheItem cacheItem = new CacheItem(order);

            // Add Key Dependency to order
            cacheItem.Dependency = dependency;

      // Add order in cache
            cache.Add(orderKey, cacheItem);
      }

      // Create order detail for the order
      OrderDetail orderDetail = new OrderDetail
      {
            OrderID = order.OrderID,
            UnitPrice = 200,
            Discount = 25.5F,
            Quantity = 10,
      };

      // Save order detail to database
      bool isOrderDetailSaved = SaveOrderDetailToDB(orderDetail);

      if (isOrderDetailSaved)
      {
            // Generate order detail Id
            string orderDetailId = $"OrderDetail: {orderDetail.OrderID}";

            // Generate an instance of Key Dependency
            CacheDependency dependency = new KeyDependency(orderKey);

            // Create CacheItem with your desired object
            CacheItem cacheItem = new CacheItem(orderDetail);

            // Apply dependency on order
            cacheItem.Dependency = dependency;

            // Insert order detail in cache with dependency
            cache.Add(orderDetailId, cacheItem);
      }
}
catch (OperationFailedException ex)
{
      // NCache specific exception

      if (ex.ErrorCode == NCacheErrorCodes.DEPENDENCY_KEY_NOT_FOUND)
      {
            // The dependent item does not exist in the cache
      }
      else
      {
            // Exception can occur due to:
            // Connection Failures
            // Operation Timeout
            // Operation performed during state transfer
      }
}
catch (Exception ex)
{
      // Any generic exception like ArgumentNullException or ArgumentException
}
try
{
      // Precondition: Cache is already connected
      // Get customer from database
      Customer customer = fetchCustomerFromDB("ALFKI");

      // Add customer in cache
      cache.add(customer.getCustomerID(), customer);

      // Get order from customer
      Order order = new Order(10248, LocalDateTime.now(), "Carrera 22 con Ave. Carlos Soublette #8-35");

      // Save order to database
      boolean isOrderSaved = saveOrderToDB(order);

      // Generate order Id
      String orderKey = "Order:" + order.getOrderID();

      if (isOrderSaved)
      {
            // Create CacheItem with your desired object
            CacheItem cacheItem = new CacheItem(order);

            // Generate an instance of Key Dependency
            cacheItem.setDependency(new KeyDependency(customer.getCustomerID()));

            // Insert order in cache
            cache.insert(orderKey, cacheItem);
      }

      // Create order detail for the order
      OrderDetail orderDetail = new OrderDetail(10248, 200, 25.5F, 10);

      // Save order detail to database
      boolean isOrderDetailSaved = SaveOrderDetailToDB(orderDetail);

      if (isOrderDetailSaved)
      {
            String orderDetailId = "OrderDetail:" + orderDetail.getOrderID();

            // Generate an instance of Key Dependency
            CacheDependency dependency = new KeyDependency(orderKey);

            // Create CacheItem with your desired object
            CacheItem cacheItem = new CacheItem(orderDetail);

            // Apply dependency on order
            cacheItem.setDependency(dependency);

            // Insert order detail in cache with dependency
            cache.insert(orderDetailId, cacheItem);
      }
}
catch (OperationFailedException ex)
{
      // NCache specific exception

      if (ex.getErrorCode() == NCacheErrorCodes.DEPENDENCY_KEY_NOT_FOUND)
      {
            // The dependent item does not exist in the cache
      }
      else
      {
            // Exception can occur due to:
            // Connection Failures
            // Operation Timeout
            // Operation performed during state transfer
      }
}
catch (Exception ex)
{
    // Any generic exception like IllegalArgumentException or NullPointerException
}
try:
    # Precondition: Cache is already connected
    # Get customer from database
    customer = fetch_customer_from_db("ALFKI")

    customer_key = f"Customer:{customer.get_customer_id()}"

    # Add customer in cache
    cache.add(customer_key, customer)

    # Get order from customer
    order = Order(
          order_id=10248,
          customer_id=customer.get_customer_id(),
          order_date=datetime.now(),
          ship_address="Carrera 22 con Ave. Carlos Soublette #8-35",
    )

    # Save order to database
    is_order_saved = save_order_to_db(order)

    # Generate order Id
    order_key = f"Order:{order.get_order_id()}"

    if is_order_saved:
          # Generate an instance of Key Dependency
          dependency = KeyDependency(customer_key)

          # Create CacheItem with your desired object
          cache_item = CacheItem(order)

          # Add Key Dependency to order
          cache_item.set_dependency(dependency)

          # Add order in cache
          cache.add(order_key, cache_item)

    # Create order detail for the order
    order_detail = OrderDetail(
          order_id=order.get_order_id(),
          unit_price=200,
          discount=25.5,
          quantity=10
    )

    # Save order detail to database
    is_order_detail_saved = save_order_detail_to_db(order_detail)

    if is_order_detail_saved:
          # Generate order detail Id
          order_detail_key = f"OrderDetail:{order_detail.get_order_id()}"

          # Generate an instance of Key Dependency
          dependency = KeyDependency(order_key)

          # Create CacheItem with your desired object
          cache_item = CacheItem(order_detail)

          # Apply dependency on order
          cache_item.set_dependency(dependency)

          # Insert order detail in cache with dependency
          cache.add(order_detail_key, cache_item)

except Exception as error:
    # Exception can occur due to:
    # Connection Failures
    # Operation Timeout
    # Operation during state transfer
    print("An error occurred:", str(error))
try
{
      // This is an async method
      // Precondition: Cache is already connected
      // Get customer from database
      const customer = await fetchCustomerFromDB("ALFKI");

      // Add customer in cache
      const customerKey = `Customer:${customer.customerID}`;
      const customerItem = new CacheItem(JSON.stringify(customer));
      await cache.add(customerKey, customerItem);

      // Get order from customer
      const order =
      {
            orderId: 10248,
            orderDate: new Date().toISOString(),
            shipAddress: "Carrera 22 con Ave. Carlos Soublette #8-35",
            customerId: customer.customerID
      };

      // Save order to DB first
      const isOrderSaved = await saveOrderToDB(order);

      // Generate order Id
      const orderKey = `Order:${order.orderId}`;

      // Save order to database
      if (isOrderSaved)
      {
            // Create CacheItem with your desired object
            const orderItem = new CacheItem(JSON.stringify(order));

            // Generate an instance of Key Dependency
            const orderDependency = new KeyDependency([customerKey]);

            // Add Key Dependency to order
            orderItem.setDependency(orderDependency);

            // Add order in cache
            await cache.add(orderKey, orderItem);
      }

      // Create order detail for the order
      const orderDetail =
      {
            orderId: order.orderId,
            unitPrice: 200,
            discount: 25.5,
            quantity: 10
      };

      // Save order detail to database
      const isOrderDetailSaved = await saveOrderDetailToDB(orderDetail);

      if (isOrderDetailSaved)

      {
            // Generate order detail Id
            const orderDetailKey = `OrderDetail:${orderDetail.orderId}`;

            // Create CacheItem with your desired object
            const orderDetailItem = new CacheItem(JSON.stringify(orderDetail));

            // Generate an instance of Key Dependency
            const detailDependency = new KeyDependency([orderKey]);

            // Apply dependency on order
            orderDetailItem.setDependency(detailDependency);

            // Add order detail in cache with dependency
            await cache.add(orderDetailKey, orderDetailItem);
      }
}
catch (error)
{
    // Handle any errors
}
// Using NCache 4.9.1
try
{
      // Precondition: Cache is already connected
      // Get customer from database
      Customer customer = FetchCustomerFromDB("ALFKI");

      // Add customer to cache
      cache.Add(customer.CustomerID, customer);

      // Get order from customer
      Order order = new Order
      {
            OrderID = 10248,
            OrderDate = DateTime.Now,
            ShipAddress = "Carrera 22 con Ave. Carlos Soublette #8-35"
      };

      // Save order to database
      bool orderSaved = SaveOrderToDB(order);

      // Generate order Id
      string orderKey = "Order:" + order.OrderID;

      // Save Order
      if (orderSaved)
      {
            cache.Add(orderKey, order, new KeyDependency(customer.CustomerID), Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, CacheItemPriority.Normal);
      }

      // Create OrderDetail
      OrderDetail orderDetail = new OrderDetail
      {
            OrderID = order.OrderID,
            UnitPrice = 200,
            Discount = 25.5F,
            Quantity = 10
      };

      // Save order's detail to database
      bool detailSaved = SaveOrderDetailToDB(orderDetail);

      // Generate order detail Id
      string detailKey = "OrderDetail:" + orderDetail.OrderID;

      if (detailSaved)
      {
            cache.Add(detailKey, orderDetail, new KeyDependency(orderKey), Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, CacheItemPriority.Normal);
      }
}
catch (OperationFailedException ex)
{
      // NCache specific exception

      if (ex.ErrorCode == NCacheErrorCodes.DEPENDENCY_KEY_NOT_FOUND)
      {
            // The dependent item does not exist in the cache
      }
      else
      {
            // Exception can occur due to:
            // Connection Failures
            // Operation Timeout
            // Operation performed during state transfer
      }
}
catch (Exception ex)
{
      // Any generic exception like ArgumentNullException or ArgumentException
}
注意:

为确保操作是故障安全的,建议处理应用程序中的任何潜在异常,如中所述 处理故障.

添加对多个键的键依赖

您还可以为依赖于多个键的项添加键依赖关系。这样,单个项就可以依赖于多个项。 添加 or 插页 方法。 的 Add 方法在缓存中添加一个新项目,而 Insert 方法添加一个具有依赖性的新项目,如果该项目已存在于缓存中,它将覆盖其属性。 以下示例演示了这一点。

  • 。NET
  • 爪哇岛
  • Python
  • Node.js
// Precondition: Cache is already connected
// Get customer from database
Customer customer = FetchCustomerFromDB();

// Get product from database
Product product = FetchProductFromDB();

// Add customer in cache
cache.Add(customer.CustomerID, customer);

// Add product to cache
string productId = $"Product: {product.ProductID}";
cache.Add(productId, product);

// Get order from customer
Order order = new Order
{
      OrderID = 10248,
      OrderDate = DateTime.Now,
      ShipAddress = "Carrera 22 con Ave. Carlos Soublette #8-35"
};

// Save order to database
bool isOrderSaved = SaveOrderToDB(order);

// Generate order Id
string orderKey = $"Order: {order.OrderID}";

if (isOrderSaved)
{
      string[] dependencyKeys = { productId, customer.CustomerID };
      // Generate an instance of Key Dependency
      CacheDependency dependency = new KeyDependency(dependencyKeys);

      // Create CacheItem with your desired object
      CacheItem cacheItem = new CacheItem(order);

      // Add Key Dependency to order
      cacheItem.Dependency = dependency;

      // Insert order into cache with dependencies
      cache.Insert(orderKey, cacheItem);
}

// Create order detail
OrderDetail orderDetail = new OrderDetail
{
      OrderID = order.OrderID,
      UnitPrice = 200,
      Discount = 25.5F,
      Quantity = 10,
};

// Save order's detail to database
bool isOrderDetailSaved = SaveOrderDetailToDB(orderDetail);

if (isOrderDetailSaved)
{
      // Generate order detail Id
      string orderDetailId = $"OrderDetail: {orderDetail.OrderID}";

      // Generate an instance of Key Dependency
      CacheDependency dependency = new KeyDependency(orderKey);

      // Create CacheItem with your desired object
      CacheItem cacheItem = new CacheItem(orderDetail);

      // Add Key Dependency to order
      cacheItem.Dependency = dependency;

      //  Insert order detail into cache with dependencies
      cache.Insert(orderDetailId, cacheItem);
}
// Precondition: Cache is already connected
// Get customer from database
Customer customer = Customer.fetchCustomerFromDB("Customer:ALFKI");

// Get product from database
Product product = Product.fetchProductFromDB("Product:1001");

// Add customer in cache
cache.add(customer.getCustomerID(), customer);

// Add product to cache
String productId = product.getProductID();
cache.add(productId, product);

// Get order from customer
Order order = new Order(10248, LocalDateTime.now(), "Carrera 22 con Ave. Carlos Soublette #8-35");

// Save order to database
boolean isOrderSaved = saveOrderToDB(order);

// Generate order Id
String orderKey = "Order:" + order.getOrderID();

if (isOrderSaved)
{
      List<String> dependencyKeys = Arrays.asList(productId, customer.getCustomerID());

      // Generate an instance of Key Dependency
      CacheDependency dependency = new KeyDependency(dependencyKeys);

      // Create CacheItem to with your desired object
      CacheItem cacheItem = new CacheItem(order);

      // Add Key Dependency to order
      cacheItem.setDependency(dependency);

      // Insert order into cache with dependencies
      cache.insert(orderKey, cacheItem);
}

// Create order detail
OrderDetail orderDetail = new OrderDetail(10248, 200, 25.5F, 10);

// Save order detail to database
boolean isOrderDetailSaved = saveOrderDetailToDB(orderDetail);

if (isOrderDetailSaved)
{
      // Generate order detail Id
      String orderDetailId = "OrderDetail:" + orderDetail.getOrderID();

      // Generate an instance of Key Dependency
      CacheDependency dependency = new KeyDependency(orderKey);

      // Create CacheItem with your desired object
      CacheItem cacheItem = new CacheItem(orderDetail);

      // Add Key Dependency to order
      cacheItem.setDependency(dependency);

      // Insert order detail into cache with dependencies
      cache.insert(orderDetailId, cacheItem);
}
# Precondition: Cache is already connected
# Get customer from database
customer = fetch_customer_from_db("ALFKI")

# Get product from database
product = fetch_product_from_db("1001")

# Add customer in cache
customer_key = f"Customer:{customer.get_customer_id()}"
cache.add(customer_key, customer)

# Add product to cache
product_key = f"Product:{product.get_product_id()}"
cache.add(product_key, product)

# Get order from customer
order = Order(
      order_id=10248,
      customer_id=customer.get_customer_id(),
      order_date=datetime.now(),
      ship_address="Carrera 22 con Ave. Carlos Soublette #8-35"
)

# Save order to database
is_order_saved = save_order_to_db(order)

# Generate order Id
order_key = f"Order:{order.get_order_id()}"

if is_order_saved:
      dependency_keys = [product_key, customer_key]

      # Generate an instance of Key Dependency
      dependency = KeyDependency(dependency_keys)

      # Create CacheItem with your desired object
      cache_item = CacheItem(order)

      # Add Key Dependency to order
      cache_item.set_dependency(dependency)

      # Insert order into cache with dependencies
      cache.insert(order_key, cache_item)

# Create order detail
order_detail = OrderDetail(
      order_id=order.get_order_id(),
      unit_price=200,
      discount=25.5,
      quantity=10
)

# Save order's detail to database
is_order_detail_saved = save_order_detail_to_db(order_detail)

if is_order_detail_saved:

      # Generate order detail Id
      order_detail_key = f"OrderDetail:{order_detail.get_order_id()}"

      # Generate an instance of Key Dependency
      dependency = KeyDependency(order_key)

      # Create CacheItem with your desired object
      cache_item = CacheItem(order_detail)

      # Add Key Dependency to order
      cache_item.set_dependency(dependency)

      #  Insert order detail into cache with dependencies
      cache.insert(order_detail_key, cache_item)
// Precondition: Cache is already connected
// Get customer from database
const customer = await fetchCustomerFromDB();

// Get product from database
const product = await fetchProductFromDB();

// Add customer in cache
const customerKey = customer.customerID;
const customerItem = new CacheItem(JSON.stringify(customer));
await cache.add(customerKey, customerItem);
console.log(`Customer ${customer.customerID} added to cache`);

// Add product to cache
const productKey = `Product:${product.productID}`;
const productItem = new CacheItem(JSON.stringify(product));
await cache.add(productKey, productItem);
console.log(`Product ${product.productID} added to cache`);

// Get order from customer
const order = {
orderId: 10248,
orderDate: new Date().toISOString(),
shipAddress: "Carrera 22 con Ave. Carlos Soublette #8-35"
};

// Save order to database
const isOrderSaved = await saveOrderToDB(order);

// Generate order Id
const orderKey = `Order:${order.orderId}`;

if (isOrderSaved) {
// Generate an instance of Key Dependency
const dependency = new KeyDependency([productKey, customerKey]);

// Create CacheItem with your desired object
const orderItem = new CacheItem(JSON.stringify(order));

// Add Key Dependency to order
orderItem.setDependency(dependency);

// Insert order into cache with dependencies
await cache.insert(orderKey, orderItem);

}

// Create order detail
const orderDetail = {
orderId: order.orderId,
unitPrice: 200,
discount: 25.5,
quantity: 10
};

// Save order's detail to database
const isOrderDetailSaved = await saveOrderDetailToDB(orderDetail);

if (isOrderDetailSaved) {

// Generate order Id
const orderDetailKey = `OrderDetail:${orderDetail.orderId}`;

// Generate an instance of Key Dependency
const detailDependency = new KeyDependency([orderKey]);

// Create CacheItem with your desired object
const orderDetailItem = new CacheItem(JSON.stringify(orderDetail));

// Add Key Dependency to order
orderDetailItem.setDependency(detailDependency);

//  Insert order detail into cache with dependencies
await cache.insert(orderDetailKey, orderDetailItem);
}

将键依赖添加到现有缓存项

NCache 允许您将密钥依赖项添加到缓存中已存在的项,而无需将其重新插入缓存。此过程通过 CacheItemAttributes 具有以下属性的类 Dependency 反对 CacheItem。 然后使用该项目的现有键设置该属性 UpdateAttributes 的方法 Cache 类。

以下示例假设缓存中已存在订单和产品。它将产品键与订单关联起来。更新或删除产品会同时删除关联的订单。

  • 。NET
  • 爪哇岛
  • Python
  • Node.js
// Precondition: Cache is already connected
string orderId = "Order:10248";
string ProductId = "Product:8899";

// Create a Key Dependency
CacheDependency dependency = new KeyDependency(ProductId);

// Create a cache item attribute
CacheItemAttributes itemAttribute = new CacheItemAttributes();

// Set dependency to cache item attribute
itemAttribute.Dependency = dependency;

// Update attribute in cache
cache.UpdateAttributes(orderId, itemAttribute);
// Precondition: Cache is already connected
String orderId = "Order:10248";
String productId = "Product:1001";

// Create a Key Dependency
CacheDependency dependency = new KeyDependency(productId);

// Create a cache item attribute
CacheItemAttributes itemAttributes = new CacheItemAttributes();

// Set dependency to cache item attribute
itemAttributes.setDependency(dependency);

// Update attribute in cache
cache.updateAttributes(orderId, itemAttributes);
# Precondition: Cache is already connected
order_id = "Order:10248"
product_id = "Product:8899"

# Create a Key Dependency
dependency = KeyDependency([product_id])

# Create a cache item attribute
attributes = CacheItemAttributes()

# Set dependency to cache item attribute
attributes.set_dependency(dependency)

# Update attribute in cache
cache.update_attributes(order_id, attributes)
// Precondition: Cache is already connected
// This is an async method

// Precondition: both the items already exist in the cache
const orderId = "Order:10248";
const productId = "Product:8899";

// create Key Dependency where order is dependent on customer
const dependency = new KeyDependency([productId]);

// Create a CacheItemAttributes for dependency
const itemAttributes = new CacheItemAttributes();

// Set dependency to cache item attribute
itemAttributes.setDependency(dependency);

// Update attribute in cache
const result = await cache.updateAttributes(orderId, itemAttributes);

更多资讯

NCache 提供了关键依赖的示例应用程序 GitHub上.

参见

.NET: Alachisoft.NCache.运行时.依赖项 命名空间。
Java的: com.alachisoft.ncache.runtime.dependencies 命名空间。
Python: ncache.runtime.dependencies 模块。
节点.js: 关键依赖 类。

联系我们

联系电话

+1 214-619-2601   (美国)

+44 20 7993 8327   (英国)

 
邮箱地址

sales@alachisoft.com

support@alachisoft.com

NCache
  • 版本比较
  • NCache 卓越
  • 基准
尝试 NCache
定价
NCache 兒童遊樂區

部署
  • 云端
  • Docker
  • Kubernetes
  • 内部部署
技术用例
  • ASP.NET 会话
  • ASP.NET Core 会话
  • 发布/订阅消息
  • SignalR 背板
  • 物联网(IOT)
  • NoSQL 数据库
  • 流处理
  • 微服务
相关资源
  • 博客
  • Foundations
  • 视频
  • 比较
  • 作品
  • 白皮书
  • 专题文章
  • 技术讲座
  • 杂志文章
  • 第三方文章
客户案例研究
  • 评价
  • 客户
支持
  • 文档
  • 安排演示
  • 小技巧
关于我们
  • 领导团队
  • 合作伙伴
  • 新闻
  • 活动
  • 加入我们
联系我们

  • 英语简体中文法语版德语版意大利语版日语版韩语版葡萄牙语西班牙语

  • 使用条款
  •  
  • 隐私政策
  •  
  • Cookie政策
©版权所有 Alachisoft 2002 - 版权所有。 NCache 是 Diyatech Corp. 的注册商标。
  • Facebook
  • Twitter
  • Youtube
  • LinkedIn
  • RSS