グループを使用してキャッシュデータを取得する
アイテムがキャッシュ データ内の特定のグループに追加されると、ユーザーはそのグループに関連付けられた特定のキャッシュ アイテムまたはキーを取得することもできます。
グループを使用してキャッシュ データを取得するための前提条件
特定のグループのキーを取得する
キャッシュ データ内の指定されたグループに属するキーのリストを返すには、この API に渡されたグループにマッピングされているキーのリストを返す GetGroupKeys メソッドを使用できます。
重要
- グループに空の文字列を渡すと、空の結果セットが返されます。
- グループの値のみを渡すと、グループの下にマップされたすべてのキーが返されます。
- グループのこの API に null を渡すと、ArgumentNullException がスローされます。
// Preconditions: Cache is connected
// A user wants to get the cache keys of all the customers from the West Coast
string groupName = "West Coast Customers";
ICollection<string> keys = cache.SearchService.GetGroupKeys(groupName);
if (keys != null && keys.Count > 0)
{
// Iterate over the result
foreach (var key in keys)
{
Console.WriteLine($"Key '{key}' belongs to '{groupName}' group.");
}
}
// Preconditions: Cache is connected
// A user wants to get the cache keys of all the customers from the West Coast
String groupName = "West Coast Customers";
Collection<String> keys = null;
keys = cache.getSearchService().getGroupKeys(groupName);
if (keys != null && !keys.isEmpty()) {
// Iterate over the result
for (String key : keys) {
System.out.println("Key '" + key + "' belongs to '" + groupName + "' group.");
}
}
// This is an async method
// Data with this group already exists in cache
var groupName = "Important Customers";
var searchService = await this.cache.getSearchService();
var retrievedKeys = await searchService.getGroupKeys(groupName);
if (retrievedKeys != null && retrievedKeys.size() > 0)
{
// Iterate over the result
retrievedKeys.forEach(key => {
// Perform Operations
});
}
else
{
// No data against the group found
}
# Data with this group already exists in cache
group_name = "Important Customers"
search_service = cache.get_search_service()
retrieved_keys = search_service.get_group_keys(group_name)
if retrieved_keys is not None and len(retrieved_keys) > 0:
# Iterate over the result
for key in retrieved_keys:
# Perform Operations
print(key)
else:
# No data against the group found
print("No keys found")
Note
操作がフェイルセーフであることを保証するために、「」で説明されているように、アプリケーション内で潜在的な例外を処理することをお勧めします。 失敗の処理.
特定のグループのキーと値を取得する
キャッシュ データ内の指定されたグループに属するキーと値の辞書を返すには、 GetGroupData
という方法が使えます。 このメソッドは、この API に渡されたグループの下にマップされたキーと値の辞書を返します。
次の例では、グループに属するキーと値を取得します 西海岸のお客様.
// A user wants to get the cache keys along with the values of all the customers from the West Coast
string groupName = "West Coast Customers";
IDictionary<string, Customer> retrievedDictionary = cache.SearchService.GetGroupData<Customer>(groupName);
if (retrievedDictionary != null)
{
foreach (KeyValuePair<string, Customer> retrievedItem in retrievedDictionary)
{
Console.WriteLine($"Customer '{retrievedItem.Value.ContactName}' having Key '{retrievedItem.Key}' belongs to West Coast");
}
}
// A user wants to get the cache keys along with the values of all the customers from the West Coast
String groupName = "West Coast Customers";
Map<String, Customer> retrievedDictionary = null;
retrievedDictionary = cache.getSearchService().<Customer>getGroupData(groupName);
if (retrievedDictionary != null && !retrievedDictionary.isEmpty()) {
for (java.util.Map.Entry<String, Customer> retrievedItem : retrievedDictionary.entrySet()) {
System.out.println("Customer '" + retrievedItem.getValue().getCustomerID() + "' having Key '" + retrievedItem.getKey() + "' belongs to West Coast");
}
}
// This is an async method
// Data with this group already exists in cache
var groupName = "Important Customers";
var searchService = await this.cache.getSearchService();
var result = await searchService.getGroupData(groupName);
if (result != null && result.size() > 0)
{
// Iterate over the result
result.forEach(item => {
// Perform Operations
});
}
else
{
// No data against the group found
}
# Data with this group already exists in cache
group_name = "Important Customers"
search_service = cache.get_search_service()
result = search_service.get_group_data(group_name)
if result is not None and len(result) > 0:
# Iterate over the result
for key in result:
# Perform Operations
print(result[key])
else:
# No data against the group found
print("No data found")
その他のリソース
NCache 上のグループのサンプルアプリケーションを提供します GitHubの.
も参照してください
。ネット: Alachisoft.NCache。クライアント 名前空間
Java: comの。alachisoft.ncache。クライアント 名前空間
Node.js: キャッシュ とに提供されます。
Python: ncache。クライアント とに提供されます。