Medical Web Viewer Custom Cache

Summary

The LEADTOOLS HTML5 Medical Web Viewer Services utilize a caching mechanism to speedup data access for reading DICOM dataset images and information. The default cache implementation is disk caching, using the caching pattern of cache-as-SOR, using the cache as though it were the primary system-of-record. The pattern delegates SOR reading and writing activities to the cache, so that application code is not directly responsible for dealing with cache details.

The caching mechanism is implemented in the Leadtools.Dicom.Imaging assembly. This library is generally only intended for internal use by the LEADTOOLS Medical Web Viewer Web Services. However, the library includes a public interface that allows you to provide a custom caching implementation to replace built-in implementation.

To create your own custom caching implementation, you need to implement the IDataCacheProvider interface, which is defined as:

IDataCacheProvider interface
public interface IDataCacheProvider 
{ 
   bool Contains(string id); 
   void Write<T>(string id, T obj, object options, DateTime? expires); 
   T Read<T>(string id, object options); 
   void Remove(string id); 
   void RemoveAll(string id); 
 
   bool IsWritable(); 
   bool IsReadable(); 
   bool IsRemovable(); 
} 

The usage pattern is:

C#
/// create a provider (IDataCacheProvider) 
var dataCache = new Leadtools.Dicom.Imaging.DiskDataCacheProvider(new Leadtools.Dicom.Imaging.DiskDataCacheStorage(settings.Storage)); 
/// pass it to the proxy  
var dicomSourceProxy = new DicomSourceProxy(dataCache); 

To create your own cache provider, simply implement the IDataCacheProvider interface and pass it to the DicomSourceProxy.

The following is a sample implementation of the IDataCacheProvider interface. This example uses ServiceStack packages to talk to a Redis server. Redis is the one of the most commonly used caching solutions, has a Windows implementation and is easily consumed from .NET (C#) code. In order to use Redis:

  1. Install Redis server for windows
  2. Create a new C# class library project
  3. Add the Nuget packages for ServiceStack
  4. Add a reference to Leadtools.Dicom.Imaging.dll

Example Redis cache implementation, with comments

C#
public class SampleRedisCacheProvider : IDataCacheProvider 
{ 
   RedisEndpoint _redisEndpoint; 
 
   public SampleRedisCacheProvider() 
   { 
      // Create a connection to the redis server, use the configured values you used when you installed the server 
      _redisEndpoint = new RedisEndpoint("localhost", 6379); 
   } 
         
   /// <summary> 
   /// Checks if the object identifed by (id) is available in the cache 
   /// </summary> 
   /// <param name="id">ID of the object to find.</param> 
   /// <returns>true if object is found, otherwise false</returns> 
   public bool Contains(string id) 
   { 
      using (var client = new RedisClient(_redisEndpoint)) 
      { 
         return client.ContainsKey(id); 
      } 
   } 
 
   /// <summary> 
   /// Writes an object of type T to the cache. 
   /// </summary> 
   /// <typeparam name="T">Type of the object.</typeparam> 
   /// <param name="id">ID of the object.</param> 
   /// <param name="obj">The object to be written.</param> 
   /// <param name="options">Reserved for future use.</param> 
   /// <param name="expires">Absolute expiry time. If this parameter is null, the object will have no expiry and it is up to the provider to free the cache item when it is no longer needed.</param> 
   public void Write<T>(string id, T obj, object options, DateTime? expires) 
   {             
      using (var client = new RedisClient(_redisEndpoint)) 
      { 
         var timeout = expires.HasValue ? expires.Value - DateTime.Now : TimeSpan.Zero; 
         client.As<T>().SetValue(id, obj, timeout); 
      } 
   } 
 
   /// <summary> 
   /// Reads an object of type T from the cache. 
   /// </summary> 
   /// <typeparam name="T">Type of the object.</typeparam> 
   /// <param name="id">ID of the object.</param> 
   /// <param name="options">Reserved for future use.</param> 
   /// <returns>The object retrieved from the cache.</returns> 
   public T Read<T>(string id, object options) 
   { 
      T result = default(T); 
      using (RedisClient client = new RedisClient(_redisEndpoint)) 
      { 
         var wrapper = client.As<T>(); 
         result = wrapper.GetValue(id); 
      } 
      return result; 
   } 
 
   /// <summary> 
   /// Removes an item from the cache. 
   /// </summary> 
   /// <param name="id">ID of the item to remove.</param> 
   void IDataCacheProvider.Remove(string id) 
   { 
      using (RedisClient client = new RedisClient(_redisEndpoint)) 
      { 
         client.Remove(id); 
      } 
   } 
 
   /// <summary> 
   /// This method performs a wildcard/fuzzy matching for the given id and deletes all related cached objects. 
   /// </summary> 
   /// <param name="id">ID of the item to remove.</param> 
   /// <remarks>This is an optional method and is not implemented here.</remarks> 
   public void RemoveAll(string id) 
   { 
      return; 
   } 
 
   /// <summary> 
   /// Checks if write capability is available. 
   /// </summary> 
   /// <returns>true if writable, otherwise false</returns> 
   /// <remarks>For example, if the caching storage is full, this should return false</remarks> 
   public bool IsWritable() 
   { 
      return true; 
   } 
 
   /// <summary> 
   /// Checks if read capability is available. 
   /// </summary> 
   /// <returns>true if readable, otherwise false</returns> 
   public bool IsReadable() 
   { 
      return true; 
   } 
 
   /// <summary> 
   /// Checks if remove capability is available. 
   /// </summary> 
   /// <returns>true if remove is available, otherwise false</returns> 
   public bool IsRemovable() 
   { 
      return true; 
   } 
} 

Note about the cache item id:

The id used to identify a cached object will always be in the following form:

xxxxxxx/xxxxxxx/xxxxxxx

You can use it as a whole or you may split it into 3 parts, separated by a '/' character.

Help Version 19.0.2017.10.19
Products | Support | Contact Us | Copyright Notices
© 1991-2017 LEAD Technologies, Inc. All Rights Reserved.
LEADTOOLS Medical Web Viewer