NET Caching Library Help
ClearByRegex Method
Example 




KellermanSoftware.NetCachingLibrary Namespace > SmartCache Class : ClearByRegex Method
Clear an item if the cache key matches a regular expression
Syntax
'Declaration
 
Public Sub ClearByRegex( _
   ByVal regex As System.Text.RegularExpressions.Regex, _
   Optional ByVal region As System.String _
) 
'Usage
 
Dim instance As SmartCache
Dim regex As System.Text.RegularExpressions.Regex
Dim region As System.String
 
instance.ClearByRegex(regex, region)
public void ClearByRegex( 
   System.Text.RegularExpressions.Regex regex,
   System.string region
)
public: void ClearByRegex( 
   System.Text.RegularExpressions.Regex* regex,
   System.string* region
) 
public:
void ClearByRegex( 
   System.Text.RegularExpressions.Regex^ regex,
   System.String^ region
) 

Parameters

regex
region
Example
//Create a Primary Memory Cache with a backing File Cache
string directory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "KellermanSoftware", "NetCachingLibrary", "Tests");
SmartConfig config = new SmartConfig(new MemoryCacheProvider(), new FileCacheProvider(directory));
            
//Specify UserName and LicenseKey for Licensed Mode
//config.UserName = "my user name";
//config.LicenseKey = "my license key";
            
//Instantiate the cache
SmartCache cache = new SmartCache(config);
            
cache.Set<string>("LovelySpam", "WonderfulSpam");
cache.Set<string>("ChoppinBrocolli", "SheWasChoppinBrocolli");
cache.Set<string>("ChoppinBrocolliAtHome", "SheWasChoppinBrocolli");
            
//Clear any keys beginning with Choppin
cache.ClearByRegex(new Regex("^Choppin.*$"));
            
//These will both be false since both cache items were deleted
Console.WriteLine(cache.Contains("ChoppinBrocolli"));
Console.WriteLine(cache.Contains("ChoppinBrocolliAtHome"));
            
//This will be true since the key did not match the regex and the cache item still exists
Console.WriteLine(cache.Contains("LovelySpam"));
'Create a Primary Memory Cache with a backing File Cache
Dim directory As String = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "KellermanSoftware", "NetCachingLibrary", "Tests")
Dim config As New SmartConfig(New MemoryCacheProvider(), New FileCacheProvider(directory))
            
'Specify UserName and LicenseKey for Licensed Mode
'config.UserName = "my user name";
'config.LicenseKey = "my license key";
            
'Instantiate the cache
Dim cache As New SmartCache(config)
            
cache.Set(Of String)("LovelySpam", "WonderfulSpam")
cache.Set(Of String)("ChoppinBrocolli", "SheWasChoppinBrocolli")
cache.Set(Of String)("ChoppinBrocolliAtHome", "SheWasChoppinBrocolli")
            
'Clear any keys beginning with Choppin
cache.ClearByRegex(New Regex("^Choppin.*$"))
            
'These will both be false since both cache items were deleted
Console.WriteLine(cache.Contains("ChoppinBrocolli"))
Console.WriteLine(cache.Contains("ChoppinBrocolliAtHome"))
            
'This will be true since the key did not match the regex and the cache item still exists
Console.WriteLine(cache.Contains("LovelySpam"))
Requirements

Target Platforms: Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

See Also

Reference

SmartCache Class
SmartCache Members