NET Caching Library Help
Set<T>(SmartParms,T) Method
Example 




KellermanSoftware.NetCachingLibrary Namespace > SmartCache Class > Set Method : Set<T>(SmartParms,T) Method
The parameters for the cache item
The value of the cache item
Insert or update a cache value.
Syntax
'Declaration
 
Public Overloads Sub Set(Of T)( _
   ByVal parms As SmartParms, _
   ByVal value As T _
) 
'Usage
 
Dim instance As SmartCache
Dim parms As SmartParms
Dim value As T
 
instance.Set(Of T)(parms, value)
public void Set<T>( 
   SmartParms parms,
   T value
)
public: void Set<T>( 
   SmartParms* parms,
   T* value
) 
public:
void Setgeneric<typename T>
( 
   SmartParms^ parms,
   T^ value
) 

Parameters

parms
The parameters for the cache item
value
The value of the cache item

Type Parameters

T
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
string cacheKey = cache.BuildCacheKey("MonthlySalesReport", month, year);
            
//Attempt to load the report from the cache
var 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);
            
	SmartParms parms = new SmartParms();
	parms.Key = cacheKey;
	parms.Region = "Default";
	parms.PrimaryExpiration = new CacheItemPolicy() { AbsoluteExpiration = DateTimeOffset.Now.AddMinutes(20) };
	parms.SecondaryExpiration = new CacheItemPolicy() { AbsoluteExpiration = DateTimeOffset.Now.AddDays(1) };
            
	//Save to the cache in the Default region using the passed in expiration
	cache.Set<List<ReportDetail>>(parms, 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
Dim cacheKey As String = cache.BuildCacheKey("MonthlySalesReport", month, year)
            
'Attempt to load the report from the cache
Dim report = 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)
            
	Dim parms As New SmartParms()
	parms.Key = cacheKey
	parms.Region = "Default"
	parms.PrimaryExpiration = New CacheItemPolicy() With {.AbsoluteExpiration = DateTimeOffset.Now.AddMinutes(20)}
	parms.SecondaryExpiration = New CacheItemPolicy() With {.AbsoluteExpiration = DateTimeOffset.Now.AddDays(1)}
            
	'Save to the cache in the Default region using the passed in expiration
	cache.Set(Of List(Of ReportDetail))(parms, 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