効率的なデータ管理には、特定の属性に基づいてアプリケーション データをすばやく検索する機能が必要です。 データ量が増えるにつれて、この必要性はさらに重要になります。 通常、データベースにはデータの分類を容易にするフィールドが既にあり、これらのフィールドはデータをキャッシュするときにインデックスとして使用できます。 ただし、タグやその他のカスタム属性など、キャッシュに存在しない属性に基づいてデータをグループ化したい場合があります。
NCacheでは、カスタム属性に基づいてアプリケーション データを分類できるだけでなく、これらの属性に基づいて検索を実行することもできます。 従来の索引付けに加えて、 NCache を使用してデータを分類するオプションを提供します グループ, タグ, 名前付きタグ、データの分類と検索の柔軟性が向上します。
グループによるキャッシュデータの分類
キャッシュされ、グループ名で識別できるアイテムのコレクションについては、グループを作成できます。 この名前は、グループに関連するアイテムを取得するのに役立ちます。 たとえば、特定の顧客または特定の製品カテゴリに関連付けられたすべての製品を含むグループを作成できます。 名前を呼ぶだけで NCache それに関連付けられているすべてのキャッシュされたアイテムを返します。 XNUMX つのグループに関連付けられるアイテムは XNUMX つだけであることに注意してください。 以下の例は、別の地域 (この場合は東海岸) の顧客がオンライン ストアでどのようにグループ化されるかを示しています。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
// Pre-conditions: Cache is already connected. // Create a unique cache key for this customer. string customerKey = $"Customer:ALFKI"; // Get customer from cache Customer customer = cache.Get<Customer>(customerKey); // Get customer from database if not found in cache if (customer == null) { customer = FetchCustomerFromDB("ALFKI"); // Create a new CacheItem var cacheItem = new CacheItem(customer); // Specify the group cacheItem.Group = "East Coast Customers"; // Add customer object to cache with group cache.Add(customerKey, cacheItem); } |
SQL クエリを使用して、キーワード $Group$ を使用して、グループに含まれるキャッシュ オブジェクトを検索できます。 次のコード例は、SQL クエリを使用して、特定の地域 (この例では西海岸) のすべての顧客を表示する方法を示しています。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
// Pre-conditions: Items with groups exist in the cache // Custom class is query indexed through the NCache Manager or config.ncconf // Create an SQL Query with the specified criteria // Make sure to use the Fully Qualified Name (FQN) string query = "SELECT CustomerID,ContactName FROM Alachisoft.NCache.Samples.Data.Customer WHERE $Group$ = ? "; // Use QueryCommand for query execution var queryCommand = new QueryCommand(query); // Providing parameters for query queryCommand.Parameters.Add("$Group$", "West Coast Customers"); // Executing the Query ICacheReader reader = cache.SearchService.ExecuteReader(queryCommand); // Read results if result set is not empty if (reader.FieldCount > 0) { while (reader.Read()) { // Get the value of the result set string customerID = reader.GetValue<string>("CustomerID"); string customerName = reader.GetValue<string>("ContactName"); Console.WriteLine($"Customer '{customerName}' having ID '{customerID}' belongs to West Coast."); } } else { Console.WriteLine($"No customers from West Coast found"); } |
グループを使用すると、効率が向上するだけでなく、ユーザーが次のことを簡単に行うことができます。 検索する or 削除 アプリケーションのスケーラビリティを向上させるこれらの論理カテゴリに基づくデータ。 さらに、グループは、特定の検索条件に一致する一般的に使用されるデータをキャッシュできるため、データベース検索を繰り返し実行するコストを回避できます。
タグによるキャッシュデータの分類
タグ in NCache 複数のタグを使用して、キャッシュされたアイテムを分類する方法を提供します。 キャッシュされたオブジェクトには、アイテムの取得に使用できる XNUMX つ以上のタグを割り当てることができます。 たとえば、地域、言語、または製品の機能に従ってアイテムを分類できます。 タグの名前を呼び出すと、 NCache そのタグがあり、キャッシュされているすべてのアイテムを返します。 タグを使用すると、キャッシュされたオブジェクトにメタデータをリンクできるため、検索とデータ操作が容易になります。 次の例は、東海岸地域からログインした顧客がどのようにキャッシュに追加されるかを示しています。
1 2 3 4 5 6 7 8 9 10 11 |
// Create an array of tags assigned to customer Tag[] tags = new Tag[2]; tags[0] = new Tag("East Coast Customers"); tags[1] = new Tag("VIP Customers"); // Setting the tag property of the cacheItem cacheItem.Tags = tags; cache.Add(customerKey, cacheItem); // CacheItem added successfully |
同様に、特定のタグ キーワード $Tag$ を持つアイテムをクエリできます。 以下の例の SQL クエリは、タグにリンクされたすべてのアイテムを返します VIPのお客様。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
// Create a query for search string query = "Select CustomerID,ContactName FROM Alachisoft.NCache.Samples.Data.Customer WHERE $Tag$ = ?"; // Use QueryCommand for query execution var queryCommand = new QueryCommand(query); queryCommand.Parameters.Add("$Tag$", "VIP Customers"); // Executing Query ICacheReader reader = cache.SearchService.ExecuteReader(queryCommand); // Read results if result set is not empty if (reader.FieldCount > 0) { while (reader.Read()) { // Get the value of the result set string customerID = reader.GetValue<string> ("CustomerID"); string contactName = reader.GetValue<string>("ContactName"); Console.WriteLine($"Customer '{contactName}' having ID '{customerID}' is a VIP Customer."); } } else { Console.WriteLine($"No VIP Customers found"); } |
タグを使用すると、ユーザーは特定のタイプごとに高いレベルの関連性を得ることができます。 タグが提供するその他の CRUD 操作については、 NCache ドキュメント.
名前付きタグによるキャッシュ データの分類
キャッシュされたオブジェクトには、リストを介して複数の名前付きタグを指定できます。これを使用してアイテムを取得できます。 それぞれの名前付きタグには XNUMX つのパラメーターがあります。タグの名前を表す文字列であるキーと、任意のプリミティブ型である値です。 たとえば、価格帯や在庫状況に基づいて名前付きタグをアイテムに割り当てることができます。 タグ名と値を呼び出すと、 NCache そのタグを持つすべてのアイテムを取得する必要があるときに、指定された名前付きタグを持つすべての格納されたアイテムを返します。
グループやタグと同様に、次のことができます。 アイテムを取得する オブジェクトクエリを使用して名前付きタグでキャッシュから。 特定の名前付きタグがキャッシュ内のアイテムに既に追加されている場合、 NCache これらの名前付きタグを更新する機会をユーザーに提供します。 使用する NCacheのカスタム クラス、つまり CacheItem を使用して、データをキャッシュに追加できます。 のプロパティとして 名前付きタグ クラスに加えて、CacheItem を使用すると、アイテムに関連する追加の仕様を指定できます。 以下の例では、VIP メンバーシップ (VIP メンバーには 10% 割引が適用されます) を持つ顧客が、名前付きタグを CacheItem のプロパティとして割り当てることによって設定されます。
1 2 3 4 5 6 7 8 9 |
// 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); |
アイテムが 名前付きタグでキャッシュに追加、SQL クエリを使用して簡単に取得できます。 次の例では、ユーザーは 12% の VIP メンバーシップ割引を受けているすべての顧客を表示したいと考えています。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
// Create an SQL Query with the specified criteria string query = "SELECT CustomerID,ContactName FROM Alachisoft.NCache.Samples.Data.Customer WHERE VIP_Membership_Discount = 0.12 "; // Use QueryCommand for query execution var queryCommand = new QueryCommand(query); // Executing the Query ICacheReader reader = cache.SearchService.ExecuteReader(queryCommand); // Read results if the result set is not empty if (reader.FieldCount > 0) { while (reader.Read()) { // Get the value of the result set string customerID = reader.GetValue<string>("CustomerID"); string customerName = reader.GetValue<string>("ContactName"); Console.WriteLine($"Customer '{customerName}' with ID '{customerID}' has VIP membership discount."); } } else { Console.WriteLine($"No VIP members found"); } |
まとめ
明らかに NCache グループ、タグ、名前付きタグによる効果的なデータ キャッシュと検索のためのさまざまな強力な機能を提供し、キャッシュされたデータを特定の属性に基づいて分類し、それらを取得します。 NCache API または SQL に似たクエリ。 これらの機能により、データ アクセスの高速化、データベース負荷の軽減、およびアプリケーション パフォーマンスの向上が可能になるため、キャッシングの魅力的なオプションとなります。 によって提供されるこれらの強力な機能を活用してください。 NCache 今すぐ無料トライアルを始めましょう!