Suggestions for simple .NET distributed caching solution
Asked Answered
J

5

6

We have one server running Windows Server 2003 for our production web server. Our web site has different modules to it and each is ran within it's own application pool. This makes caching a bit of a problem since each module has it's own cache and often times multiple modules cache the same items. The problem is when an item in the cache is altered in one module, another module caching the same item cannot easily be refreshed.

Our site is written in ASP.NET 4.0 and we cache datasets, business objects, collections, lists, etc. using the standard HttpRuntime.Cache object as a simple key/value store. We don't use any file dependencies but we do use a time expiration. We only have about 400 users at once so we aren't doing anything too crazy.

It sounds like some sort of shared/distributed cache would help resolve this issue but I am not sure which one to use or if it would be better to roll our own since we do not need a lot of the features available in most of the products I've seen. I've seen some examples using WCF but most have suggested not to use their examples in production and I know little about WCF.

I'm looking for something simple, fast, lightweight, safe to run in production, and preferably free if possible but it does not have to be. It needs to run on Windows Server 2003 & work with ASP.NET 4.0.

Any suggestions or info?

Joanne answered 8/12, 2011 at 14:26 Comment(0)
T
7

memcached along with an ASP.NET provider is a popular choice. Bear in mind though that in .NET 4.0 the recommended way to do caching is to use the new ObjectCache instead of HttpRuntime.Cache. There is a built in memory implementation into the .NET framework (MemoryCache) and also you may checkout an implementation for memcached.

Twentytwo answered 8/12, 2011 at 14:35 Comment(2)
Thanks for article it looks very helpful, I'm going to look into his implementation.Joanne
The only provider that is a Distributed Cache is memcached, the others are not distributed.Stagemanage
B
13

Simple, fast, lightweight and safe sound like things like redis and memcached, which can be very effective as a central cache. For stackoverflow we use redis via BookSleeve (client) but most stores will work similarly. There is also an AppFabric cache, but that is considerably more complex.

Key points though:

  • your data will need to be serializable in some way
  • if you are currently using cache of large objects (like a big DataTable) you'll need to consider bandwidth implications, or make it more granular
  • you'd probably benefit from a 2-tier cache (one local memory, with the central store as secondary)
  • which means you'd also need to consider invalidation (we do that via the pub/sub API in redis)
Bernicebernie answered 8/12, 2011 at 14:40 Comment(2)
Thanks for the info, do you run on a Windows or Unix box? If Windows which redis port do you use? What made you go with redis over memcached?Joanne
@Joanne in production we use *nix for the server , as "fork" allows for a more efficient background save. This is not likely to be an issue for 400 users, though. I use the dmajkic binaries, which work fine. Re "why" - more features :pBernicebernie
T
7

memcached along with an ASP.NET provider is a popular choice. Bear in mind though that in .NET 4.0 the recommended way to do caching is to use the new ObjectCache instead of HttpRuntime.Cache. There is a built in memory implementation into the .NET framework (MemoryCache) and also you may checkout an implementation for memcached.

Twentytwo answered 8/12, 2011 at 14:35 Comment(2)
Thanks for article it looks very helpful, I'm going to look into his implementation.Joanne
The only provider that is a Distributed Cache is memcached, the others are not distributed.Stagemanage
T
3

Give Dache a try. It is an open source .NET distributed caching solution that is extremely performant. http://www.getdache.net

Teerell answered 23/10, 2013 at 3:38 Comment(2)
It has dual license. For commercial projects you'll have to buy a license.Crespo
@haney this site now shows property listing. I guess the project is not available any more?Archery
A
1

I implemented a simple Distributed Cache using Redis:

 public class RedisCacher
{
    private static Lazy<ConnectionMultiplexer> _lazyConnection;

    public static ConnectionMultiplexer Connection
    {
        get
        {
            return _lazyConnection.Value;
        }
    }

    public RedisCacher()
    {
        string redisConnectionString = $"your redis connection(IP, Port ...)";

        _lazyConnection = new Lazy<ConnectionMultiplexer>(() =>
        {
            return ConnectionMultiplexer.Connect(redisConnectionString);
        });
    }

    public void Delete(string key)
    {
        var redis = Connection.GetDatabase();
        redis.KeyDelete(key);
    }

    public T GetValue<T>(string key)
    {
        var redis = Connection.GetDatabase();

        string valueAsStringFromRedis = redis.StringGet(key);
        var value = JsonConvert.DeserializeObject<T>(valueAsStringFromRedis);
        return value;
    }

    public void Add<T>(string key, T value, TimeSpan expirationTime)
    {
        var redis = Connection.GetDatabase();
        string valueAsString = JsonConvert.SerializeObject(value);
        redis.StringSet(key, valueAsString, expiry: expirationTime);
    }
}

And the usage is:

 var cacher = new RedisCacher();
 cacher.Add(id.ToString(), request.Request.ProductTree, DateTimeOffset.UtcNow.AddMinutes(30).TimeOfDay);

.....
 var cacher = new RedisCacher();
 var productTree = cacher.GetValue<ProductTreeItem>(id);
Adhesion answered 14/12, 2020 at 18:59 Comment(0)
L
0

if price is not an issue, then NCache can be the best available option for you.Its latest version ( NCache 4.1 ) works with .NET 4.0

Lupitalupo answered 3/7, 2012 at 7:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.