Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 24 additions & 19 deletions Ovicus.Caching/SqlCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ public class SqlCache : ObjectCache
{
private string name;
private string connectionString;
private string schemaName = "dbo"; // Default schema name
private string tableName = "Cache"; // Default table name
private static readonly TimeSpan OneDay = new TimeSpan(24, 0, 0); // 1 day

Expand All @@ -56,13 +57,17 @@ public SqlCache(string name, string connectionString)
this.name = name;
}

public SqlCache(string name, string connectionString, string tableName)
public SqlCache(string name, string connectionString, string schemaName, string tableName)
: this(name, connectionString)
{
if (string.IsNullOrEmpty(tableName))
throw new ArgumentException("A valid name for the cache table should be provided", "tableName");

if (string.IsNullOrEmpty(schemaName))
throw new ArgumentException("A valid schema for the cache table should be provided", "schemaName");

this.tableName = tableName;
this.schemaName = schemaName;
}

private static byte[] Serialize(object value)
Expand Down Expand Up @@ -92,7 +97,7 @@ private void InsertOrUpdateEntry(string key, object value, CacheItemPolicy polic
SqlCommand cmdIns = new SqlCommand();
cmdIns.Connection = con;

const string cmdText = @"MERGE [{0}] as cache
const string cmdText = @"MERGE [{0}].{1} as cache
USING (VALUES (@Key)) as src ([Key])
ON (cache.[Key] = src.[Key])

Expand All @@ -110,7 +115,7 @@ WHEN NOT MATCHED THEN
VALUES (@Key, @Value, @Created, @LastAccess, @SlidingExpirationTimeInMinutes, @AbsoluteExpirationTime, @ObjectType) ;";


cmdIns.CommandText = string.Format(cmdText, tableName);
cmdIns.CommandText = string.Format(cmdText, this.schemaName, this.tableName);

cmdIns.Parameters.AddWithValue("@Key", key);
cmdIns.Parameters.AddWithValue("@Created", DateTime.Now);
Expand Down Expand Up @@ -138,9 +143,9 @@ private void InsertEntry(string key, object value, CacheItemPolicy policy)
SqlCommand cmdIns = new SqlCommand();
cmdIns.Connection = con;

const string cmdText = "INSERT INTO {0} ([Key], [Value], Created, LastAccess, SlidingExpirationTimeInMinutes, AbsoluteExpirationTime, ObjectType) " +
const string cmdText = "INSERT INTO [{0}].{1} ([Key], [Value], Created, LastAccess, SlidingExpirationTimeInMinutes, AbsoluteExpirationTime, ObjectType) " +
"VALUES (@Key, @Value, @Created, @LastAccess, @SlidingExpirationTimeInMinutes, @AbsoluteExpirationTime, @ObjectType)";
cmdIns.CommandText = string.Format(cmdText, tableName);
cmdIns.CommandText = string.Format(cmdText, this.schemaName, this.tableName);

cmdIns.Parameters.AddWithValue("@Key", key);
cmdIns.Parameters.AddWithValue("@Created", DateTime.Now);
Expand Down Expand Up @@ -168,10 +173,10 @@ private void UpdateEntry(string key, object value, CacheItemPolicy policy)
SqlCommand cmdUpd = new SqlCommand();
cmdUpd.Connection = con;

const string cmdText = "UPDATE {0} SET [Value]=@Value, Created=@Created, LastAccess=@LastAccess, " +
const string cmdText = "UPDATE [{0}].{1} SET [Value]=@Value, Created=@Created, LastAccess=@LastAccess, " +
"SlidingExpirationTimeInMinutes=@SlidingExpirationTimeInMinutes, AbsoluteExpirationTime=@AbsoluteExpirationTime, " +
"ObjectType=@ObjectType WHERE [Key] = @Key";
cmdUpd.CommandText = string.Format(cmdText, tableName);
cmdUpd.CommandText = string.Format(cmdText, this.schemaName, this.tableName);

cmdUpd.Parameters.AddWithValue("@Key", key);
cmdUpd.Parameters.AddWithValue("@Created", DateTime.Now);
Expand Down Expand Up @@ -255,8 +260,8 @@ public override object Get(string key, string regionName = null)
SqlCommand cmdSel = new SqlCommand();
cmdSel.Connection = con;

string cmdText = "SELECT [Value], Created, LastAccess, SlidingExpirationTimeInMinutes, AbsoluteExpirationTime FROM {0} WHERE [Key] = @Key";
cmdSel.CommandText = string.Format(cmdText, tableName);
string cmdText = "SELECT [Value], Created, LastAccess, SlidingExpirationTimeInMinutes, AbsoluteExpirationTime FROM [{0}].{1} WHERE [Key] = @Key";
cmdSel.CommandText = string.Format(cmdText, this.schemaName, this.tableName);

cmdSel.Parameters.AddWithValue("@Key", key);
con.Open();
Expand Down Expand Up @@ -293,8 +298,8 @@ public override object Get(string key, string regionName = null)
SqlCommand cmdUpd = new SqlCommand();
cmdUpd.Connection = con;

cmdText = "UPDATE {0} SET LastAccess=@LastAccess WHERE [Key] = @Key";
cmdUpd.CommandText = string.Format(cmdText, tableName);
cmdText = "UPDATE [{0}].{1} SET LastAccess=@LastAccess WHERE [Key] = @Key";
cmdUpd.CommandText = string.Format(cmdText, this.schemaName, this.tableName);

cmdUpd.Parameters.AddWithValue("@Key", key);
cmdUpd.Parameters.AddWithValue("@LastAccess", DateTime.Now);
Expand Down Expand Up @@ -323,8 +328,8 @@ public override long GetCount(string regionName = null)
SqlCommand cmdSel = new SqlCommand();
cmdSel.Connection = con;

const string cmdText = "SELECT COUNT([Key]) FROM {0} WHERE (AbsoluteExpirationTime > GETDATE()) OR (AbsoluteExpirationTime = NULL AND SlidingExpirationTimeInMinutes <> NULL)";
cmdSel.CommandText = string.Format(cmdText, tableName);
const string cmdText = "SELECT COUNT([Key]) FROM [{0}].{1} WHERE (AbsoluteExpirationTime > GETDATE()) OR (AbsoluteExpirationTime = NULL AND SlidingExpirationTimeInMinutes <> NULL)";
cmdSel.CommandText = string.Format(cmdText, this.schemaName, this.tableName);

con.Open();

Expand All @@ -343,8 +348,8 @@ protected override IEnumerator<KeyValuePair<string, object>> GetEnumerator()
SqlCommand cmdSel = new SqlCommand();
cmdSel.Connection = con;

const string cmdText = "SELECT [Key], [Value] FROM {0} WHERE (AbsoluteExpirationTime > GETDATE()) OR (AbsoluteExpirationTime = NULL AND SlidingExpirationTimeInMinutes <> NULL)";
cmdSel.CommandText = string.Format(cmdText, tableName);
const string cmdText = "SELECT [Key], [Value] FROM [{0}].{1} WHERE (AbsoluteExpirationTime > GETDATE()) OR (AbsoluteExpirationTime = NULL AND SlidingExpirationTimeInMinutes <> NULL)";
cmdSel.CommandText = string.Format(cmdText, this.schemaName, this.tableName);

con.Open();

Expand Down Expand Up @@ -387,8 +392,8 @@ private void Remove(string key)
SqlCommand cmdDel = new SqlCommand();
cmdDel.Connection = con;

const string cmdText = "DELETE FROM {0} WHERE [Key] = @Key";
cmdDel.CommandText = string.Format(cmdText, tableName);
const string cmdText = "DELETE FROM [{0}].{1} WHERE [Key] = @Key";
cmdDel.CommandText = string.Format(cmdText, this.schemaName, this.tableName);

cmdDel.Parameters.AddWithValue("@Key", key);
con.Open();
Expand Down Expand Up @@ -443,8 +448,8 @@ public void Flush()

// TODO: Remove items with sliding expiration time

const string cmdText = "DELETE FROM {0} WHERE (AbsoluteExpirationTime <= GETDATE())";
cmdDel.CommandText = string.Format(cmdText, tableName);
const string cmdText = "DELETE FROM [{0}].{1} WHERE (AbsoluteExpirationTime <= GETDATE())";
cmdDel.CommandText = string.Format(cmdText, this.schemaName, this.tableName);

con.Open();
cmdDel.ExecuteNonQuery();
Expand Down