Class CacheApi
Namespace: Casewhere.Runtime.DSL.Api
Assembly: Casewhere.Runtime.dll
public class CacheApi : ICacheApi, IDslApi
Inheritance
Implements
Inherited Members
object.ToString(), object.Equals(object), object.Equals(object, object), object.ReferenceEquals(object, object), object.GetHashCode(), object.GetType(), object.MemberwiseClone()
Extension Methods
ObjectExtension.ConvertToBsonValue(object), EnumExtensions.DeepClone<CacheApi>(CacheApi)
Constructors
CacheApi(ICachingService)
public CacheApi(ICachingService cachingService)
Parameters
cachingService
ICachingService
Methods
ContainsKey(string)
Determines whether a cache entry exists in the cache.
public bool ContainsKey(string key)
Parameters
key
string
The key.
Returns
true if the cache contains a cache entry whose key matches key; otherwise, false.
Examples
var cacheApi = ctx.Use<ICacheApi>();
var hasKey = cacheApi.ContainsKey("cache_key");
Get<T>(string)
Returns an entry from the cache.
public T Get<T>(string key)
Parameters
key
string
The key.
Returns
T
A reference to the cache entry that is identified by key, if the entry exists; otherwise, It will throw an exception.
Type Parameters
T
Type of the cached object.
Examples
var cacheApi = ctx.Use<ICacheApi>();
var entryValue = cacheApi.Get<object>("cache_key");
Exceptions
NotFoundException
The exception is thrown when the cache system not contains the cached object with the key
InvalidCastOperationException
The exception is thrown when the cached object can't be cast to an specified type by generic type T
.
Remove(string)
Removes the specified item from the cache.
public void Remove(string key)
Parameters
key
string
The key.
Examples
var cacheApi = ctx.Use<ICacheApi>();
cacheApi.Remove("cache_key");
Set<T>(string, T)
Inserts a cache entry into the cache by using a key and a value with default expiration time which is 15 minutes.
public void Set<T>(string key, T value)
Parameters
key
string
The key.
value
T
The value.
Type Parameters
T
Type of the cached object.
Examples
var cacheApi = ctx.Use<ICacheApi>();
cacheApi.Set("cache_key", "cache_value");
Remarks
If the specified entry does not exist, it is created. If the specified entry exists, it is updated.
Set<T>(string, T, TimeSpan)
Inserts a cache entry into the cache by using a key and a value and specifies time-based expiration details.
public void Set<T>(string key, T value, TimeSpan timeExpiration)
Parameters
key
string
The key.
value
T
The value.
timeExpiration
TimeSpan
The expiration time.
Type Parameters
T
Type of the cached object.
Examples
var cacheApi = ctx.Use<ICacheApi>();
cacheApi.Set("cache_key", "cache_value", new System.TimeSpan(30));
Remarks
If the specified entry does not exist, it is created. If the specified entry exists, it is updated.
Set<T>(string, T, TimeSpan, bool)
Inserts a cache entry into the cache by using a key and a value and specifies whether it is for sliding expiration or time-based expiration.
public void Set<T>(string key, T value, TimeSpan timeExpiration, bool sliding)
Parameters
key
string
The key.
value
T
The value.
timeExpiration
TimeSpan
The expiration time.
sliding
bool
If True, it is sliding expiration. Otherwise, time-based expiration.
Type Parameters
T
Type of the cached object.
Examples
var cacheApi = ctx.Use<ICacheApi>();
cacheApi.Set("cache_key", "cache_value", new System.TimeSpan(30), true);
Remarks
Inserts a cache entry into the cache by using a key and a value and specifies time-based expiration details.
Sliding expiration means the cache entry should be evicted if it has not been accessed in a given span of time.
TryGet<T>(string, out T)
Get item from cached if return true, otherwise it returns false
public bool TryGet<T>(string key, out T data)
Parameters
key
string
The key.
data
T
The cached data object.
Returns
True if the key is found in the cache; otherwise, False.
Type Parameters
T
Type of the cached object.
Examples
var cacheApi = ctx.Use<ICacheApi>();
var hasValue = cacheApi.TryGet<object>("cache_key", out object returnObject);