缓存数据依赖于文件
文件依赖关系提供了一种在非关系数据源的情况下使缓存数据无效的机制。您可以在此处将文件/文件夹依赖项与缓存数据项关联起来。每当该文件/文件夹被删除或修改时, NCache 将从缓存中删除其依赖项。 缓存清理线程每次都会监视依赖文件/文件夹的任何更改 CleanInterval
.
在缓存清理线程上, NCache 在以下场景中会触发依赖:
- 依赖文件被删除/修改。
- 依赖文件夹被删除/修改。
- 依赖项是在不存在的文件/文件夹中创建的,但在清理间隔期间创建的。
同样,多个项目可以依赖于单个文件。 文件中的任何更改(通过更新文件或删除)都会导致缓存从缓存中删除依赖项。 同样,一个项目可以在文件依赖关系中创建对多个文件的依赖关系。
您还可以提供一个名为的延迟 startAfter
在文件依赖关系中,指示何时开始监视依赖文件的任何更改。 这样, NCache 之后将开始检查依赖文件 startAfter
时间过去了。
缓存数据对文件先决条件的依赖性
重要
确保 NCache 服务可以访问要依赖的文件所在的文件路径。
添加带有缓存数据的文件依赖关系
CacheItem
是由提供的自定义类 NCache 它可用于将数据添加到缓存,还允许您设置与此类对象关联的附加元数据。 此元数据定义项目的属性,例如依赖项、过期时间等。
地址 方法在缓存数据中添加一个新项目,而 插页 方法添加一个具有依赖性的新项目,如果该项目已存在于缓存中,它将覆盖预先存在的属性。
在下面的例子中,添加到缓存中的数据是放置在指定路径的文件的内容,作为一个 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 允许您为此指定时间间隔。
地址 方法在缓存中添加一个新项目,而 插页 方法添加一个具有依赖性的新项目,如果该项目已存在于缓存中,它将覆盖其属性。
在下面的示例中,添加到缓存中的数据是放置在指定路径下的文件的内容,并且对文件有依赖关系。 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上.
参见
.NET: Alachisoft.NCache.运行时.依赖项 命名空间。命名空间。
Java的: COM。alachisoft.ncache.runtime.dependency 命名空间。
节点.js: 文件依赖 类。
Python: ncache.runtime.dependency 类。