Alachisoft NCache 4.1 - Online Documentation

File Dependency

 
We can set the validity of the cache item based on an external file or files. When this resource changes, the cached object becomes obsolete and is removed from the cache. In File Dependency object, a File/Directory path is provided. Cache object also expires if the path is used to create a new File/Directory. Following steps demonstrate how to use file based dependency.
 
The following steps demonstrate how to use file based dependency:
 
  1. Create a text file with name NCache.txt at path C:\ i.e C:\NCache.txt .
  2. Add the following import statement in your project:
     
    import com.alachisoft.ncache.web.caching.*;
    import com.alachisoft.ncache.runtime.dependencies;
     
  3. Copy and run the following code in Java environment.
  4. When prompted "Enter when done", either Remove, Modify or Rename the file, C:\NCache.txt, then press Enter, the dependant key will be removed from the Cache automatically.
 
    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"))
    {
        System.out.println("cache Key: \"dependantItemOnFile\" exists");
    }
 
    // Wait for file removal, deletion or modification to show File based dependency
    System.out.println("Remove/Rename/Modify file C:\NCache.txt/nPress Enter when done");
    System.in.read();
 
    if (_cache.contains("dependantItemOnFile"))
    {
        System.out.println("ERROR: cache Key: \"dependantItemOnFile\" not removed,/n Please read Help from NCache for further details or contact support@alachisoft.com");
    }
    else
    {
        System.out.println("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.