NET Caching Library Help
BuildCacheKeyWithReflection(String,Dictionary<String,Object>) Method
Example 




KellermanSoftware.NetCachingLibrary Namespace > SmartCache Class > BuildCacheKeyWithReflection Method : BuildCacheKeyWithReflection(String,Dictionary<String,Object>) Method
Name of the base.
The parms.
Builds the cache key with reflection so it is unique for the caller
Syntax
'Declaration
 
Public Overloads Function BuildCacheKeyWithReflection( _
   ByVal baseName As System.String, _
   ByVal parms As System.Collections.Generic.Dictionary(Of String,Object) _
) As System.String
'Usage
 
Dim instance As SmartCache
Dim baseName As System.String
Dim parms As System.Collections.Generic.Dictionary(Of String,Object)
Dim value As System.String
 
value = instance.BuildCacheKeyWithReflection(baseName, parms)
public System.string BuildCacheKeyWithReflection( 
   System.string baseName,
   System.Collections.Generic.Dictionary<string,object> parms
)
public: System.string* BuildCacheKeyWithReflection( 
   System.string* baseName,
   System.Collections.Generic.Dictionary<string*,Object*>* parms
) 
public:
System.String^ BuildCacheKeyWithReflection( 
   System.String^ baseName,
   System.Collections.Generic.Dictionary<String^,Object^>^ parms
) 

Parameters

baseName
Name of the base.
parms
The parms.

Return Value

System.String.
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);
 
//Build parameters for the report
int month = DateTime.Now.Month;
int year = DateTime.Now.Year;
 
//Get the cache key based on the report parameters
//The get cache key method uses reflection to ensure uniqueness
Dictionary<string, object> parms = new Dictionary<string, object>()
{
    {"Month", month },
    {"Year", year }
};
string cacheKey = cache.BuildCacheKeyWithReflection("MonthlySalesReport", parms);
 
//Attempt to load the report from the cache
List<ReportDetail> report = cache.Get<List<ReportDetail>>(cacheKey);
 
//Not found in the cache
if (report == null)
{
    //Load the report from the database
    report = ReportLogic.MonthlySalesReport(month, year);
 
    //Save to the cache using the default expiration of 20 minutes for the memory cache and 24 hours for the file cache
    cache.Set<List<ReportDetail>>(cacheKey, report);
}
'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)
 
'Build parameters for the report
Dim month As Integer = Date.Now.Month
Dim year As Integer = Date.Now.Year
 
'Get the cache key based on the report parameters
'The get cache key method uses reflection to ensure uniqueness
Dim parms As New Dictionary(Of String, Object)() From {
    {"Month", month },
    {"Year", year }
}
Dim cacheKey As String = cache.BuildCacheKeyWithReflection("MonthlySalesReport", parms)
 
'Attempt to load the report from the cache
Dim report As List(Of ReportDetail) = cache.Get(Of List(Of ReportDetail))(cacheKey)
 
'Not found in the cache
If report Is Nothing Then
    'Load the report from the database
    report = ReportLogic.MonthlySalesReport(month, year)
 
    'Save to the cache using the default expiration of 20 minutes for the memory cache and 24 hours for the file cache
    cache.Set(Of List(Of ReportDetail))(cacheKey, report)
End If
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
Overload List