NET Caching Library Help
Add<T>(CacheItem,CacheItemPolicy) Method
Example 




KellermanSoftware.NetCachingLibrary Namespace > SmartCache Class > Add Method : Add<T>(CacheItem,CacheItemPolicy) Method
The object to add.
An object that contains eviction details for the cache entry. This object provides more options for eviction than a simple absolute expiration.
Tries to insert a cache entry into the cache as a System.Runtime.Caching.CacheItem instance, and adds details about how the entry should be evicted. The expiration policy will be the same for both the primary and secondary cache providers.
Syntax
'Declaration
 
Public Overloads Function Add(Of T)( _
   ByVal item As System.Runtime.Caching.CacheItem, _
   ByVal policy As System.Runtime.Caching.CacheItemPolicy _
) As System.Boolean
'Usage
 
Dim instance As SmartCache
Dim item As System.Runtime.Caching.CacheItem
Dim policy As System.Runtime.Caching.CacheItemPolicy
Dim value As System.Boolean
 
value = instance.Add(Of T)(item, policy)
public System.bool Add<T>( 
   System.Runtime.Caching.CacheItem item,
   System.Runtime.Caching.CacheItemPolicy policy
)
public: System.bool Add<T>( 
   System.Runtime.Caching.CacheItem* item,
   System.Runtime.Caching.CacheItemPolicy* policy
) 
public:
System.bool Addgeneric<typename T>
( 
   System.Runtime.Caching.CacheItem^ item,
   System.Runtime.Caching.CacheItemPolicy^ policy
) 

Parameters

item
The object to add.
policy
An object that contains eviction details for the cache entry. This object provides more options for eviction than a simple absolute expiration.

Type Parameters

T

Return Value

true if insertion succeeded, or false if there is an already an entry in the cache that has the same key as item.
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);
            
	CacheItem item = new CacheItem(cacheKey, report, "Default");
            
	//Expire in 20 minutes
	CacheItemPolicy policy = new CacheItemPolicy()
	{
		AbsoluteExpiration = DateTimeOffset.Now.AddMinutes(20),
		SlidingExpiration = SmartCache.NoSlidingExpiration
	};
            
	//Save to the cache using the same expiration policy for the memory cache and file cache
	bool result = cache.Add<string>(item, policy);
            
	//This will be true since it was added to the cache
	Console.WriteLine(result);
}
'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 item As New CacheItem(cacheKey, report, "Default")
            
	'Expire in 20 minutes
	Dim policy As New CacheItemPolicy() With {
		.AbsoluteExpiration = DateTimeOffset.Now.AddMinutes(20),
		.SlidingExpiration = SmartCache.NoSlidingExpiration
	}
            
	'Save to the cache using the same expiration policy for the memory cache and file cache
	Dim result As Boolean = cache.Add(Of String)(item, policy)
            
	'This will be true since it was added to the cache
	Console.WriteLine(result)
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