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




KellermanSoftware.NetCachingLibrary Namespace > SmartCache Class > Set Method : Set<T>(String,T,CacheItemPolicy,String) Method
A unique identifier for the cache entry.
The object to insert.
An object that contains eviction details for the cache entry. This object provides more options for eviction than a simple absolute expiration.
Optional. A named region in the cache to which the cache entry can be added. The default value for the optional parameter is Default
Inserts or updates a cache entry in the cache. The passed expiration policy will be used for both the primary and secondary cache providers.
Syntax
'Declaration
 
Public Overloads Sub Set(Of T)( _
   ByVal key As System.String, _
   ByVal value As T, _
   ByVal policy As System.Runtime.Caching.CacheItemPolicy, _
   Optional ByVal regionName As System.String _
) 
'Usage
 
Dim instance As SmartCache
Dim key As System.String
Dim value As T
Dim policy As System.Runtime.Caching.CacheItemPolicy
Dim regionName As System.String
 
instance.Set(Of T)(key, value, policy, regionName)
public void Set<T>( 
   System.string key,
   T value,
   System.Runtime.Caching.CacheItemPolicy policy,
   System.string regionName
)
public: void Set<T>( 
   System.string* key,
   T* value,
   System.Runtime.Caching.CacheItemPolicy* policy,
   System.string* regionName
) 
public:
void Setgeneric<typename T>
( 
   System.String^ key,
   T^ value,
   System.Runtime.Caching.CacheItemPolicy^ policy,
   System.String^ regionName
) 

Parameters

key
A unique identifier for the cache entry.
value
The object to insert.
policy
An object that contains eviction details for the cache entry. This object provides more options for eviction than a simple absolute expiration.
regionName
Optional. A named region in the cache to which the cache entry can be added. The default value for the optional parameter is Default

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
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);
            
	CacheItemPolicy policy = new CacheItemPolicy()
	{
		AbsoluteExpiration = DateTimeOffset.Now.AddMinutes(20),
		SlidingExpiration = SmartCache.NoSlidingExpiration
	};
            
	//Save to the cache in the Default region using the default expiration for the primary and secondary caches
	cache.Set<List<ReportDetail>>(cacheKey, report, policy, "Default");
}
'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 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)
            
	Dim policy As New CacheItemPolicy() With {
		.AbsoluteExpiration = DateTimeOffset.Now.AddMinutes(20),
		.SlidingExpiration = SmartCache.NoSlidingExpiration
	}
            
	'Save to the cache in the Default region using the default expiration for the primary and secondary caches
	cache.Set(Of List(Of ReportDetail))(cacheKey, report, policy, "Default")
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