Azure Redis Cache Example

This example shows an implementation of Azure Redis Cache with the LEADTOOLS Document Library.

This is a full-fledged example that can be used in a production environment. However, it requires saving large amounts of binary data directly into the cache. This approach may not be suitable if the documents are to be accessible from multiple servers due to the increase of re-syncing time. Refer to Azure Redis Cache with Storage Blobs Example for a better approach.

This example code requires the following nuget packages:

In the Document Service source code, replace the code inside ServiceHelper.CreateCache with:

C#
// Get the Redis Cache database object to use 
var configuration = "your-cache-url.redis.cache.windows.net,password=your-password"; 
var configurationOptions = ConfigurationOptions.Parse(configuration); 
// Increase the sync-timeout since we may be storing large binaries. 
// Note that this is not required for RedisWithBlobsObjectCache implementation. 
configurationOptions.SyncTimeout = 5000; 
ConnectionMultiplexer connection = ConnectionMultiplexer.Connect(configurationOptions); 
IDatabase redisDatabase = connection.GetDatabase(); 
 
// Create our LEADTOOLS ObjectCache wrapper 
_cache = new RedisObjectCache(redisDatabase); 

RedisObjectCache.cs

C#
using Leadtools; 
using Leadtools.Caching; 
using Leadtools.Codecs; 
using Leadtools.Svg; 
 
using Newtonsoft.Json; 
 
using StackExchange.Redis; 
 
using System; 
using System.Collections.Generic; 
using System.IO; 
 
namespace MyNamespace 
{ 
   /// <summary> 
   /// Wraps a Redis Cache IDatabase object to be used with the LEADTOOLS Document Library 
   /// </summary> 
   public class RedisObjectCache : ObjectCache 
   { 
      private RedisObjectCache() { } 
 
      /// <summary> 
      /// Initializes a LEADTOOLS Object Cache wrapper from a Redis cache database object. 
      /// </summary> 
      /// <param name="cache">Full initialized Redis database object ready to be used.</param> 
      public RedisObjectCache(StackExchange.Redis.IDatabase cache) 
      { 
         this.Cache = cache; 
      } 
 
      /// <summary> 
      /// The Redis cache database object being used. 
      /// </summary> 
      public StackExchange.Redis.IDatabase Cache { get; private set; } 
 
      // -------------------------------------------------------------------------------------- 
      // These members must be implemented by our class and are called by the Document toolkit 
      // -------------------------------------------------------------------------------------- 
 
      public override string Name 
      { 
         get 
         { 
            return "Redis Object Cache"; 
         } 
      } 
 
      public override CacheSerializationMode PolicySerializationMode 
      { 
         get 
         { 
            // Redis do not use this so we will just assume it is binary 
            return CacheSerializationMode.Binary; 
         } 
 
         set { throw new NotSupportedException(); } 
      } 
 
      public override CacheSerializationMode DataSerializationMode 
      { 
         get 
         { 
            // Binary meaning we will do our own serialization 
            return CacheSerializationMode.Binary; 
         } 
         set { throw new NotSupportedException(); } 
      } 
 
      public override DefaultCacheCapabilities DefaultCacheCapabilities 
      { 
         get 
         { 
            // We support serialization: Meaning, the toolkit can send us "fat" .NET objects 
            // we will serialize them and not change the original reference 
            return DefaultCacheCapabilities.Serialization; 
         } 
      } 
 
      public override CacheItem<T> AddOrGetExisting<T>(CacheItem<T> item, CacheItemPolicy policy) 
      { 
         // Method called when a cache item is added. 
         // Must return the old value 
 
         // Resolve the key, remember, we do not have regions 
         var resolvedKey = ResolveCacheKey(item.RegionName, item.Key); 
 
         CacheItem<T> oldItem = null; 
 
         // Get the old value (if any) 
         var existingValue = this.Cache.StringGet(resolvedKey); 
         if (existingValue.HasValue) 
         { 
            var oldValue = GetFromCache<T>(existingValue, item.RegionName, item.Key); 
            oldItem = new CacheItem<T>(item.Key, (T)oldValue, item.RegionName); 
         } 
 
         // Add new value 
         AddToCache(item, policy); 
 
         // Return old item 
         return oldItem; 
      } 
 
      public override CacheItem<T> GetCacheItem<T>(string key, string regionName) 
      { 
         // If we have an item with this key, return it. Otherwise return null 
 
         var resolvedKey = ResolveCacheKey(regionName, key); 
 
         CacheItem<T> item = null; 
 
         var value = this.Cache.StringGet(resolvedKey); 
         if (value.HasValue) 
         { 
            var itemValue = GetFromCache<T>(value, regionName, key); 
            item = new CacheItem<T>(key, (T)itemValue, regionName); 
         } 
 
         return item; 
      } 
 
      public override bool Contains(string key, string regionName) 
      { 
         // Check if the key is in the dictionary 
 
         var resolvedKey = ResolveCacheKey(regionName, key); 
         var exists = this.Cache.KeyExists(resolvedKey); 
         return exists; 
      } 
 
      public override bool UpdateCacheItem<T>(CacheItem<T> item) 
      { 
         // Update the item 
 
         if (item == null) 
            throw new ArgumentNullException("item"); 
 
         var resolvedKey = ResolveCacheKey(item.RegionName, item.Key); 
         var exists = this.Cache.KeyExists(resolvedKey); 
         if (exists) 
         { 
            AddToCache(item, null); 
         } 
 
         return exists; 
      } 
 
      public override T Remove<T>(string key, string regionName) 
      { 
         // Removed if found, return old value 
 
         T existingValue = default(T); 
         var resolvedKey = ResolveCacheKey(regionName, key); 
         if (this.Cache.KeyExists(resolvedKey)) 
         { 
            RedisValue value = this.Cache.StringGet(resolvedKey); 
            existingValue = (T)GetFromCache<T>(value, regionName, key); 
         } 
 
         // Delete it 
         DeleteItem(key, regionName); 
 
         return existingValue; 
      } 
 
      public override void DeleteItem(string key, string regionName) 
      { 
         // Delete if found 
 
         DeleteFromCache(regionName, key); 
      } 
 
      public override void UpdatePolicy(string key, CacheItemPolicy policy, string regionName) 
      { 
         // Redis Cache does not allow us to update the expiration policy of an item. 
      } 
 
      private static string ResolveCacheKey(string regionName, string key) 
      { 
         // Both must me non-empty strings 
         if (string.IsNullOrEmpty(regionName)) throw new InvalidOperationException("Region name must be a none empty string"); 
         if (string.IsNullOrEmpty(key)) throw new InvalidOperationException("Region key name must be a none empty string"); 
 
         // regionName might not be unique, key might not be unique, but combine them and we are guaranteed a unique key 
         return regionName + "-" + key; 
      } 
 
      // LEADTOOLS Document Library will call us with the following items: 
      // - Native types that are compatible with Redis: strings, byte arrays and JSON serializable objects. For these, we will just pass the along 
      // - RasterImage or SvgDocument, these are not compatible with Redis and we must serialize them first 
 
      private void AddToCache<T>(CacheItem<T> item, CacheItemPolicy policy) 
      { 
         // Get a Redis value from our item data 
         byte[] blob = null; 
         bool hasBlob = false; 
         string json = null; 
 
         var typeOfT = typeof(T); 
 
         if (typeOfT == typeof(RasterImage)) 
         { 
            blob = ImageToBlob(item.Value as RasterImage); 
            hasBlob = true; 
         } 
         else if (typeOfT == typeof(SvgDocument)) 
         { 
            blob = SvgToBlob(item.Value as SvgDocument); 
            hasBlob = true; 
         } 
         else if (typeOfT == typeof(byte[])) 
         { 
            blob = item.Value as byte[]; 
            hasBlob = true; 
         } 
         else 
         { 
            // JSON serialize it 
            json = JsonConvert.SerializeObject(item.Value); 
            hasBlob = false; 
         } 
 
         // If the sliding expiration is used, make it the absolute value 
         TimeSpan? expiry = null; 
 
         if (policy != null) 
         { 
            var expiryDate = policy.AbsoluteExpiration; 
            if (policy.SlidingExpiration > TimeSpan.Zero) 
            { 
               expiryDate = DateTime.UtcNow.Add(policy.SlidingExpiration); 
            } 
 
            // Now, we have a date, convert it to time span from now (all UTC) 
            expiry = expiryDate.Subtract(DateTime.UtcNow); 
         } 
 
         var resolvedKey = ResolveCacheKey(item.RegionName, item.Key); 
 
         // Set the cache item value 
         if (hasBlob) 
            this.Cache.StringSet(resolvedKey, blob, expiry); 
         else 
            this.Cache.StringSet(resolvedKey, json, expiry); 
      } 
 
      private object GetFromCache<T>(RedisValue value, string regionName, string key) 
      { 
         var typeOfT = typeof(T); 
         object result = null; 
 
         if (value.HasValue) 
         { 
            if (typeOfT == typeof(RasterImage) || typeOfT == typeof(SvgDocument) || typeOfT == typeof(byte[])) 
            { 
               // Read the blob 
               byte[] blob = (byte[])value; 
               if (typeOfT == typeof(RasterImage)) 
               { 
                  result = ImageFromBlob(blob); 
               } 
               else if (typeOfT == typeof(SvgDocument)) 
               { 
                  result = SvgFromBlob(blob); 
               } 
               else 
               { 
                  result = (byte[])blob; 
               } 
            } 
            else 
            { 
               // JSON deserialize it 
               result = JsonConvert.DeserializeObject<T>(value); 
            } 
         } 
 
         return result; 
      } 
 
      private void DeleteFromCache(string regionName, string key) 
      { 
         var resolvedKey = ResolveCacheKey(regionName, key); 
         if (this.Cache.KeyExists(resolvedKey)) 
            this.Cache.KeyDelete(resolvedKey); 
      } 
 
      // Helper methods to convert RasterImage or SvgDocument objects from/to byte[] 
      private static byte[] ImageToBlob(RasterImage image) 
      { 
         if (image == null) 
            return null; 
 
         // Save as PNG into a memory stream, use the byte[] data 
         using (var rasterCodecs = new RasterCodecs()) 
         { 
            using (var ms = new MemoryStream()) 
            { 
               rasterCodecs.Save(image, ms, RasterImageFormat.Png, 0); 
               return ms.GetBuffer(); 
            } 
         } 
      } 
 
      private static RasterImage ImageFromBlob(byte[] blob) 
      { 
         if (blob == null || blob.Length == 0) 
            return null; 
 
         // Save as PNG into a memory stream, use the byte[] data 
         using (var rasterCodecs = new RasterCodecs()) 
         { 
            using (var ms = new MemoryStream(blob)) 
            { 
               return rasterCodecs.Load(ms, 1); 
            } 
         } 
      } 
 
      private static byte[] SvgToBlob(SvgDocument svg) 
      { 
         if (svg == null) 
            return null; 
 
 
         using (var ms = new MemoryStream()) 
         { 
            svg.SaveToStream(ms, null); 
            return ms.GetBuffer(); 
         } 
      } 
 
      private static SvgDocument SvgFromBlob(byte[] blob) 
      { 
         if (blob == null || blob.Length == 0) 
            return null; 
 
         return SvgDocument.LoadFromMemory(blob, 0, blob.Length, null); 
      } 
 
      // 
      // These members must be over implemented by our class but are never called by the Document toolkit 
      // So just throw a not supported exception 
      // 
 
      // This is for default region support. We do not have that 
      public override object this[string key] 
      { 
         get 
         { 
            throw new NotSupportedException(); 
         } 
         set 
         { 
            throw new NotSupportedException(); 
         } 
      } 
 
      // Delete a region in one shot. We do not support that 
      // Note: This is only called if we have DefaultCacheCapabilities.CacheRegions. Since we do not, the caller is responsible for 
      // calling DeleteAll passing all the items of the region (which in turn will call DeleteItem for each) 
      public override void DeleteRegion(string regionName) 
      { 
         throw new NotSupportedException(); 
      } 
 
      // Begin adding an external resource. We do not support that 
      // Note: This is only called if we have DefaultCacheCapabilities.ExternalResources 
      public override Uri BeginAddExternalResource(string key, string regionName, bool readWrite) 
      { 
         throw new NotSupportedException(); 
      } 
 
      // End adding an external resource. We do not support that 
      // Note: This is only called if we have DefaultCacheCapabilities.ExternalResources 
      public override void EndAddExternalResource<T>(bool commit, string key, T value, CacheItemPolicy policy, string regionName) 
      { 
         throw new NotSupportedException(); 
      } 
 
      // Get the item external resource. We do not support that 
      // Note: This is only called if we have DefaultCacheCapabilities.ExternalResources 
      public override Uri GetItemExternalResource(string key, string regionName, bool readWrite) 
      { 
         throw new NotSupportedException(); 
      } 
 
      // Remove the item external resource. We do not support that 
      // Note: This is only called if we have DefaultCacheCapabilities.ExternalResources 
      public override void RemoveItemExternalResource(string key, string regionName) 
      { 
         throw new NotSupportedException(); 
      } 
 
      // Get the item virtual directory path. We do not support that 
      // Note: This is only called if we have DefaultCacheCapabilities.VirtualDirectory 
      public override Uri GetItemVirtualDirectoryUrl(string key, string regionName) 
      { 
         throw new NotSupportedException(); 
      } 
 
      // Getting number of items in the cache. We do not support that 
      public override long GetCount(string regionName) 
      { 
         throw new NotSupportedException(); 
      } 
 
      // Statistics. We do not support that 
      public override CacheStatistics GetStatistics() 
      { 
         throw new NotSupportedException(); 
      } 
 
      // Statistics. We do not support that 
      public override CacheStatistics GetStatistics(string key, string regionName) 
      { 
         throw new NotSupportedException(); 
      } 
 
      // Getting all the values. We do not support that 
      public override IDictionary<string, object> GetValues(IEnumerable<string> keys, string regionName) 
      { 
         throw new NotSupportedException(); 
      } 
 
      // Enumeration of the items. We do not support that 
      protected override IEnumerator<KeyValuePair<string, object>> GetEnumerator() 
      { 
         throw new NotSupportedException(); 
      } 
 
 
      // Enumeration of the keys. We do not support that 
      public override void EnumerateKeys(string region, EnumerateCacheEntriesCallback callback) 
      { 
         throw new NotSupportedException(); 
      } 
 
      // Enumeration of regions. We do not support that 
      public override void EnumerateRegions(EnumerateCacheEntriesCallback callback) 
      { 
         throw new NotSupportedException(); 
      } 
   } 
} 

Help Version 23.0.2024.5.10
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2024 LEAD Technologies, Inc. All Rights Reserved.

LEADTOOLS HTML5 JavaScript
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.