How to implement distributed Cache in the .net Framework Project of version 4.8

295 Views Asked by At

I wanted to implement the Distributed Cache in the project with following code, but it is throwing null reference.

public class RedisCacheService : ICacheService
 {
     private readonly IDistributedCache _distributedcache;
     private readonly ConnectionMultiplexer _redisConnection;

 public RedisCacheService(IDistributedCache distributedcache)
     {
     var redisconstr = ConfigurationManager.ConnectionStrings["Redis"].ConnectionString;
     _redisConnection = ConnectionMultiplexer.Connect(redisconstr);
     _distributedcache = distributedcache;
     _distributedcache = (IDistributedCache)_redisConnection.GetDatabase();

 }

 public async Task<T> GetSlidingCache<T>(string key)
     {
     var redisObj = await _distributedcache.GetStringAsync(key);

     if (string.IsNullOrEmpty(redisObj))
         return default(T);

     return JsonConvert.DeserializeObject<T>(redisObj
             , new JsonSerializerSettings
             {
                 ReferenceLoopHandling = ReferenceLoopHandling.Serialize,
                 PreserveReferencesHandling = PreserveReferencesHandling.Objects
             });
 }

     public async Task<bool> SetSlidingCache<T>(string key, T value, TimeSpan timeSpan)
     {
         var strValue = JsonHandler.Serialize(value);

         await _distributedcache.SetStringAsync(key, strValue, new DistributedCacheEntryOptions
         {
             SlidingExpiration = timeSpan
         });

         return true;
     }
 }


For GetCache the data:

List<EmployeeRole> EmployeeRoles = _cacheService.GetSlidingCache<List<EmployeeRole>>(Constants.CACHE_CALENDARROLES).GetAwaiter().GetResult();

For SetCache the Data:

EmployeeRoles = ReadEmployeeRoles( i_RoleId,i_VendorId);

CacheHelper.Cache.SetCache<List<ModelEmployeeRole>>(Constants.CACHE_EmployeeROLES,       CalendarRoles, timeSpanMinutes);

It is using the below code for set the data. It is working fine.

 public class RedisCache : ICache
 {
     private readonly ConnectionMultiplexer _redisConnection;
     private readonly IDatabase _Cache;
     public RedisCache()
     {
         var redisconstr = ConfigurationManager.ConnectionStrings["Redis"].ConnectionString;
         _redisConnection = ConnectionMultiplexer.Connect(redisconstr);

         _Cache = _redisConnection.GetDatabase();
     }

     public async Task<T> GetCache<T>(string key)
     {
         var redisObj = await _Cache.StringGetAsync(key);
         if (!redisObj.HasValue)
             return default(T);

         return JsonConvert.DeserializeObject<T>(redisObj
                 , new JsonSerializerSettings
                 {
                     ReferenceLoopHandling = ReferenceLoopHandling.Serialize,
                     PreserveReferencesHandling = PreserveReferencesHandling.Objects
                 });
     }

     public async Task<bool> SetCache<T>(string key, T value,int timeSpan)
     {
         if (value == null)
             return true;

         var strVal = JsonConvert.SerializeObject(value
                 , Formatting.Indented
                 , new JsonSerializerSettings
                 {
                     ReferenceLoopHandling = ReferenceLoopHandling.Serialize,
                     PreserveReferencesHandling = PreserveReferencesHandling.Objects
                 });
        
         var cacheTimeSpan = timeSpan > 0 ? TimeSpan.FromMinutes(timeSpan) : TimeSpan.FromMinutes(10);

         //Take this settings from configuration
         int Hours = 10;
         
         return await _Cache.StringSetAsync(key, strVal,cacheTimeSpan);
     }
 }


But I want to implement the Distributed Cache for the project. It is not supporting the IDistributed Cache Interface which needs to pass the DistributedCacheEntryOptions as an Argument.

May I get any solution to implement the sliding cache technique in the project which is having version 4.8?

0

There are 0 best solutions below