Click or drag to resize

IAdhocCacheService Interface

Defines a service which can store commonly used objects in a transient cache

Namespace: SanteDB.Core.Services
Assembly: SanteDB.Core.Api (in SanteDB.Core.Api.dll) Version: 3.0.2081-alpha+b4ae72647f2cc271f89142f76fff26ad69e00f5a
Syntax
public interface IAdhocCacheService : IServiceImplementation

The IAdhocCacheService type exposes the following members.

Properties
 NameDescription
Public propertyServiceName Gets the service name
(Inherited from IServiceImplementation)
Top
Methods
 NameDescription
Public methodAddT Add the specified object to the cache
Public methodExists Returns true if key exists in the cache
Public methodGetT Gets the specified object from the cache
Public methodRemove Removes the specified object from the adhoc
Public methodRemoveAll Remove all keys matching patternKey
Public methodTryGetT Try to fetch key from the cache
Top
Remarks

The ad-hoc cache service differs from the data cache in that the ad-hoc cache can be used to store any data with any key and value within the caching technology implementation. The cache is commonly used to store repeat or commonly fetched data (for example policy decision outcomes, keys, reference term lookups, etc.).

The cache can be used to save fetching and querying data to/from the persistence layer.

Note to Implementers: Your implementation of this interface should not be a persistent cache (if possible to enforce). The callers of this interface typically assume a short lifecycle of data within the cache, and transient, rapid access should be prioritized over durability.

Example
Implementing a Cache Service
// A horrible implementation of the cache service that uses a simple dictionary
public class DictionaryCache : IAdHocCacheService {

    private ConcurrentDictionary<String, Object> m_cache = new ConcurrentDictionary<String, Object>();

    // Add an object to cache
    public void Add<T>(String key, T value, TimeSpan? timeout = null) {
        this.m_cache.TryAdd(key, value); // note: we won't implement timeouts
    }

    public T Get<T>(String key) {
        if(this.m_cache.TryGetValue(key, out T retVal))
        {
            return retVal;
        }
        else
        {
            return default(T);
        }
    }

    public bool Remove(String key) {
        this.m_cache.TryRemove(key, out _);
    }
}
See Also