キャッシュでのキューの動作と使用法
キューは、先入れ先出し (FIFO) 動作の原則を使用する順序付けされたデータ構造です。 キューを使用して、実行時に処理される情報を維持できます。 たとえば、企業の潜在的な候補者のアプリケーション ID をキューに保存できます。 キューは本質的に FIFO であるため、最初に応募した候補者が最初に面接を受けることになります。
NCache 提供することにより、キューのデータ構造をさらに強化します NCache- などの特有の機能 グループ, タグ, 満了, ロッキング, 依存関係、 もっと。 このシナリオでは、会社は管理職に応募した人々のキューに対してグループを指定できます。
行動
- キューは、任意のプリミティブ型またはカスタムオブジェクトにすることができます。
- のキュー
CacheItem
ネストされたキューはまだサポートされていません。
- キューには名前が付けられています。 したがって、キューに一意のキャッシュキーを提供する必要があります。
- Nullはサポートされている値型ではありません。
- 重複する値がサポートされています。
前提条件
- すべてを使用するために必要な標準的な前提条件について学習するには NCache クライアント側の機能については、次のページを参照してください。 クライアント側 API の前提条件.
- APIの詳細については、以下を参照してください。 Iキャッシュ, IDistributedQueue, IDataTypeManager, 含まれています, CopyTo, 配列へ, ピーク, GetQueue, デキュー, Enqueue, キューの作成, 登録通知, DataTypeDataNotificationCallback, イベントタイプ, データタイプイベントデータフィルター, ロック, アンロック.
- すべてを使用するために必要な標準的な前提条件について学習するには NCache クライアント側の機能については、次のページを参照してください。 クライアント側 API の前提条件.
- APIの詳細については、以下を参照してください。 キャッシュ, 分散キュー, キューの作成, getQueue, 含まれています, ロック, アンロック, イベントタイプ, getEventType, addChangeListener, getDataStructuresManager, onDataStructureChanged, データタイプイベントデータフィルター, DataStructureDataChangeListener, データ構造イベント引数, getCollectionItem.
- すべてを使用するために必要な標準的な前提条件について学習するには NCache クライアント側の機能については、次のページを参照してください。 クライアント側 API の前提条件.
- APIの詳細については、以下を参照してください。 キャッシュ, 分散キュー, getDataStructuresManager, キューの作成, 加えます, getQueue, 含まれています, コピー先, toArray, ピーク, 削除します, イベントタイプ, DataStructureDataChangeListener, addChangeListener, データタイプイベントデータフィルター, データ構造イベント引数, getEventType, getCollectionItem.
- すべてを使用するために必要な標準的な前提条件について学習するには NCache クライアント側の機能については、次のページを参照してください。 クライアント側 API の前提条件.
- APIの詳細については、以下を参照してください。 キャッシュ, データ構造マネージャー, get_data_structs_manager, 分散キュー, 含まれています, 加えます, get_iterator, 削除します, add_change_listener, 作成_キュー, get_queue, get_event_type, データタイプイベントデータフィルター, イベントデータフィルター, DataStructureDataChangeListener, onDataStructureChanged.
キューの作成とデータの追加
次のコードサンプルは、 候補者 タイプは、キャッシュキーに対してキャッシュに作成できます 候補キュー アイテムがキューに追加される方法。
先端
また、ワイルドカード*を使用すると、任意の文字にマッチし、XNUMXつのコマンドで複数のファイルを削除することができます。 検索可能な属性を構成する グループ/タグ/名前付きタグなど 無効化属性 データ構造の作成中の有効期限/削除/依存関係など。
// Precondition: Cache must be connected
// Specify unique cache key for queue
string key = "CandidateQueue";
// Create Queue of Candidate type
IDistributedQueue<Candidate> queue = cache.DataTypeManager.CreateQueue<Candidate>(key);
Candidate[] candidates = FetchCandidates();
foreach(var candidate in candidates)
{
// Add candidates to queue
queue.Enqueue(candidate);
}
// Precondition: Cache must be connected
// Specify unique cache key for queue
String key = "CandidateQueue";
// Create Queue of Candidate type
DistributedQueue<Candidate> queue = cache.getDataStructuresManager().createQueue(key, Candidate.class);
Candidate[] candidates = fetchCandidates();
for (var candidate : candidates) {
// Add candidates to queue
queue.add(candidate);
}
// This is an async method
// Precondition: Cache must be connected
// Specify unique cache key for queue
var key = "CandidateQueue";
// Create Queue
var manager = await this.cache.getDataStructuresManager();
var queue = await manager.createQueue(key, ncache.JsonDataType.Object);
var candidates = this.fetchCandidates();
for (var candidate in candidates) {
// Add candidates to queue
queue.add(candidate);
}
# Precondition: Cache must be connected
# Specify unique cache key for queue
key = "CandidateQueue"
# Create Queue
manager = cache.get_data_structures_manager()
queue = manager.create_queue(key, Candidate)
candidates = fetch_candidates()
for candidate in candidates:
# Add candidates to queue
queue.add(candidate)
# Check if a candidate exists against given ID
candidate = get_candidate_by_id(1002)
if queue.contains(candidate):
# Candidate exists with this ID
print("Candidate found")
次のコードサンプルは、を使用してキャッシュからキューをフェッチします GetQueue
次に、アイテムがキューに存在するかどうかを確認します。 Contains
. を使用して削除せずにキューの先頭からアイテムを取得する方法 Peek
。 ソース キュー全体をターゲット アレイにコピーするには、次のコマンドを使用します。 CopyTo
を使用して、このキューを XNUMX 次元配列にコピーします。 ToArray
.
// Queue with this key already exists in cache
string key = "CandidateQueue";
// Get queue and show items of queue
IDistributedQueue<Candidate> retrievedQueue = cache.DataTypeManager.GetQueue<Candidate>(key);
// Get any candidate ID
Candidate candidate = GetCandidateByID(1002);
// Check whether queue contains the a particular candidate or not
if(retrievedQueue.Contains(candidate))
{
// Candidate exists with this ID
}
// List of candidate's for the queue to be copied into
Candidate[] candidates = new Candidate[retrievedQueue.Count];
// Copy the queue to another array
retrievedQueue.CopyTo(candidates, 0);
// Copy queue to one-dimensional array
candidates = retrievedQueue.ToArray();
// Get an item at beginning from queue without removing
Candidate firstCandidate = retrievedQueue.Peek();
// Precondition: Cache is already connected
// Queue with this key already exists in cache
String key = "CandidateQueue";
// Get queue and show items of queue
DistributedQueue<Candidate> retrievedQueue = cache.getDataStructuresManager().getQueue(key, Candidate.class);
// Get any candidate's ID
Candidate candidate = getCandidateByID(1002);
// Check whether queue contains the a particular candidate or not
if(retrievedQueue.contains(candidate))
{
// Candidate exists with this ID
}
// List of candidates for the queue to be copied into
Candidate[] candidates = new Candidate[retrievedQueue.size()];
// Copy the queue to another array
retrievedQueue.copyTo(candidates, 0);
// Copy queue to one-dimensional array
candidates = retrievedQueue.toArray();
// Get an item at beginning from queue without removing
Candidate firstCandidate = retrievedQueue.peek();
// This is an async method
// Precondition: Cache is already connected
// Queue with this key already exists in cache
var key = "CandidateQueue";
// Get queue and show items of queue
var manager = await this.cache.getDataStructuresManager();
var retrievedQueue = await manager.getQueue(key, ncache.JsonDataType.Object);
// Get any candidate's ID
var candidate = this.getCandidateByID(1002);
// Check whether queue contains the a particular candidate or not
if(retrievedQueue.contains(candidate))
{
// Candidate exists with this ID
}
// List of candidates for the queue to be copied into
var candidates = new Candidate[retrievedQueue.length];
// Copy the queue to another array
retrievedQueue.copyTo(candidates, 0);
// Copy queue to one-dimensional array
var candidates = retrievedQueue.toArray();
// Get an item at beginning from queue without removing
var firstCandidate = retrievedQueue.peek();
# Precondition: Cache is already connected
# Queue with this key already exists in cache
key = "CandidateQueue"
# Get queue and show items of queue
manager = cache.get_data_structures_manager()
retrieved_queue = manager.get_queue(key, Candidate)
if retrieved_queue is not None:
for item in retrieved_queue.get_iterator():
# Perform operations
print(item)
else:
# Queue does not exist
print("Queue not found")
キューからアイテムを削除する
アイテムは、FIFO方式でキューから削除できます。 次のコードサンプルは、キューから最初の候補を削除します。
// Queue with this key already exists in cache
string key = "CandidateQueue";
IDistributedQueue<Candidate> retrievedQueue = cache.DataTypeManager.GetQueue<Candidate>(key);
// Remove first item of queue
retrievedQueue.Dequeue();
// Precondition: Cache must be connected
// Queue with this key already exists in cache
String key = "CandidateQueue";
DistributedQueue<Candidate> retrievedQueue = cache.getDataStructuresManager().getQueue(key, Candidate.class);
// Remove first item of queue
retrievedQueue.remove();
// This is an async method
// Precondition: Cache must be connected
// Queue with this key already exists in cache
var key = "CandidateQueue";
var manager = await this.cache.getDataStructuresManager();
var retrievedQueue = await manager.getQueue(key, ncache.JsonDataType.Object);
// Remove first item of queue
await retrievedQueue.remove();
# Precondition: Cache must be connected
# Queue with this key already exists in cache
key = "CandidateQueue"
manager = cache.get_data_structures_manager()
retrieved_queue = manager.get_queue(key, Candidate)
# Remove first item of queue
retrieved_queue.remove()
Note
操作がフェイルセーフであることを保証するために、で説明されているように、アプリケーション内の潜在的な例外を処理することをお勧めします。 失敗の処理.
キューのイベント通知
キャッシュ イベント、キーベースのイベント、データ構造イベントをキューなどのデータ構造に登録できます。 動作については、を参照してください。 機能に関する動作.
次のコードサンプルは、次のキャッシュイベントを登録します。 ItemAdded
および ItemUpdated
だけでなく、イベントを登録します ItemAdded
および ItemUpdated
キャッシュ内のキューにあります。 キャッシュにキューが作成されると、 ItemAdded
キャッシュレベルのイベントが発生します。 ただし、アイテムがキューに追加されると、 ItemAdded
データ構造イベントが発生し、 ItemUpdated
キャッシュレベルイベントが発生します。
作成されたキューにイベントを登録する
// Unique cache key for queue
string key = "CandidateQueue";
// Create queue of Candidate type
IDistributedQueue<Candidate> queue = cache.DataTypeManager.CreateQueue<Candidate>(key);
// Register ItemAdded, ItemUpdated, ItemRemoved events on queue created
// DataTypeNotificationCallback is callback method specified
queue.RegisterNotification(DataTypeDataNotificationCallback, EventType.ItemAdded |
EventType.ItemUpdated | EventType.ItemRemoved,
DataTypeEventDataFilter.Data);
// Perform operations
// Precondition: Cache is already connected
// Unique cache key for the queue
String key = "CandidateQueue";
// Create queue of candidate type
DistributedQueue<Candidate> queue = cache.getDataStructuresManager().createQueue(key, Candidate.class);
// Create EnumSet of event types
EnumSet<EventType> enumSet = EnumSet.of(com.alachisoft.ncache.runtime.events.EventType.ItemAdded,
EventType.ItemUpdated, EventType.ItemRemoved);
// Register ItemAdded, ItemUpdated, ItemRemoved events on queue created
// dataChangeListener is the specified callback method
DataStructureDataChangeListener dataChangeListener = dataStructureListener.onDataStructureChanged(collectionName, args);
queue.addChangeListener(dataChangeListener, enumSet, DataTypeEventDataFilter.Data);
// Perform operations
// This is an async method
// Precondition: Cache is already connected
// Unique cache key for the queue
var key = "CandidateQueue";
// Create queue of candidate type
var queue = await this.cache
.getDataStructuresManager()
.createQueue(key, ncache.JsonDataType.Object);
// Create EnumSet of event types
var enumSet = enumSet.of(
ncache.EventType.ItemAdded,
ncache.EventType.ItemUpdated,
ncache.EventType.ItemRemoved
);
// Register ItemAdded, ItemUpdated, ItemRemoved events on queue created
// dataChangeListener is the specified callback method
var dataChangeListener = this.dataStructureListener.onDataStructureChanged(
collectionName,
args
);
queue.addChangeListener(
dataChangeListener,
enumSet,
ncache.DataTypeEventDataFilter.Data
);
// Perform operations
def datastructure_callback_function(collection_name, collection_event_args):
# Perform Operations
print("Event Fired for " + str(collection_name))
# Precondition: Cache is already connected
# Unique cache key for queue
key = "CandidateQueue"
# Create queue
queue = cache.get_data_structures_manager().create_queue(key, Candidate)
# Register ItemAdded, ItemUpdated, ItemRemoved events on queue created
events_list = [ncache.EventType.ITEM_ADDED, ncache.EventType.ITEM_UPDATED, ncache.EventType.ITEM_REMOVED]
queue.add_change_listener(datastructure_callback_function, events_list, ncache.DataTypeEventDataFilter.DATA)
イベント通知のコールバックを指定する
private void DataTypeDataNotificationCallback(string collectionName, DataTypeEventArg collectionEventArgs)
{
switch (collectionEventArgs.EventType)
{
case EventType.ItemAdded:
// Item has been added to the collection
break;
case EventType.ItemUpdated:
if (collectionEventArgs.CollectionItem != null)
{
// Item has been updated in the collection
// Perform operations
}
break;
case EventType.ItemRemoved:
// Item has been removed from the collection
break;
}
}
DataStructureDataChangeListener dataStructureListener = new DataStructureDataChangeListener() {
@Override
public void onDataStructureChanged(String collection, DataStructureEventArg dataStructureEventArg) {
switch (dataStructureEventArg.getEventType()) {
case ItemAdded:
// Item has been added to the collection
break;
case ItemUpdated:
if (dataStructureEventArg.getCollectionItem() != null) {
//Item has been updated in the collection
// perform operations
}
break;
case ItemRemoved:
//Item has been removed from the collection
break;
}
}
};
dataStructureListener = new ncache.DataStructureDataChangeListener();
{
function onDataStructureChanged(collection, dataStructureEventArg) {
switch (dataStructureEventArg.getEventType()) {
case ncache.EventType.ItemAdded:
//Item has been added to the collection
break;
case ncache.EventType.ItemUpdated:
if (dataStructureEventArg.getCollectionItem() != null) {
//Item has been updated in the collection
// perform operations
}
break;
case ncache.EventType.ItemRemoved:
//Item has been removed from the collection
break;
}
}
}
def datastructure_callback_function(collection_name: str, collection_event_args: DataStructureEventArg):
if collection_event_args.get_event_type() is ncache.EventType.ITEM_ADDED:
# Item has been added to the collection
print("Item added in " + collection_name)
elif collection_event_args.get_event_type() is ncache.EventType.ITEM_UPDATED:
# Item has been updated in the collection
print("Item updated in " + collection_name)
elif collection_event_args.get_event_type() is ncache.EventType.ITEM_REMOVED:
# Item has been removed from the collection
print("Item removed from " + collection_name)
キューのロック
データの一貫性を確保するために、キューを明示的にロックおよびロック解除できます。 次のコードサンプルは、キューを作成し、を使用して10秒間ロックします。 ロック 次に、を使用してロックを解除します アンロック.
// Queue exists with key "CandidateQueue" cache key
string key = "CandidateQueue";
// Get queue
IDistributedQueue<Candidate> queue = cache.DataTypeManager.GetQueue<Candidate>(key);
// Lock queue for 10 seconds
bool isLocked = queue.Lock(TimeSpan.FromSeconds(10));
if (isLocked)
{
// Queue is successfully locked for 10 seconds
// Unless explicitly unlocked
}
else
{
// Queue is not locked because either:
// Queue is not present in the cache
// Queue is already locked
}
queue.Unlock();
// Preconditions: Cache is already connected
// Queue exists with key "CandidateQueue" cache key
String key = "CandidateQueue";
// Get queue
DistributedQueue<Candidate> queue = cache.getDataStructuresManager().getQueue(key, Candidate.class);
// Lock queue for 10 seconds
boolean isLocked = queue.lock(TimeSpan.FromSeconds(10));
if (isLocked) {
// Queue is successfully locked for 10 seconds
// Unless explicitly unlocked
} else {
// Queue is not locked because either:
// Queue is not present in the cache
// Queue is already locked
}
queue.unlock();
その他のリソース
NCache のキューデータ構造のサンプルアプリケーションを提供します GitHubの.
も参照してください
。ネット: Alachisoft.NCache.Client.DataTypes 名前空間
Java: comの。alachisoft.ncache.client.datastructs 名前空間
Node.js: 分散キュー とに提供されます。
Python: ncache.client.datastructs とに提供されます。