NCache 매우 빠르고 확장 가능한 분산 캐시입니다. . NET, 자바, Node.js를예산 및 Python 분야의 다양한 어플리케이션에서 사용됩니다. NCache 고트랜잭션 .NET 서버 애플리케이션에서 애플리케이션 데이터 캐싱을 위해 사용됩니다. ASP.NET / ASP.NET 코어 세션 저장소 및 게시/구독 메시징.
NCache 이벤트 분산 데이터 구조 여러 사용자가 공유할 수 있는 구조입니다. 클라이언트 애플리케이션의 힙에 상주하는 로컬 구조와 달리, 이러한 구조는 분산 캐시 클러스터에 호스팅되어 로컬 메모리 사용량과 가비지 컬렉션 오버헤드를 줄입니다. 이러한 구조에는 다음이 포함됩니다.
| 제품 특장점 | 표준 진행 중 수집 | NCache 분산 구조 |
|---|---|---|
| 범위 | 프로세스 제약적(로컬 힙) | 클러스터 전체(공유 네트워크 리소스) |
| 주기 | 애플리케이션 재시작/재활용 시 데이터가 손실됨 | 애플리케이션 수명 주기와 무관함 |
| 이용 가능 여부 (Availability) | 단일 실패 지점(프로세스) | 고가용성(파티션/복제본 토폴로지를 통해) |
| 일관성 | 단일 프로세스 내에서만 스레드 안전성 보장 | 여러 서버에서 일관성 보장 |
다음 섹션에서는 .NET 및 Java 애플리케이션에 대한 세부 정보와 코드 예를 제공합니다.
A 분산 큐 구현 IDistributedQueue<T> 이 인터페이스를 사용하면 분산 환경에서 실행되는 여러 .NET 및 Java 애플리케이션에서 공유할 수 있는 큐를 생성할 수 있습니다. 또한, 이 큐는 데이터를 입력한 순서를 유지합니다. 큐에서 데이터를 검색하는 모든 애플리케이션은 이 순서가 항상 올바르다는 것을 확신할 수 있습니다.
string cacheName = "demoCache";
string QueueName = "DistributedQueue:Customers";
// Initialize an instance of the cache to begin performing operations
ICache cache = CacheManager.GetCache(cacheName);
// Create a thread-safe distributed queue visible to all cluster nodes
IDistributedQueue<Customer> distQueue =
cache.DataTypeManager.CreateQueue<Customer>(QueueName);
distQueue.Enqueue(new Customer {
ContactName = "David Johnes",
CompanyName = "Lonesome Pine Restaurant"
});
String cacheName = "demoCache";
String queueName = "DistributedQueue:Customers";
// Initialize an instance of the cache to begin performing operations
Cache cache = CacheManager.getCache(cacheName);
// Create a thread-safe distributed queue visible to all cluster nodes
DistributedQueue<Customer> distQueue = cache.getDataStructuresManager().createQueue(queueName, Customer.class);
distQueue.add(new Customer("David Johnes", "Lonesome Pine Restaurant"));
distQueue.add(new Customer("Carlos Gonzalez", "LILA-Supermercado"));
위에서 볼 수 있듯이 Distributed Queue를 사용하면 시퀀스에 항목을 추가(Enqueue)하여 나중에 동일한 시퀀스(FIFO)에서 항목을 제거(Dequeue)할 수 있습니다. 분산 대기열 인터페이스는 다음과 같습니다.
public interface IDistributedQueue<T> : IEnumerable<T>, IEnumerable, ICollection, IDistributedDataTypes, ILockable, INotifiable
{
void Clear();
bool Contains(T item);
void CopyTo(T[] array, int arrayIndex);
T Dequeue();
IList<T> DequeueBulk(int maxitems = 10);
void Enqueue(T item);
void EnqueueBulk(IList<T> items);
T Peek();
T[] ToArray();
}
public interface DistributedQueue<T> extends Queue<T>, DistributedDataStructure, Notifiable {
void clear();
void copyTo(T[] var1, int var2);
T peek();
Object[] toArray();
}
A 분산 해시 세트 .NET HashSet 클래스와 동일하게 동작하지만, 여러 애플리케이션과 사용자가 공유하는 방식으로 동작합니다. 다음과 같은 모든 Set 연산을 제공합니다.
여러 애플리케이션이나 사용자가 공유하는 HashSet이므로 강력한 데이터 구조로 활용할 수 있습니다. 다음은 사용 방법의 예입니다.
string cacheName = "demoCache";
string hashSetName = "DistributedHashSet:UniqueValueHashSet";
// Initialize an instance of the cache to begin performing operations:
ICache cache = CacheManager.GetCache(cacheName);
// Creating a distributed HashSet with absolute expiration
IDistributedHashSet<string> hashSet;
hashSet = cache.DataTypeManager.CreateHashSet<string>(hashSetName);
// Create data for HashSet
var daysOfWeek = new string[] { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" };
// Add multiple entries to the HashSet
hashSet.AddRange(daysOfWeek);
// Since this entry already exists, no change to the HashSet is made by this call
hashSet.Add("Monday");
String cacheName = "demoCache";
String hashSetName = "DistributedHashSet:UniqueValueHashSet";
// Initialize an instance of the cache to begin performing operations:
Cache cache = CacheManager.getCache(cacheName);
// Creating a distributed HashSet with absolute expiration
DistributedHashSet<String> hashSet;
hashSet = cache.getDataStructuresManager().createHashSet(hashSetName, String.class);
// Add entries to the hashset
hashSet.add("Monday");
hashSet.add("Tuesday");
hashSet.add("Wednesday");
hashSet.add("Thursday");
hashSet.add("Friday");
hashSet.add("Saturday");
hashSet.add("Sunday");
// Since this entry already exists, no change to the hashset is made by this call
hashSet.add("Monday");
보시다시피, "월요일"을 두 번째로 추가해도 HashSet에 추가되지 않습니다.
A 분산 사전 (IDistributedDictionary)는 일반 .NET IDictionary 인터페이스를 대체하여 여러 애플리케이션과 사용자가 공유할 수 있도록 설계되었습니다. 추가, 업데이트, 삭제, 포함 등 모든 사전 작업을 지원합니다.
일반 사전 기능 외에도 Distributed Dictionary는 다음을 기반으로 항목을 만료하는 기능을 제공합니다. NCache 다음과 같은 만료 옵션 절대 만료 슬라이딩 만료.
ICache cache = CacheManager.GetCache("demoCache");
string dictName = "DistributedDictionary:Customers";
IDistributedDictionary<string, Customer> dict = cache.DataTypeManager.GetDictionary<string, Customer>(dictName);
if (dict == null)
{
DataTypeAttributes attributes = new DataTypeAttributes {
Expiration = new Expiration(ExpirationType.Absolute, new TimeSpan(0, 1, 0))
};
// Creating a distributed Dictionary with an absolute expiration of 1 minute
dict = cache.DataTypeManager.CreateDictionary<string, Customer>(dictName, attributes);
}
Customer cust = new Customer
{
CustomerID = "customer1",
ContactName = "David Johnes",
CompanyName = "Lonesome Pine Restaurant",
ContactNo = "(1) 408-354-9768",
Address = "Silicon Valley, Santa Clara, California",
};
dict.Add("customer1", cust);
Customer cachedCust;
bool fetched = dict.TryGetValue("customer1", out cachedCust);
String cacheName = "demoCache";
String dictName = "DistributedDictionary:Customers";
// Initialize an instance of the cache to begin performing operations
Cache cache = CacheManager.getCache(cacheName);
DistributedMap<String, Customer> dict = cache.getDataStructuresManager().createMap(dictName, Customer.class);
Customer customer = new Customer("David Johnes", "Lonesome Pine Restaurant", "Customer1", "(1) 408-354-9768");
dict.put("customer1", customer);
Customer cachedCustomer = dict.get("customer1");
분산 목록 완전히 구현합니다 System.Collections.Generic.IList<T> 분산형 리스트는 표준 리스트를 대체할 수 있는 인터페이스를 제공합니다. 주요 차이점은 분산형 리스트가 여러 애플리케이션 및 프로세스에서 공유될 수 있다는 점입니다. 분산형 리스트는 정렬되지 않은 리스트이며, 리스트의 끝이나 중간에 항목을 추가할 수 있습니다. 추가하다() 방법이나 이를 통해 어느 위치에서나 끼워 넣다() 인덱스를 제공하는 방법입니다.
다음은 분산 목록을 사용하는 방법의 예입니다.
ICache cache = CacheManager.GetCache("demoCache");
string listName = "DistributedList:Customers";
IDistributedList<Customer> distList = cache.DataTypeManager.GetList<Customer>(listName);
if (distList == null)
{
DataTypeAttributes attributes = new DataTypeAttributes {
Expiration = new Expiration(ExpirationType.Absolute, new TimeSpan(0, 1, 0))
};
// Creating Distributed List with absolute expiration of 1 minute
distList = cache.DataTypeManager.CreateList<Customer>(listName, attributes);
}
Customer cust = new Customer
{
CustomerID = "customer1",
ContactName = "David Johnes",
CompanyName = "Lonesome Pine Restaurant",
ContactNo = "(1) 408-354-9768",
Address = "Silicon Valley, Santa Clara, California",
};
distList.Add(cust);
Customer cachedCustomer = distList[0];
String cacheName = "demoCache";
String listName = "DistributedList:Customers";
// Initialize an instance of the cache to begin performing operations:
Cache cache = CacheManager.getCache(cacheName);
DistributedList distributedList = cache.getDataStructuresManager().createList(listName, Customer.class);
Customer customer = new Customer("David Johnes", "Lonesome Pine Restaurant", "Customer1", "(1) 408-354-9768");
distributedList.add(customer);
Customer cachedCustomer = (Customer) distributedList.get(0);
목록에서 항목을 제거하고, 반복하고, 기타 여러 작업을 수행할 수도 있습니다.
분산 카운터 강력한 데이터 구조입니다 NCache 이를 통해 여러 애플리케이션이 공유하는 분산 환경에서 고유한 카운터를 유지할 수 있습니다. 이는 많은 용도로 사용되며 이를 중심으로 애플리케이션을 빠르게 개발할 수 있습니다. 아래는 예입니다:
ICache cache = CacheManager.GetCache("demoCache");
string counterName = "DistributedCounter:Customers";
ICounter counter = cache.DataTypeManager.GetCounter(counterName);
if (counter == null)
{
DataTypeAttributes attributes = new DataTypeAttributes {
Expiration = new Expiration(ExpirationType.Absolute, new TimeSpan(0, 1, 0))
};
// Creating Distributed Counter with absolute expiration to modify cache properties of the Counter, provide an instance of DataTypeAttributes in the second parameter
counter = cache.DataTypeManager.CreateCounter(counterName, attributes);
}
counter.SetValue(1000);
long newValue = counter.IncrementBy(5);
newValue = counter.Value;
newValue = counter.DecrementBy(2);
String cacheName = "demoCache";
String counterName = "DistributedCounter:Customers";
// Initialize an instance of the cache to begin performing operations
Cache cache = CacheManager.getCache(cacheName);
Counter counter = cache.getDataStructuresManager().createCounter(counterName);
counter.setValue(1000);
long newValue = counter.incrementBy(5);
newValue = counter.getValue();
newValue = counter.decrementBy(2);
IDictionary<K,V> IList<T>표준적인 방법(예: )을 사용할 수 있습니다. 추가, 제거, 포함) 그리고 객체 초기화를 사용하도록 간단히 교체하면 됩니다. NCache 핸들.