Skip to main content

Posts

Showing posts from June, 2012

Redis as OutputCacheProvider in ASP.NET

Basic implementation of Redis as OutputCacheProvider: public class RedisBRCacheProvider : OutputCacheProvider { private readonly RedisClient redisClient; private Object cacheLock = new Object(); public RedisBRCacheProvider() { redisClient = new RedisClient("locahost"); } public override object Add(string key, object entry, System.DateTime utcExpiry) { Debug.WriteLine("Cache.Add(" + key + ", " + entry + ", " + utcExpiry + ")"); lock (cacheLock) { var cacheItem = redisClient.Get(key); if (cacheItem != null) { if (cacheItem.Expiration.ToUniversalTime() <= DateTime.UtcNow) { redisClient.Remove(key); this.Set(key, entry, utcExpiry); } else return SerializationHelper.Deserialize(cacheItem.Data); } this.Set(key, entry, utcExpiry); } return entry; } public override object Get(string key) { lock (cacheLock) { //If Exists() the in…