ASP.NET - Multiple Web applications - Sharing cache objects

Viewed 2428

I'm working on asp.net 4 web project , The project is build with 3 sub projects:

  • Site.Admin - Admin Website - ASP.NET Project
  • Site.Core - DB & BL - C# Library
  • Site.Web - Frontend Website. - ASP.NET Project

The project is hosted on a Single windows server 32 bit

I'm currently using ASP.NET Cache (HttpRuntime.Cache) to cache objects but the problem is that i cant share the cache between Frontend & Admin Websites.

From reading StackOverflow and other posts , i understand that the following options exist:

  1. Memcached - No stable windows version ?
  2. Couchbase - No free version ? overkill to replace asp.net cache ?
  3. AppFabric
  4. Redis - No stable windows version?

I'm looking for a free/non paid solution to replace asp.net cache.

I'd like to get some input regarding :

  1. Windows install compatibility

  2. Performance

  3. Ease of use (replacing existing cache code) & .NET API.

  4. Other alternatives ?

2 Answers

Basically you can create a web api service as repository for your cached object.

The solution in simple and using generics you can use the cache for diffent object type.

Some step to follows once che web.api is created.

1) create the connection string to your redis server

"ConnectionStrings": {
   "Redis": "127.0.0.1:6379"
}

2) modify the startup.cs

public void ConfigureServices(IServiceCollection services)
{
   // add redis cache service
   services.AddDistributedRedisCache(options => {
   options.Configuration = Configuration.GetConnectionString("Redis");
   options.InstanceName = "User_";
   });

   services.AddSession();
   services.AddMvc();
}

3) create a class to interact with your cache instance

namespace CacheRepository
{
// declare the class
public static class RedisCache
{

// save 
public static async Task SetObjectAsync<T>(IDistributedCache cache, string key, T value)
{
await cache.SetStringAsync(key, JsonConvert.SerializeObject(value));
}

// get
public static async Task<T> GetObjectAsync<T>(IDistributedCache cache, string key)
{
var value = await cache.GetStringAsync(key);
return value == null ? default(T) : JsonConvert.DeserializeObject<T>(value);
}

// verify if an object exists
public static async Task<bool> ExistObjectAsync<T>(IDistributedCache cache, string key)
{
var value = await cache.GetStringAsync(key);
return value == null ? false : true;
}
}
}

4) create the controller that expose the public method with the relative http verbs

// GET api/values
[HttpGet("{name}")]
public async Task<User> GetUser(string name)
{
  User user = new User();
  try
  {
    if (!string.IsNullOrEmpty(name))
    {
     user = await RedisCache.GetObjectAsync<User>(_Cache, name);
    }
  }
  catch (Exception ex)
  {
    throw ex;
  }
  return user;
}

this is a simple way ;)

here below you can find the article in which I describe every detail.

https://medium.com/@massimomannoni/code-web-api-with-redis-distributed-cache-6598dbe854d5

thanks Massimo

Related