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




KellermanSoftware.NetCachingLibrary Namespace > SmartCache Class > AddOrGetExisting Method : AddOrGetExisting<T>(CacheItem,CacheItemPolicy) Method
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.
Inserts the specified System.Runtime.Caching.CacheItem object into the cache, specifying information about how the entry will be evicted. The expiration policy will be the same for both the primary and secondary cache providers.
Syntax
'Declaration
 
Public Overloads Function AddOrGetExisting(Of T)( _
   ByVal item As System.Runtime.Caching.CacheItem, _
   ByVal policy As System.Runtime.Caching.CacheItemPolicy _
) As System.Runtime.Caching.CacheItem
'Usage
 
Dim instance As SmartCache
Dim item As System.Runtime.Caching.CacheItem
Dim policy As System.Runtime.Caching.CacheItemPolicy
Dim value As System.Runtime.Caching.CacheItem
 
value = instance.AddOrGetExisting(Of T)(item, policy)
public System.Runtime.Caching.CacheItem AddOrGetExisting<T>( 
   System.Runtime.Caching.CacheItem item,
   System.Runtime.Caching.CacheItemPolicy policy
)
public: System.Runtime.Caching.CacheItem* AddOrGetExisting<T>( 
   System.Runtime.Caching.CacheItem* item,
   System.Runtime.Caching.CacheItemPolicy* policy
) 
public:
System.Runtime.Caching.CacheItem^ AddOrGetExistinggeneric<typename T>
( 
   System.Runtime.Caching.CacheItem^ item,
   System.Runtime.Caching.CacheItemPolicy^ policy
) 

Parameters

item
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.

Type Parameters

T

Return Value

If a cache entry with the same key exists, the specified cache entry; otherwise, null.
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;
            
//Load the report from the database
List<ReportDetail> report = ReportLogic.MonthlySalesReport(month, year);
            
//Get the cache key based on the report parameters
string cacheKey = cache.BuildCacheKey("MonthlySalesReport", month, year);
            
CacheItem item = new CacheItem(cacheKey, report, "Default");
CacheItemPolicy policy = new CacheItemPolicy()
{
	AbsoluteExpiration = DateTimeOffset.Now.AddMinutes(20),
	SlidingExpiration = SmartCache.NoSlidingExpiration
};
            
CacheItem result = cache.AddOrGetExisting<List<ReportDetail>>(item, policy);
            
if (result == null)
	Console.WriteLine("The item was added to the cache");
else
	Console.WriteLine("The item already exists in the cache");
'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
            
'Load the report from the database
Dim report As List(Of ReportDetail) = ReportLogic.MonthlySalesReport(month, year)
            
'Get the cache key based on the report parameters
Dim cacheKey As String = cache.BuildCacheKey("MonthlySalesReport", month, year)
            
Dim item As New CacheItem(cacheKey, report, "Default")
Dim policy As New CacheItemPolicy() With {
	.AbsoluteExpiration = DateTimeOffset.Now.AddMinutes(20),
	.SlidingExpiration = SmartCache.NoSlidingExpiration
}
            
Dim result As CacheItem = cache.AddOrGetExisting(Of List(Of ReportDetail))(item, policy)
            
If result Is Nothing Then
	Console.WriteLine("The item was added to the cache")
Else
	Console.WriteLine("The item already exists in the cache")
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