DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Scaling Databases With EclipseLink And Redis
  • Prompt Caching: Overriding Tokenization for Faster and More Cost-Effective AI
  • Caching Issues With the Spring Expression Language
  • A Practical Guide to Semantic Caching With Redis LangCache

Trending

  • How to Submit a Post to DZone
  • The "Just Read the Code" Myth Is Killing Your AI Workflow
  • Idempotency Under Concurrency: Why Your Application Check Isn't the Last Line of Defense
  • Beyond Manual Mapping: AI-Orchestrated Governance for Hybrid Cloud

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Software Design and Architecture
  3. Cloud Architecture
  4. Simple Sophisticated Object Cache Service Using Azure Redis

Simple Sophisticated Object Cache Service Using Azure Redis

Cache service has been an integral part of major distributed systems. So let's dive deep into how to build a sophisticated object cache service using Redis.

By 
Srivatsan Balakrishnan
·
Jan. 23, 23 · Code Snippet
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
3.6K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

The usage of cache has evolved a long way from simple string key-value pair to a string key to any object. Each application scenario demands the usage of cache in a different way. Most of the applications want to partition cache and reuse in other microservices or other parts of the system at the granular level. We will dive into detail about implementing a sophisticated object cache service at a granular level using Azure Redis in C#.

Prerequisites:

  1. Access to the Azure portal.
  2. Azure Redis provisioned of desired SKU.

The main objective of object storing is to set and get objects. But depending on application scenarios, usage of cache needs to be partitioned at the granular level. So let's define three variants of object store API.

API to: 

  1. Global cache
  2. Partition cache by single key to add more distribution and uniqueness.
  3. Partition cache by double key to add even more distribution and uniqueness.
C#
 
[ApiController]
[Route("api/objectcache")]
public class ObjectCacheController : ControllerBase
{
  
  
 // Simple global cache to set
  [HttpPost]
  public async Task<bool> Set ([FromBody] object value, [FromQuery] string key)
  {
  }

  // Simple global cache to get
  [HttpGet]
  public async Task<object> Get ([FromuQuery] string key)
  {
  }

  // Global cache partitioned by single record
  [HttpPost(“/pk1/{partitionkey1}”)]
  public async Task<bool> SetBySinglePartition ([FromBody] object value, [FromuQuery] string key, string partitionkey1)
  {
  }

  // Global cache partitioned by single entity
  [HttpGet(“/pk1/{partitionkey1}”)]
  public async Task<object> GetBySingleParititon ([FromuQuery] string key, string partitionkey1)
  {
  }

  // Global cache partitioned by two entities for more uniqueness
  [HttpPost(“/pk1/{partitionkey1}/pk2/{partitionkey2}”)]
  public async Task<bool> SetByDoublePartition ([FromBody] object value, [FromuQuery] string key, string partitionkey1, string partitionkey2)
  {
  }

  // Global cache partitioned by two entities for more uniqueness
  [HttpGet(“/pk1/{partitionkey1}/pk2/{partitionkey2}”)]
  public async Task<object> GetByDoublePartition ([FromQuery] string key, string partitionkey1, string partitionkey2)
  {
  }
}


To implement an object store, let's use Azure Redis as our backend storage. 

To leverage Redis, we need to provide a common interface for API to access.

Let's put backend storage as an enum option. Example:

C#
 
public enum ObjectStoreHandlerType
    {
        Redis,
	CosmosDb,
	PostGre
    }


Let's define the interface IObjectsStoreHandler that provides abstraction and functionalities toward backend storage.

C#
 
public interface IObjectStoreHandler
    {
        Task<T> GetObjectAsync(string key);

        Task<bool> SetObjectAsync<T>( string key, T value);

        Task<T> GetObjectBySinglePartitionAsync<T>(string key, string partitionkey1);

        Task<bool> SetObjectBySinglePartitionAsync<T>(string key, string partitionkey1, T value);

        Task<T> GetObjectByDoublePartitionAsync<T>(string key, string partitionkey1, string partitionkey2);

        Task<bool> SetObjectBySinglePartitionAsync<T>(string key, string partitionkey1, string partitionkey2, T value);


        ObjectStoreHandlerType Type { get; }
    }


To follow OOD paradigms, let's define an ObjectStoreHandlerfactory to create different ObjectStores based on the handler type.

C#
 
public interface IObjectStoreHandlerFactory
    {
        IObjectStoreHandler CreateObjectStoreHandler(ObjectStoreHandlerType objectStoreProviderType = ObjectStoreHandlerType.Redis);
    }

public class ObjectStoreHandlerFactory : IObjectStoreHandlerFactory
    {
        private readonly IEnumerable<IObjectStoreHandlers> _objectStorehandlers;

        public ObjectStoreProviderFactory(IEnumerable<IObjectStoreHandlers> objectStorehandlers)
        {
            _objectStorehandlers = objectStorehandlers;
        }

        public IObjectStoreHandler CreateObjectStoreHandler(ObjectStoreProviderType requestedHandlerType)
        {
            return _objectStorehandlers.SingleOrDefault(handlers => handlers.Type == requestedHandlerType);
        }
    }


Let's define Redis ObjectStoreHandler. Then, you can create a simple RedisClient.

C#
 
public class RedisObjectStoreHander : IObjectStoreHandler
    {
        private readonly RedisClient _redisClient;

        public RedisObjectStoreProvider(RedisClient redisClient)
        {
            _redisClient= redisClient;
        }

        public ObjectStoreHandlerType  Type  => ObjectStoreHandlerType.Redis;

        public async Task<T> GetObjectAsync<T>(string key)
        {
            RedisKey redisKey = $"{key}";

            try
            {
                    var redisValue = await _redisClient.GetConnectedDatabase().StringGetAsync(redisKey);
                    string strValue = redisValue.ToString();
                    if (!string.IsNullOrWhiteSpace(strValue))
                    {
                        return JsonConvert.DeserializeObject<T>(strValue);
                    }
            }
            catch (Exception ex)
            {
            }

            return default;
        }

	public async Task<bool> SetObjectAsync<T>(string key, T value)
        {
            RedisKey redisKey = $"{key}";
            RedisValue redisValue = JsonConvert.SerializeObject(value);
            try
            {
await _redisConnection.GetConnectedDatabase().StringSetAsync(redisKey, redisValue, flags: ComandFlags.DemandMaster); 
               
return true;
            }
            catch (Exception ex)
            {
            }

            return false;
        }
public async Task<T> GetObjectBySinglePartitionAsync<T>(string key, string partitionkey1);        {
            RedisKey redisKey = $" {{partitionkey1}}_{key}";

            try
            {
                    var redisValue = await _redisClient.GetConnectedDatabase().StringGetAsync(redisKey);
                    string strValue = redisValue.ToString();
                    if (!string.IsNullOrWhiteSpace(strValue))
                    {
                        return JsonConvert.DeserializeObject<T>(strValue);
                    }
            }
            catch (Exception ex)
            {
            }

            return default;
        }

	public async Task<bool> SetObjectBySinglePartitionAsync <T>( string key, string partitionkey1, T value)
        {
            RedisKey redisKey = $" {{partitionkey1}}_{key}";
            RedisValue redisValue = JsonConvert.SerializeObject(value);
            try
            {
await _redisConnection.GetConnectedDatabase().StringSetAsync(redisKey, redisValue, flags: ComandFlags.DemandMaster); 
               
return true;
            }
            catch (Exception ex)
            {
            }

            return false;
        }
public async Task<T> GetObjectByDoublePartitionAsync<T>(string key, string partitionkey1, string partitionkey2);        {
            RedisKey redisKey = $"{{{partitionkey1}_{partitionkey2}}}_{key}";

            try
            {
                    var redisValue = await _redisClient.GetConnectedDatabase().StringGetAsync(redisKey);
                    string strValue = redisValue.ToString();
                    if (!string.IsNullOrWhiteSpace(strValue))
                    {
                        return JsonConvert.DeserializeObject<T>(strValue);
                    }
            }
            catch (Exception ex)
            {
            }

            return default;
        }

	public async Task<bool> SetObjectByDoublePartitionAsync <T>( string key, string partitionkey1, string partitionkey2, T value)
        {
            RedisKey redisKey = $"{{{partitionkey1}_{partitionkey2}}}_{key}";
            RedisValue redisValue = JsonConvert.SerializeObject(value);
            try
            {
await _redisConnection.GetConnectedDatabase().StringSetAsync(redisKey, redisValue, flags: ComandFlags.DemandMaster); 
               
return true;
            }
            catch (Exception ex)
            {
            }

            return false;
        }

}


That's it. You can now integrate RedisObjectStoreHandler with the API layer. 

C#
 
private readonly IObjectStoreHandler _objectStoreHandler;

public ObjectCacheController(IObjectStoreHandlerFactory objectStoreHandlerFactory)
  {
      _objectStoreHandler = objectStoreHandlerFactory.Create(ObjectStoreHandlerType.Redis);
  }


You can now have a robust object store service that can take in unique keys with granular level partitions and can store/retrieve objects of the desired type.

azure Cache (computing) Object (computer science) Redis (company)

Opinions expressed by DZone contributors are their own.

Related

  • Scaling Databases With EclipseLink And Redis
  • Prompt Caching: Overriding Tokenization for Faster and More Cost-Effective AI
  • Caching Issues With the Spring Expression Language
  • A Practical Guide to Semantic Caching With Redis LangCache

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook