キャッシュ内のスライド有効期限
スライディング有効期限 NCache は、頻繁にアクセスされるデータを保持しつつ、一定期間使用されていないデータを自動的に削除する動的なメモリ管理戦略です。アクセスごとに有効期限(TTL)をリセットすることで、使用されていないデータを削除しつつ、頻繁にアクセスされる項目をキャッシュ内に保持します。
スライディング有効期限設定では、指定された時間間隔内にデータにアクセスがあった場合、その特定のキャッシュエントリの有効期限(TTL)がリセットされます。これにより、特定のキャッシュエントリの有効期限切れを防ぎ、アプリケーションのアクティビティが高い時でもデータを最新の状態に保つことができます。例えば、有効期限が10分のセッションが追加されたとします。セッションが6分目にアクセスされた場合、キャッシュ内のセッションの有効期間はさらに10分延長されます。
前提条件
使用する前に NCache クライアント側APIについては、以下の前提条件が満たされていることを確認してください。
- .NET クライアント アプリケーションに次の NuGet パッケージをインストールします。
- アプリケーションに次の名前空間を含めます。
- キャッシュが実行されている必要があります。
- 追加するデータが シリアライズ可能.
- APIの詳細については、以下を参照してください。 Iキャッシュ, キャッシュ項目, 含まれています, 数量カウント, 満了, インサート.
- Java クライアント アプリケーションに次の Maven 依存関係を追加します。
pom.xml ファイル:
<dependency>
<groupId>com.alachisoft.ncache</groupId>
<!--for NCache Enterprise-->
<artifactId>ncache-client</artifactId>
<version>x.x.x</version>
</dependency>
- Python クライアント アプリケーションに次のパッケージをインストールします。
- アプリケーションに次のパッケージをインポートします。
- キャッシュが実行されている必要があります。
- 追加するデータが シリアライズ可能.
- APIの詳細については、以下を参照してください。 キャッシュ, キャッシュ項目, 含まれています, get_count, insert, 満了.
- 次のモジュールをインストールして、Node.js クライアント アプリケーションに含めます。
- アプリケーションに次のクラスを含めます。
- キャッシュが実行されている必要があります。
- 追加するデータが シリアライズ可能.
- APIの詳細については、以下を参照してください。 キャッシュ, キャッシュ項目, 含まれています, getCount, 満了, insert.
- .NET クライアント アプリケーションに次のいずれかの NuGet パッケージをインストールします。
- エンタープライズ:
Install-Package Alachisoft.NCache.SDK -Version 4.9.1.0
- 新しいコンソール アプリケーションを作成します。
- 追加するデータが シリアライズ可能.
- 追加 NCache 参考情報 見つけることによって
%NCHOME%\NCache\bin\assembly\4.0 と追加 Alachisoft.NCache.Web (NAIST) と Alachisoft.NCache.Runtime 適切に。
- を含める
Alachisoft.NCache.Web.Caching アプリケーション内の名前空間。
- の詳細については NCache レガシーAPIをダウンロードしてください NCache 4.9 文書は .zip 上のファイル Alachisoft ウェブサイト.
キャッシュアイテムを追加または更新して有効期限を設定するには、 インサート 方法。 ザ CacheItem によって提供されるカスタムクラスです NCache これを使用すると、キャッシュにデータを追加でき、このクラスのオブジェクトに関連付けられた追加のメタデータを設定することもできます。 このメタデータは、有効期限、依存関係などのアイテムのプロパティを定義します。
次の例では、の値を設定します Expiration 5分間のスライディング有効期限のプロパティ CacheItem キーが存在しない場合はキャッシュに追加し、存在する場合はアイテムを更新します。 CacheItem 5 分後にキャッシュから削除されます。
try
{
// Precondition: Cache is already connected
// Get product from database against given product ID
Product product = FetchProductFromDB(1001);
// Generate a unique cache key for this product
string key = $"Product:{product.ProductID}";
// Create a new CacheItem for this product
var cacheItem = new CacheItem(product);
// Resetting TTL to optimize memory
var expiration = new Expiration(ExpirationType.Sliding, TimeSpan.FromMinutes(5));
cacheItem.Expiration = expiration;
// Add/Update item with expiration
cache.Insert(key, cacheItem);
}
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 product from database against given product ID
Product product = fetchProductFromDB(1001);
// Generate a unique cache key for this product
String key = "Product:" + product.getProductID();
// Create a new CacheItem for this product
CacheItem cacheItem = new CacheItem(product);
// Set Expiration type and TimeSpan
Expiration expiration = new Expiration(ExpirationType.Sliding, TimeSpan.FromMinutes(5));
cacheItem.setExpiration(expiration);
// Add/Update item in cache with expiration
cache.insert(key, cacheItem);
}
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 product from 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
cache_item = ncache.CacheItem(product)
# Set Expiration type and TimeSpan
expiration = ncache.Expiration(ncache.ExpirationType.SLIDING, ncache.TimeSpan.from_minutes(5))
cache_item.set_expiration(expiration)
# Add/Update item in cache with expiration
cache.insert(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 product from database against given product ID
var product = this.fetchProductFromDB(1001);
// Generate a unique cache key for this product
var key = "Product:" + product.getProductID();
// Create a new CacheItem for this product
// You also need to specify the FQN (Fully Qualified Name) of the class
var cacheItem = new ncache.CacheItem(product,"FQN.Product");
// Set Expiration type and TimeSpan
var expiration = new ncache.Expiration(ncache.ExpirationType.Sliding, ncache.TimeSpan.FromMinutes(5));
cacheItem.setExpiration(expiration);
// Add/Update item in cache with expiration
await this.cache.insert(key, cacheItem);
}
catch (error)
{
// Handle errors
}
// Using NCache Enterprise 4.9.1
try
{
// Precondition: Cache is already connected
Product product = FetchProductFromDB("1001");
// Generate a unique cache key for this product
string key = $"Product:{product.ProductID}";
CacheItem cacheItem = new CacheItem(product);
// Adding sliding expiration of 5 min through Add API
cache.Add(key, product, null, Cache.NoAbsoluteExpiration, new TimeSpan(0, 5, 0), CacheItemPriority.Default);
}
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
}
お願い
操作がフェイルセーフであることを保証するために、で説明されているように、アプリケーション内の潜在的な例外を処理することをお勧めします。 失敗の処理.
API でスライディング有効期限を指定するには、 DefaultSliding (NAIST) と DefaultSlidingLonger で設定された値 NCache 管理センターまたは config.ncconf。 これにより、必要に応じて、コードを変更せずに値を外部から変更できる柔軟性が得られます。
次のコードは、設定されたデフォルトの有効期限(DefaultSliding (この場合)アイテムとキーを関連付け、キーが存在しない場合はキャッシュに追加し、既存のアイテムを新しい有効期限で更新します。設定された時間間隔内にアイテムにアクセスされない場合、アイテムは期限切れになります。アクセスされた場合は、同じ時間間隔でアイテムの有効期限が延長されます。
// Precondition: Cache is already connected
// Get product from database against given product ID
Product product = FetchProductFromDB(1001);
// Generate a unique cache key for this product
string key = $"Product:{product.ProductID}";
// Create a cacheItem instance for product
var cacheItem = new CacheItem(product);
// Set Expiration TimeSpan
var expiration = new Expiration(ExpirationType.DefaultSliding);
cacheItem.Expiration = expiration;
// Resetting TTL to optimize memory
CacheItemVersion version = cache.Insert(key, cacheItem);
// Precondition: Cache is already connected
// Get product from database against given product ID
Product product = fetchProductFromDB(1001);
// Generate a unique cache key for this product
String key = "Product:" + product.getProductID();
// Create a cacheItem instance for product
CacheItem cacheItem = new CacheItem(product);
// Set Expiration type
Expiration expiration = new Expiration(ExpirationType.DefaultSliding);
cacheItem.setExpiration(expiration);
// Add/Update item into the cache with expiration
CacheItemVersion version = cache.insert(key, cacheItem);
# Precondition: Cache is already connected
# Get product from database against given product ID
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
cache_item = ncache.CacheItem(product)
# Set Expiration type and TimeSpan
expiration = ncache.Expiration(ncache.ExpirationType.DEFAULT_SLIDING)
cache_item.set_expiration(expiration)
# Add/Update item in cache with expiration
version = cache.insert(key, cache_item)
// Precondition: Cache is already connected
// This is an async method
// Get product from database against given product ID
var product = this.fetchProductFromDB(1001);
// Generate a unique cache key for this product
var key = "Product:" + product.getProductID();
// Create a cacheItem instance for product
// You also need to specify the FQN(Fully Qualified Name) of the class
var cacheItem = new ncache.CacheItem(product,"FQN.Product");
// Set Expiration type
var expiration = new ncache.Expiration(ncache.ExpirationType.DefaultSliding);
cacheItem.setExpiration(expiration);
// Add/Update item into the cache with expiration
var version = await this.cache.insert(key, cacheItem);
// Using NCache Enterprise 4.9.1
// Precondition: Cache is already connected
// Get product from database against given product ID
Product product = FetchProductFromDB(1001);
// Generate a unique cache key for this product
string key = $"Product:{product.ProductID}";
CacheItem cacheItem = new CacheItem(product);
// Adding Default Sliding expiration with configured value
cache.Add(key, product, null, Cache.NoAbsoluteExpiration, Cache.DefaultSliding, CacheItemPriority.Default);
その他のリソース
NCache スライディング有効期限のサンプルアプリケーションを提供します GitHub.
も参照してください
。ネット: Alachisoft.NCache.ランタイム.キャッシュ 名前空間
Java: com.alachisoft.ncache.runtime.caching 名前空間
Python: ncache.runtime.caching モジュールを開きます。
Node.js: 満了 とに提供されます。