IAdhocCacheService Interface |
Namespace: SanteDB.Core.Services
The IAdhocCacheService type exposes the following members.
Name | Description | |
---|---|---|
ServiceName |
Gets the service name
(Inherited from IServiceImplementation.) |
Name | Description | |
---|---|---|
AddT |
Add the specified object to the cache
| |
Exists |
Returns true if key exists in the cache
| |
GetT |
Gets the specified object from the cache
| |
Remove |
Removes the specified object from the adhoc
|
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.
// 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 _); } }
bool IsAUser(String userName) { var cacheService = ApplicationServiceContext.Current.GetService<IAdhocCacheService>(); // Attempt to load what we're looking for in the cache var cachedResult = cacheService?.Get<bool?>($"isAUser.{userName}"); if(!cachedResult.HasValue) { var persistenceService = ApplicationServiceContext.Current.GetService<IRepositoryService<SecurityUser>>(); cachedResult = persistenceService.Count(o=>o.UserName == userName, AuthenticationContext.SystemPrincipal) > 0; cacheService?.Add($"isAUser.{userName}", cacheResult); } return cacheResult;