Skip to main content

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 int value will be 1, otherwise 0
    if (redisClient.Exists(key) > 0)
    {
     var getKeyData = redisClient.Get(key);
     if (getKeyData != null)
      return SerializationHelper.Deserialize(getKeyData);
    }
    return null;
   }
  }

  public override void Remove(string key)
  {
   lock (cacheLock)
   {
    var cacheItem = redisClient.Get(key);
    redisClient.Delete(cacheItem);
    redisClient.Save();
   }
  }

  public override void Set(string key, object entry, System.DateTime utcExpiry)
  {
   lock (cacheLock)
   {
    var cacheItem = new CacheItem();
    cacheItem.Key = key;
    cacheItem.Data = SerializationHelper.Serialize(entry);
    cacheItem.Expiration = utcExpiry;
    this.Save(cacheItem, key);
   }
  }

  public void Save(CacheItem item, string key)
  {
   lock (cacheLock)
   {
    redisClient.Set(key, item.Data);
    redisClient.Save();
   }
  }
Add this to your web.config

    
     
      
     
    
   
Post a Comment

Popular posts from this blog

Compact and Repair an Access Database. Add Ref. to : AdoDb, Jro

< ?xml version="1.0" encoding="utf-8" ?>









using ADODB;
using JRO;
using System.Configuration;
using System.Data.OleDb;
using System.IO;

public class CompactAndRepairAccessDb : System.Windows.Forms.Form
{
private System.ComponentModel.Container components = null;
private JRO.JetEngine jro;
private System.Windows.Forms.Button btnConfirm;
private System.Windows.Forms.TextBox tbxOriginalDbSize;
private System.Windows.Forms.TextBox tbxCompactedDbSize;
private OleDbConnection cnn;

public CompactAndRepairAccessDb() {
InitializeComponent();

FileInfo fi = new FileInfo( ConfigurationSettings.AppSettings["PathOriginal"] );
int s = Convert.ToInt32( fi.Length/1000 );
this.tbxOriginalDbSize.Text = s.ToString() + " kb";
}

private void btnConfirm_Click(object sender, System.EventArgs e) {
// First close all instances of the database
// MUST HAVE EXCLUS…

VBScript to Automate login into gmail

Dim IE
Dim uSERNAME
Dim PASSWORD
Dim crtScreen
Set IE = CreateObject("InternetExplorer.Application")
USERNAME = "saudaziz"
PASSWORD = ""

With IE
.navigate "http://www.gmail.com"
.visible=1
End With

'wait a while until IE as finished to load
Do while IE.busy
loop
set WshShell = WScript.CreateObject("WScript.Shell")
Do While UCase(IE.Document.readyState) <> "COMPLETE"
WScript.Sleep 100
DoEvents
Loop
set WshShell=nothing
IE.document.all.Item("Email").value = USERNAME
IE.document.all.Item("pASSWD").value =pASSWORD
'IE.document.gaia_loginform.Email.value=USERNAME
IE.document.all.item("null").click
Set IE = Nothing

WScript.Quit(0)