파일에 대한 캐시 데이터 종속성
파일 종속성은 비관계형 데이터 소스의 경우 캐시 데이터를 무효화하는 메커니즘을 제공합니다. 여기에서 파일/폴더 종속성을 캐시 데이터 항목과 연결할 수 있습니다. 해당 파일/폴더가 제거되거나 수정될 때마다 NCache 캐시에서 종속 항목을 제거합니다. 캐시 정리 스레드는 매 순간 종속 파일/폴더의 변경 사항을 모니터링합니다. CleanInterval
.
캐시 정리 스레드에서 NCache 다음 시나리오에서 종속성을 트리거합니다.
- 종속성 파일이 제거/수정되었습니다.
- 종속성 폴더가 제거/수정되었습니다.
- 존재하지 않는 파일/폴더에 종속성이 생성되지만 정리 간격에 따라 생성됩니다.
마찬가지로 여러 항목이 단일 파일에 종속될 수 있습니다. 파일을 업데이트하거나 삭제하여 파일을 변경하면 캐시가 캐시에서 종속 항목을 제거하게 됩니다. 마찬가지로 항목은 파일 종속성에서 여러 파일에 대한 종속성을 생성할 수 있습니다.
다음과 같은 지연을 제공할 수도 있습니다. startAfter
변경 사항에 대해 종속 파일 모니터링을 시작할 시기를 나타내는 파일 종속성. 이런 식으로, NCache 후 종속 파일 검사를 시작합니다 startAfter
시간이 지났습니다.
파일 전제조건에 대한 캐시 데이터 종속성
중대한
다음 사항을 확인하십시오. NCache 서비스는 종속될 파일이 있는 파일 경로에 액세스할 수 있습니다.
캐시 데이터로 파일 종속성 추가
CacheItem
에서 제공하는 사용자 정의 클래스입니다. NCache 캐시에 데이터를 추가하는 데 사용할 수 있으며 이 클래스의 개체와 관련된 추가 메타데이터를 설정할 수도 있습니다. 이 메타데이터는 종속성, 만료 등과 같은 항목의 속성을 정의합니다.
XNUMXD덴탈의 추가 메소드는 캐시 데이터에 새 항목을 추가하는 반면 끼워 넣다 메서드는 종속성이 있는 새 항목을 추가하고 항목이 이미 캐시에 있는 경우 기존 속성을 덮어씁니다.
다음 예에서 캐시에 추가된 데이터는 지정된 경로에 있는 파일의 내용입니다. CacheItem
파일에 대한 의존성을 가지고 있습니다. 파일이 변경되면 캐시에 있는 종속 데이터가 만료됩니다. 만료된 항목은 캐시에서 제거됩니다. CleanInterval
.
// Precondition: Cache is already connected
// Specify the file path to add dependency on the file
string filepath = "D:\\Products.csv";
// Getting product from file.
Product product = FetchProductFromFile(filepath);
string key = $"Product: {product.ProductID}";
// Create a new cacheItem with product data.
var cacheItem = new CacheItem(product);
// Create the dependency on the file with specified path.
cacheItem.Dependency = new FileDependency(filepath);
// Add the file data in the cache with dependency on the file
cache.Add(key, cacheItem);
// Precondition: Cache is already connected
// Specify the file path to add dependency on the file
String filepath = "D:\\Products.csv";
// Getting product from file.
Product product = fetchProductFromFile(filepath);
String key = "Product: " + product.getProductId();
// Create a new cacheItem with product data.
var cacheItem = new CacheItem(product);
// Create the dependency on the file with specified path.
cacheItem.setDependency(new FileDependency(filepath));
// Add the file data in the cache with dependency on the file
cache.add(key, cacheItem);
// Precondition: Cache is already connected
// Generate a unique key for the fileData
let key = "FileData";
let filePath = "test.txt";
// Get the content of the file as string
let fileData = this.cache.get(key, String.class);
let blob = new Blob;
// Read the contents of the file placed at the path
if (!(fileData == null)) {
var file = new FileReader();
file.readAsText(blob, filePath);
}
// Create a new cache item with the file data
let cacheItem = new ncache.CacheItem(fileData);
// Create File Dependency on the file placed at filePath
cacheItem.setDependency(new ncache.FileDependency(filePath));
// Add the file data in the cache with dependency on the file
this.cache.insert(key, cacheItem);
# Precondition: Cache is already connected
# Generate a unique key for the fileData
key = "FileData"
file_path = "Path/To/File"
# Get the content of the file as string
file_data = cache.get(key, str)
# Check if the file_data is already in the cache
if file_data is None or not file_data:
# Read the contents of the file placed at the path
file = open(file_path, mode='r')
file_data = file.read()
file.close()
# Create a new cache item with the file data
cache_item = ncache.CacheItem(file_data)
# Create File Dependency on the file placed at filePath
cache_item.set_dependency(ncache.FileDependency(file_path))
# Add the file data in the cache with dependency on the file
cache.insert(key, cache_item)
주의 사항
작업이 안전하도록 하려면 에 설명된 대로 응용 프로그램 내에서 잠재적인 예외를 처리하는 것이 좋습니다. 처리 실패.
지연으로 파일 종속성 트리거
특정 시간 이후에 실행되는 항목에 종속성을 추가하려면, NCache 시간 간격을 지정할 수 있습니다.
XNUMXD덴탈의 추가 메서드는 캐시에 새 항목을 추가하지만 끼워 넣다 메서드는 종속성이 있는 새 항목을 추가하고 해당 항목이 이미 캐시에 있는 경우 해당 속성을 덮어씁니다.
다음 예에서 캐시에 추가된 데이터는 파일에 대한 종속성과 함께 지정된 경로에 배치된 파일의 내용입니다. 종속성은 20분 후에 트리거됩니다. 파일이 변경되면 캐시에 있는 종속 데이터가 만료됩니다. 만료된 항목은 캐시에서 제거됩니다. CleanInterval
.
// Specify the file path to add dependency on the file
string filepath = "D:\\Products.csv";
//Getting product from file.
Product product = FetchProductFromFile(filepath);
// Creating a unique key for product.
string key = $"Product: {product.ProductID}";
// Create a new cacheItem with product data.
var cacheItem = new CacheItem(product);
// Create the dependency on the file with specified path.
cacheItem.Dependency = new FileDependency(filepath, DateTime.Now.AddMinutes(20));
// Add the file data in the cache with dependency on the file
cache.Insert(key, cacheItem);
// Specify the file path to add dependency on the file
String filepath = "D:\\Products.csv";
//Getting product from file.
Product product = fetchProductFromFile(filepath);
// Creating a unique key for product.
String key = "Product: " + product.getProductId();
// Create a new cacheItem with product data.
var cacheItem = new CacheItem(product);
// Create the dependency on the file with specified path.
cacheItem.setDependency(new FileDependency(filepath, Date.from(Instant.ofEpochSecond(TimeSpan.FromMinutes(20)._ticks))));
// Add the file data in the cache with dependency on the file
cache.insert(key, cacheItem);
// Generate a unique key for the fileData
let key = "FileData";
let filePath = "test.txt";
// Get the content of the file as string
let fileData = this.cache.get(key, String.class);
let blob = new Blob;
// Read the contents of the file placed at the path
if (!(fileData == null)) {
var file = new FileReader();
file.readAsText(blob, filePath);
}
// Create a new cache item with the key
let cacheItem = new ncache.CacheItem(fileData);
// Create delay for dependency
let now = ncache.Calender.getInstance();
now.add(ncache.Calendar.MINUTE, 20);
let delayTime = now.getTime();
// Create File Dependency on the file placed at filePath
// The dependency starts after 20 minutes
cacheItem.setDependency(new ncache.FileDependency(filePath, delayTime));
// Add the file data in the cache with dependency on the file
this.cache.insert(key, cacheItem);
# Generate a unique key for the fileData
key = "FileData"
# Specify the file path to add dependency on the file
file_path = "Path/To/File"
# Get the content of the file as string
file_data = cache.get(key, str)
# Check if the file_data is already in the cache
if file_data is None or not file_data:
# Read the contents of the file placed at the path
file = open(file_path, mode='r')
file_data = file.read()
file.close()
# Create a new CacheItem with the file_data
cache_item = ncache.CacheItem(file_data)
# Create the dependency on the file on the specified path
# The dependency starts after 20 minutes
cache_item.set_dependency(ncache.FileDependency(file_path, datetime.now() + timedelta(minutes=20)))
cache.insert(key, cache_item)
여러 파일에 파일 종속성 추가
여러 파일이나 폴더에 종속된 항목에 파일 종속성을 추가할 수도 있습니다. 이렇게 하면 단일 항목이 다음을 사용하여 여러 항목에 종속될 수 있습니다. 추가 or 끼워 넣다 방법. 그만큼 Add
메서드는 캐시에 새 항목을 추가하지만 Insert
메서드는 종속성이 있는 새 항목을 추가하고 항목이 이미 캐시에 있으면 해당 속성을 덮어씁니다.
다음 예에서는 지정된 경로에 있는 두 파일에 종속되는 키가 있는 항목을 추가합니다.
// Specify the file paths to add dependency on the file
string orderFilePath = "D:\\Orders.csv";
string orderDetailFilePath = "D:\\OrderDetail.csv";
string[] filePaths = { orderFilePath, orderDetailFilePath };
// Getting order from file.
Order order = FetchOrderFromFile(orderFilePath);
string key = $"Order: {order.OrderID}";
// Create a new cacheItem with order data.
var cacheItem = new CacheItem(order);
// Create the dependency on the files with specified path.
cacheItem.Dependency = new FileDependency(filePaths);
// Add the file data in the cache with dependency on the files.
cache.Add(key, cacheItem);
// Specify the file paths to add dependency on the file
String orderFilePath = "D:\\Orders.csv";
String orderDetailFilePath = "D:\\OrderDetail.csv";
ArrayList<String> paths = new ArrayList<>(){};
paths.add(orderFilePath);
paths.add(orderDetailFilePath );
// Create the dependency on the files with specified paths.
FileDependency fileDependency = new FileDependency(paths);
// Getting customer from file.
Order order = fetchOrderFromFile(orderFilePath);
// Creating a unique key for the customer.
String key = "Order:" + order.getOrderDate();
// Create a new cacheItem with customer data.
CacheItem cacheItem = new CacheItem(order);
// Create the dependency on the files with specified path.
cacheItem.setDependency(fileDependency);
// Add the file data to the cache with dependency on the files.
cache.insert(key, cacheItem);
// Get product from database against productId
let product = this.fetchProductFromDb(this.productId);
// Create a unique cache key for this product
let key = "Product:" + product.ProductID;
// Create a new cache item
let cacheItem = new ncache.CacheItem(product);
// SPecify paths of the master files
// Make sure that the network file paths are accessible by NCache Service
let fileNames = ["\\fileserver1\\ProductList.csv", "\\fileserver1\\OrderList.csv"];
// Adding File Dependency on the specified cache item
cacheItem.setDependency(new ncache.FileDependency(fileNames));
// Insert the cache item with File Dependency
this.cache.insert(key, cacheItem);
// For successful addition of item with dependency
// Update or remove the file
# Get product from database against productId
product = fetch_product_from_db("1001")
# Create a unique cache key for this product
key = "Product:" + product.get_product_id()
# Create a new cache item
cache_item = ncache.CacheItem(product)
# Specify paths of the master files
# Make sure that the network file paths are accessible by NCache Service
file_names = ["\\fileserver1\\ProductList.csv", "\\fileserver1\\OrderList.csv"]
# Adding File Dependency on the specified cache item
cache_item.set_dependency(ncache.FileDependency(file_names))
# Insert the cache item with File Dependency
cache.insert(key, cache_item)
# For successful addition of item with dependency
# Update or remove the file
기존 캐시 항목에 파일 종속성 추가
NCache 또한 캐시에 다시 삽입하지 않고도 캐시에 이미 존재하는 항목에 키 종속성을 쉽게 추가할 수 있습니다.
중대한
이 접근 방식은 항목이 이미 캐시에 있으므로 상대적으로 덜 가볍고 비용 효율적입니다.
UpdateAttributes API 사용
이는 다음을 통해 수행됩니다. CacheItemAttribute
의 속성을 가진 클래스 Dependency
반대하다 CacheItem
. 그런 다음 속성은 다음을 사용하여 항목의 기존 키에 대해 설정됩니다. UpdateAttributes
방법 ICache
인터페이스.
다음 예에서는 종속성 없이 캐시에 항목을 추가한 다음 UpdateAttributes
방법. 캐시에 항목을 다시 추가할 필요가 없습니다.
// Precondition: Cache is already connected
// Specify the file path to add dependency on the file
string filepath = "D:\\Products.csv";
string key = $"Product: 1";
// Create a new cacheItemAttribute.
var attribute = new CacheItemAttributes();
// Create the dependency on the file with specified path.
attribute.Dependency = new FileDependency(filepath);
// update the cacheItem.
cache.UpdateAttributes(key, attribute);
// Specify the file path to add dependency on the file
String filepath = "D:\\Products.csv";
String key = "Product: 1";
// Create a new cacheItemAttribute.
CacheItemAttributes attribute = new CacheItemAttributes();
// Create the dependency on the file with specified path.
attribute.setDependency(new FileDependency(filepath));
// Update the cacheItem attributes.
cache.updateAttributes(key, attribute);
System.out.println("Cache item with key " + key + " updated with file dependency.");
// Generate a unique cache key for file data
let key = "FileData";
// Get the content of the file as string
// Make sure that the item already exists in the cache
let fileData = this.cache.get(key, String.class);
// Specify the filepath
let filePath = "\\FileHost\\ProductList.csp";
// Create a File Dependency where key is dependent on the file placed at filePath
var dependency = new ncache.FileDependency(filePath);
// Create a cache item attribute for dependency
var attribute = new ncache.CacheItemAttributes();
attribute.setDependency(dependency);
// Set the attributes of dependency against the existing cache key
this.cache.updateAttributes(key, attribute);
// Monitor/Verify dependency through PerfMon Counters or Cache API
# Generate a unique cache key for file data
key = "FileData"
# Specify the filepath
file_path = "\\FileHost\\ProductList.csp"
# Create a File Dependency where key is dependent on the file placed at file_path
dependency = ncache.FileDependency(file_path)
# Create a cache item attribute for dependency
attribute = ncache.CacheItemAttributes()
attribute.set_dependency(dependency)
# Set the attributes of dependency against the existing cache key
cache.update_attributes(key, attribute)
# Monitor / Verify dependency through PerfMon Counters or Cache API
추가 자료
NCache 파일 종속성을 위한 샘플 애플리케이션 제공 GitHub의.
도 참조
.그물: Alachisoft.NCache.런타임.종속성 네임스페이스. 네임스페이스.
자바 : COM.alachisoft.ncache.runtime.종속성 네임 스페이스.
Node.js : 파일 종속성 클래스입니다.
파이썬 : ncache.runtime.종속성 클래스입니다.