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

更新/插入缓存数据

NCache 提供 Insert 方法及其重载用于添加或更新缓存数据。如果键已存在, Insert 替换现有条目;否则,添加新条目。当应用程序需要检测或阻止并发更新时,请使用锁定或条目版本控制。

硬件需求

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

  • 。NET
  • 爪哇岛
  • Python
  • Node.js
  • 遗留 API
  • 在您的 .NET 客户端应用程序中安装以下 NuGet 包:
    • NCache: Alachisoft.NCache.SDK
    • NCache 开源: Alachisoft.NCache.Opensource.SDK
  • 在您的应用程序中包含以下命名空间:
    • Alachisoft.NCache。客户
    • Alachisoft.NCache.Runtime.Exceptions
  • 缓存必须正在运行。
  • 确保要添加的数据是 可序列化.
  • 有关 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.*
  • 缓存必须正在运行。
  • 确保要添加的数据是 可序列化.
  • 有关 API 详细信息,请参阅: 缓存, 缓存项版本, 缓存项, 插.
  • 在您的 Python 客户端应用程序中安装以下软件包:
    • NCache: ncache-客户端
  • 在您的应用程序中导入以下包:
    • ,来自 ncache.client 导入*
  • 缓存必须正在运行。
  • 确保要添加的数据是 可序列化.
  • 有关 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.Web.Caching 应用程序中的命名空间。
  • 要了解更多关于 NCache 旧版 API,请下载 NCache 4.9 可作为 邮编 文件在 Alachisoft 官方网站.

如何更新缓存中的对象

此 Insert 该方法是应用程序层和分布式存储之间进行基本同步的标准方法。您可以使用各种重载来更新缓存中的自定义对象。 Insert 方法。 这会返回一个 CacheItemVersion.

重要提示
  • 如果 key 已经存在,它将用相同的 key 覆盖现有的缓存项。
  • 如果该键不存在,则该操作将该项添加到缓存中。

以下示例获取现有对象的值,并将其根据相同的键插入到缓存中。 如果该键不存在,则将其添加到缓存中。

  • 。NET
  • 爪哇岛
  • Python
  • Node.js
  • 遗留 API
try
{
  // Precondition: Cache is already connected
  string customerKey = $"Customer:ALFKI";

  // Get updated customer from database against given customer key
  Customer customer = FetchCustomerFromDB(customerKey);

  // Insert Customer object to cache
  CacheItemVersion version = cache.Insert(customerKey, customer);

  // Item updated in cache successfully
}
catch (OperationFailedException ex)
{
    // 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 updated product from database against given product ID
  Product product = fetchProductFromDB(1001);

  // Insert Product object to cache
  CacheItemVersion version = cache.insert("1001", product);

  // Item updated in cache successfully
}
catch (OperationFailedException ex)
{
    // 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 updated product from database against given product ID
    product = fetch_product_from_db(1001)

    # Generate a unique cache key for this product
    key = "Product:" + product.get_product_id()

    # Insert Product object to cache
    version = cache.insert(key, product)

    # Item updated in cache successfully

except Exception as error:
    # Exception can occur due to:
    # Connection Failures
    # Operation Timeout
    # Operation performed during state transfer
    print(f"An error occurred: {error}")
try
{
  // Precondition: Cache is already connected

  //Get updated product from database against given product ID
  product = await this.fetchProductFromDB(1001);

  // Generate a unique cache key for this product
  var key = "Product:" + product.getProductID();

  // Insert Product object to cache
  var version = await this.cache.insert(key, product);

  // Item updated in cache successfully
}
catch(error)
{
    // Exception can occur due to:
    // Connection Failures
    // Operation Timeout
    // Operation performed during state transfer
}
// Using NCache 4.9.1

try
{
  // Precondition: Cache is already connected

  string customerKey = $"Customer:ALFKI";

  // Get updated customer from database against given customer key
  Customer customer = FetchCustomerFromDB(customerKey);

  // Insert Customer object to cache
  CacheItemVersion version = cache.Insert(customerKey, customer);

  // Item updated in cache successfully
}
catch (OperationFailedException ex)
{
    // Exception can occur due to:
    // Connection Failures
    // Operation Timeout
    // Operation performed during state transfer
}
catch (Exception ex)
{
    // Any generic exception like ArgumentNullException or ArgumentException
}
注意:

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

如何更新带有过期时间的对象

CacheItem 是由提供的自定义类 NCache 它可用于向缓存添加数据,还可以将与对象关联的其他规范设置为此类的属性。

此 CacheItem 根据唯一键在缓存中更新。通过为缓存分配过期策略 CacheItem缓存可实现自动内存管理,限制项目在缓存中保留的时间。

重要提示
  • 如果 key 已经存在,它将用相同的 key 覆盖现有的缓存项。
  • 如果该键不存在,则该操作将该项添加到缓存中。

以下示例获取现有 CacheItem 并将其与相同的键对应的值插入到缓存中。如果该键不存在,则将其添加到缓存中。

  • 。NET
  • 爪哇岛
  • Python
  • Node.js
  • 遗留 API
// Precondition: Cache is already connected
string customerKey = $"Customer:ALFKI";

// Get the customer from the database
Customer customer = FetchCustomerFromDB(customerKey);

// Update Customer's Phone
customer.Phone = "0921-12 34 65";

// Create CacheItem
CacheItem cacheItem = new CacheItem(customer);
cacheItem.Expiration = new Expiration(ExpirationType.Sliding, TimeSpan.FromMinutes(5));

// Update customer in DB and Cache
if (UpdateDB(customer))
{
    cache.Insert(customerKey, cacheItem);
}
// Precondition: Cache is already connected
String customerKey = "ALFKI";

// Get the customer from the database
Customer customer = fetchCustomerFromDB(customerKey);

// Update Customer's Phone
customer.setPhone("0921-12 34 65");

// Create CacheItem
CacheItem cacheItem = new CacheItem(customer);
cacheItem.setExpiration(new Expiration(ExpirationType.Sliding, TimeSpan.FromMinutes(5)));

// Update customer in DB and Cache
cache.insert(customerKey, cacheItem);
# Precondition: Cache is already connected
# Get the customer from the database
product = fetch_product_from_db()

# Generate a unique cache key for this product
key = "Product:" + product.get_product_id()

# Create a new CacheItem for this product
# You can OPTIONALLY specify multiple properties e.g.Priority, Expiration
# These properties are explained in successive chapters
cache_item = ncache.CacheItem(product)

# Insert CacheItem to cache
version = cache.insert(key, cache_item)
// Precondition: Cache is already connected
// Get the customer from the database
product = await this.fetchProductFromDB();

// Generate a unique cache key for this product
var key = "Product:" + product.getProductID();

// Create a new CacheItem for this product
// You can OPTIONALLY specify multiple properties e.g. Priority, Expiration
// These properties are explained in successive chapters
var cacheItem = new ncache.CacheItem(product);

// Insert CacheItem to cache
var version = await this.cache.insert(key,cacheItem);
// Using NCache 4.9.1
// Precondition: Cache is already connected

string customerKey = $"Customer:ALFKI";

// Get the customer from the database
Customer customer = FetchCustomerFromDB(customerKey);

// Update Customer's Phone
customer.CustomerPhone = "0921-12 34 65";

CacheItem cacheItem = new CacheItem(customer);
cache.Insert(customerKey, customer, null, Cache.NoAbsoluteExpiration, new TimeSpan(0, 5, 0), CacheItemPriority.Default);

更多资讯

NCache 提供了基本操作的示例应用程序 GitHub上.

参见

.NET: Alachisoft.NCache。客户 命名空间。
Java的: com.alachisoft.ncache.client 命名空间。
Python: ncache.客户端 类。
节点.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