NET Caching Library Help
AddOrGetExisting<T>(String,T,DateTimeOffset,String) Method
Example 




KellermanSoftware.NetCachingLibrary Namespace > SmartCache Class > AddOrGetExisting Method : AddOrGetExisting<T>(String,T,DateTimeOffset,String) Method
A unique identifier for the cache entry.
The object to insert.
The fixed date and time at which the cache entry will expire.
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 a cache entry into the cache, by using a key, an object for the cache entry, an absolute expiration value, and an optional region to add the cache into. The absolute expiration will be the same for both the primary and secondary cache providers.
Syntax
'Declaration
 
Public Overloads Function AddOrGetExisting(Of T)( _
   ByVal key As System.String, _
   ByVal value As T, _
   ByVal absoluteExpiration As System.DateTimeOffset, _
   Optional ByVal regionName As System.String _
) As T
'Usage
 
Dim instance As SmartCache
Dim key As System.String
Dim value As T
Dim absoluteExpiration As System.DateTimeOffset
Dim regionName As System.String
Dim value As T
 
value = instance.AddOrGetExisting(Of T)(key, value, absoluteExpiration, regionName)
public T AddOrGetExisting<T>( 
   System.string key,
   T value,
   System.DateTimeOffset absoluteExpiration,
   System.string regionName
)
public: T* AddOrGetExisting<T>( 
   System.string* key,
   T* value,
   System.DateTimeOffset absoluteExpiration,
   System.string* regionName
) 
public:
T^ AddOrGetExistinggeneric<typename T>
( 
   System.String^ key,
   T^ value,
   System.DateTimeOffset absoluteExpiration,
   System.String^ regionName
) 

Parameters

key
A unique identifier for the cache entry.
value
The object to insert.
absoluteExpiration
The fixed date and time at which the cache entry will expire.
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

Return Value

If a cache entry with the same key exists, the specified cache entry's value; 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);
            
List<ReportDetail> result = cache.AddOrGetExisting<List<ReportDetail>>(cacheKey, report, DateTimeOffset.Now.AddMinutes(20), "Default");
            
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 result As List(Of ReportDetail) = cache.AddOrGetExisting(Of List(Of ReportDetail))(cacheKey, report, DateTimeOffset.Now.AddMinutes(20), "Default")
            
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