public void TestContains() { using (var cacheProvider = new CacheProvider<string>((int)TimeSpan.FromSeconds(60).TotalMilliseconds)) { const int Key = 123; cacheProvider.Set(Key, "aValue1"); Assert.IsTrue(cacheProvider.Contains(Key)); cacheProvider.Set(Key, "aKey2"); Assert.IsTrue(cacheProvider.Contains(Key)); } }
/// <summary> /// /// </summary> /// <param name="command"></param> void SniffSprocParameters(SqlCommand command) { string cacheKey = command.CommandText; SqlParameter[] sprocParams = null; if (CacheProvider.Contains(cacheKey)) { sprocParams = CacheProvider.Get <SqlParameter[]>(cacheKey); } else { SqlCommandBuilder.DeriveParameters(command); sprocParams = new SqlParameter[command.Parameters.Count]; for (int i = 0; i < sprocParams.Length; ++i) { var param = command.Parameters[i] as SqlParameter; var paramClone = param as ICloneable; var clonedParam = paramClone.Clone(); sprocParams[i] = clonedParam as SqlParameter; } CacheProvider.Put(cacheKey, sprocParams, new Shinto.Cache.Modules.AbsoluteExpirationPolicy(cacheKey, sprocParams, DateTime.Now.AddHours(1))); return;//Command already has parameters, return to avoid adding, below } if (sprocParams != null && sprocParams.Length > 0) { //Add the params to the given command for (int i = 0; i < sprocParams.Length; ++i) { SqlParameter param = sprocParams[i]; var clonedParam = new SqlParameter(param.ParameterName, param.SqlDbType, param.Size, param.Direction, param.IsNullable, param.Precision, param.Scale, param.SourceColumn, param.SourceVersion, param.Value); command.Parameters.Add(clonedParam); } } }
protected virtual PreferenceItemCollection GetAllPreferences(string collectionName) { cacheLock.EnterUpgradeableReadLock(); try { Dictionary <string, PreferenceItemCollection> cachedItems = new Dictionary <string, PreferenceItemCollection> (); if (CacheProvider.Contains(this.Name)) { cachedItems = CacheProvider.Read(this.Name) as Dictionary <string, PreferenceItemCollection>; } if (cachedItems.ContainsKey(collectionName)) { return(cachedItems [collectionName]); } else { cacheLock.EnterWriteLock(); try { PreferenceItemCollection result = InitializePreferences(collectionName); cachedItems.Add(collectionName, result); CacheProvider.Update(this.Name, cachedItems); return(result); } finally { cacheLock.ExitWriteLock(); } } } finally { cacheLock.ExitUpgradeableReadLock(); } }
public virtual Form GetForm(string formName) { cacheLock.EnterUpgradeableReadLock(); try { Dictionary <string, Form> cachedItems = new Dictionary <string, Form>(); if (CacheProvider.Contains(this.Name)) { cachedItems = CacheProvider.Read(this.Name) as Dictionary <string, Form>; } if (cachedItems.ContainsKey(formName)) { return(cachedItems[formName]); } else { cacheLock.EnterWriteLock(); try { Form result = InitializeForm(formName); cachedItems.Add(formName, result); CacheProvider.Update(this.Name, cachedItems); return(result); } finally { cacheLock.ExitWriteLock(); } } } finally { cacheLock.ExitUpgradeableReadLock(); } }
public void Cache_Can_Remove() { GetOrAdd(); Assert.IsTrue(CacheProvider.Contains(CustomerKey)); CacheProvider.Remove(CustomerKey); Assert.IsFalse(CacheProvider.Contains(CustomerKey)); }
public void Cache_Can_Store() { GetOrAdd(); Assert.IsTrue(CacheProvider.Contains(CustomerKey)); }