Alachisoft NCache 4.1 - Online Documentation

File Dependency

 
We can set the validity of the cache item based on an external file(s). When this resource changes, the cached object becomes obsolete and is removed from the cache. In FileDependency object, a File/Directory path is provided. Cache object also expires if the path is used to create a new File/Directory.
 
The following steps demonstrate how to use file based dependency.
 
Add the following namespaces in your project:
 
using Alachisoft.NCache.Web.Caching;
using Alachisoft.NCache.Runtime;
using Alachisoft.NCache.Runtime.Dependencies;
 
The following code explains the implementation.
 
    Cache _cache = NCache.InitializeCache("mycache");
    _cache.Clear();      
 
    //Adding cache item "dependantItemOnFile" dependant on file C:/NCache.txt
    _cache.Add("dependantItemOnFile", "dependant", new FileDependency("C://NCache.txt"), Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, CacheItemPriority.Normal);
 
    if (_cache.Contains("dependantItemOnFile"))
    {
        Console.WriteLine("Cache Key: dependantItemOnFile exists");
    }
 
    //Wait for file removal, deletion or modification to show File based dependency
    Console.WriteLine("Remove/Rename/Modify file C://NCache.txt Press Enter when done");
    Console.ReadKey();
 
    if (_cache.Contains("dependantItemOnFile"))
    {
        Console.WriteLine("ERROR: Cache Key: dependantItemOnFile not removed,Please read Help from NCache for further details or contact support@alachisoft.com");
    }
 
    else
    {
        Console.WriteLine("Cache Key: dependantItemOnFile does not exist, item removed since the file C://NCache.txt was either Removed, Renamed or Modified");
    }
 
 
 
See Also
 
Copyright © 2005-2012 Alachisoft. All rights reserved.