NET Caching Library Help
GetCacheItem<T> Method (SmartCache)
Example 




KellermanSoftware.NetCachingLibrary Namespace > SmartCache Class : GetCacheItem<T> Method
The key of the item in the cache
The name of the region
Get an item from the cache
Syntax
'Declaration
 
Public Function GetCacheItem(Of T)( _
   ByVal key As System.String, _
   Optional ByVal regionName As System.String _
) As System.Runtime.Caching.CacheItem
'Usage
 
Dim instance As SmartCache
Dim key As System.String
Dim regionName As System.String
Dim value As System.Runtime.Caching.CacheItem
 
value = instance.GetCacheItem(Of T)(key, regionName)
public System.Runtime.Caching.CacheItem GetCacheItem<T>( 
   System.string key,
   System.string regionName
)
public: System.Runtime.Caching.CacheItem* GetCacheItem<T>( 
   System.string* key,
   System.string* regionName
) 
public:
System.Runtime.Caching.CacheItem^ GetCacheItemgeneric<typename T>
( 
   System.String^ key,
   System.String^ regionName
) 

Parameters

key
The key of the item in the cache
regionName
The name of the region

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
CacheItem cacheItem = cache.GetCacheItem<List<ReportDetail>>(cacheKey);
            
//Not found in the cache
if (cacheItem == null)
{
	//Load the report from the database
	List<ReportDetail> report = ReportLogic.MonthlySalesReport(month, year);
            
	//Save to the cache using expiring 30 minutes from now for both the memory cache and the file cache
	bool result = cache.Add<List<ReportDetail>>(cacheKey, report, DateTimeOffset.Now.AddMinutes(30), "Default");
            
	//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 cacheItem As CacheItem = cache.GetCacheItem(Of List(Of ReportDetail))(cacheKey)
            
'Not found in the cache
If cacheItem Is Nothing Then
	'Load the report from the database
	Dim report As List(Of ReportDetail) = ReportLogic.MonthlySalesReport(month, year)
            
	'Save to the cache using expiring 30 minutes from now for both the memory cache and the file cache
	Dim result As Boolean = cache.Add(Of List(Of ReportDetail))(cacheKey, report, DateTimeOffset.Now.AddMinutes(30), "Default")
            
	'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