internal static Skin GetSkin(string viewName) { string skinFile; if (!_cache.TryGetValue(viewName, out skinFile)) { string skinFileName; if (viewName.StartsWith("~/")) { skinFileName = string.Format("{0}/Skins/{1}.html", WebAppConfig.ThemePath, viewName.Substring(2)); } else if (viewName.StartsWith("/")) { skinFileName = string.Format("{0}/Skins/{1}.html", WebAppConfig.ThemePath, viewName.Substring(1)); } else { skinFileName = string.Format("{0}/Skins/{1}.html", WebAppConfig.ThemePath, viewName); } skinFile = WebAppContext.Server.MapPath(skinFileName); if (!File.Exists(skinFile)) { throw new FileNotFoundException("Unable to find the skin file.", skinFileName); } _cache.Add(viewName, skinFile); } return(Skin.CreateInstance(skinFile)); }
public void Test_Sliding() { MockTimeProvider time = new MockTimeProvider(); time.Now = new DateTime(2000, 1, 1); SmartCache <int> cache = new SmartCache <int>(5, time); int item; cache.Add("1", 1, TimeSpan.FromMinutes(100)); cache.Add("2", 2, TimeSpan.FromMinutes(200)); cache.Add("3", 3, TimeSpan.FromMinutes(300)); time.Now += TimeSpan.FromMinutes(20); Assert.IsTrue(cache.TryGetValue("1", out item)); Assert.AreEqual(3, cache.ItemCount); time.Now += TimeSpan.FromMinutes(150); Assert.IsFalse(cache.TryGetValue("1", out item)); Assert.IsTrue(cache.TryGetValue("2", out item)); Assert.AreEqual(2, cache.ItemCount); time.Now += TimeSpan.FromDays(1); Assert.IsFalse(cache.TryGetValue("1", out item)); Assert.IsFalse(cache.TryGetValue("2", out item)); Assert.IsFalse(cache.TryGetValue("3", out item)); Assert.AreEqual(0, cache.ItemCount); }
public IExpression Parse(string s, TokenPosition position) { IExpression expression; if (_expressionCache.TryGetValue(s, out expression)) { return(expression); } expression = ParseToRPN(s, position).Compile(); _expressionCache.Add(s, expression); return(expression); }
public IExpression Parse(string s, TokenPosition position) { IExpression expression; if (_expressionCache.TryGetValue(s, out expression)) { return(expression); } ExpressionToken[] tokens = _tokenizer.Tokenize(s, position).Where(t => t.TokenType != TokenType.WhiteSpace).ToArray(); expression = new ExpressionCompiler(this, tokens).Compile(); _expressionCache.Add(s, expression); return(expression); }
public void ThreadedTest() { SmartCache <int> cache = new SmartCache <int>(50); Thread[] threads = new Thread[50]; int exceptionCount = 0; object exceptionCountLock = new object(); for (int i = 0; i < 50; i++) { threads[i] = new Thread(data => { try { for (int i1 = 0; i1 < 1000; i1++) { int x; if (!cache.TryGetValue(i1.ToString(), out x)) { cache.Add(i1.ToString(), i1); } } } catch { lock (exceptionCountLock) exceptionCount++; throw; } }); threads[i].Start(); } for (int i = 0; i < 50; i++) { threads[i].Join(); } Assert.AreEqual(50, cache.ItemCount); Assert.AreEqual(0, exceptionCount); }
private void ThreadCacheRemove(object data) { SmartCache <int> cache = (SmartCache <int>)data; try { Random random = new Random(); for (int i = 0; i < 10000; i++) { int x; if (!cache.TryGetValue(i.ToString(), out x)) { if (random.Next() % 4 != 0) { cache.Remove(i.ToString()); } else { cache.Add(i.ToString(), i); } } else { if (random.Next() % 3 == 0) { cache.Remove(i.ToString()); } } } } catch { lock (_exceptionCountLock) _exceptionCount++; throw; } }
private void trd(object data) { SmartCache <int> cache = (SmartCache <int>)data; try { for (int i = 0; i < 1000; i++) { int x; if (!cache.TryGetValue(i.ToString(), out x)) { cache.Add(i.ToString(), i); } } } catch { lock (_exceptionCountLock) _exceptionCount++; throw; } }
public bool CheckPermission(string controllerName, string actionMethod) { string key = string.Concat(controllerName, "$", actionMethod); long value; if (_methods.TryGetValue(key, out value)) { long userId = WebAppContext.Session.UserId; string cacheKey = string.Concat(userId, ":", key); bool pass; if (!_cache.TryGetValue(cacheKey, out pass)) { long right = WebAppContext.Session.Right; if (value == 0) { value = SecurityManager.GetSecurity(key); _methods[key] = value; } pass = (right & value) > 0; _cache.Add(key, pass); } return(pass); } else { return(true); } }
public void Test1() { SmartCache <int> cache = new SmartCache <int>(5); int item; Assert.AreEqual(0, cache.ItemCount); Assert.IsFalse(cache.TryGetValue("1", out item)); cache.Add("1", 1); Assert.AreEqual(1, cache.ItemCount); Assert.IsTrue(cache.TryGetValue("1", out item)); cache.Add("2", 2); cache.Add("3", 3); cache.Add("4", 4); cache.Add("5", 5); Assert.AreEqual(5, cache.ItemCount); Assert.IsTrue(cache.TryGetValue("1", out item)); Assert.AreEqual(1, item); Assert.IsTrue(cache.TryGetValue("2", out item)); Assert.AreEqual(2, item); Assert.IsTrue(cache.TryGetValue("3", out item)); Assert.AreEqual(3, item); Assert.IsTrue(cache.TryGetValue("4", out item)); Assert.AreEqual(4, item); Assert.IsTrue(cache.TryGetValue("5", out item)); Assert.AreEqual(5, item); cache.Add("6", 6); Assert.AreEqual(5, cache.ItemCount); Assert.IsFalse(cache.TryGetValue("1", out item)); Assert.IsTrue(cache.TryGetValue("6", out item)); Assert.IsTrue(cache.TryGetValue("3", out item)); cache.Add("7", 7); cache.Add("8", 8); cache.Add("9", 9); Assert.AreEqual(5, cache.ItemCount); Assert.IsFalse(cache.TryGetValue("1", out item)); Assert.IsFalse(cache.TryGetValue("2", out item)); Assert.IsTrue(cache.TryGetValue("3", out item)); Assert.AreEqual(3, item); Assert.IsFalse(cache.TryGetValue("4", out item)); Assert.IsFalse(cache.TryGetValue("5", out item)); Assert.IsTrue(cache.TryGetValue("6", out item)); Assert.AreEqual(6, item); Assert.IsTrue(cache.TryGetValue("7", out item)); Assert.AreEqual(7, item); Assert.IsTrue(cache.TryGetValue("8", out item)); Assert.AreEqual(8, item); Assert.IsTrue(cache.TryGetValue("9", out item)); Assert.AreEqual(9, item); Assert.IsTrue(cache.TryGetValue("7", out item)); Assert.AreEqual(7, item); cache.CacheSize = 2; Assert.AreEqual(2, cache.ItemCount); Assert.IsFalse(cache.TryGetValue("1", out item)); Assert.IsFalse(cache.TryGetValue("2", out item)); Assert.IsFalse(cache.TryGetValue("3", out item)); Assert.IsFalse(cache.TryGetValue("4", out item)); Assert.IsFalse(cache.TryGetValue("5", out item)); Assert.IsFalse(cache.TryGetValue("6", out item)); Assert.IsTrue(cache.TryGetValue("7", out item)); Assert.IsFalse(cache.TryGetValue("8", out item)); Assert.IsTrue(cache.TryGetValue("9", out item)); cache.ClearCache(); Assert.AreEqual(0, cache.ItemCount); Assert.IsFalse(cache.TryGetValue("1", out item)); Assert.IsFalse(cache.TryGetValue("2", out item)); Assert.IsFalse(cache.TryGetValue("3", out item)); Assert.IsFalse(cache.TryGetValue("4", out item)); Assert.IsFalse(cache.TryGetValue("5", out item)); Assert.IsFalse(cache.TryGetValue("6", out item)); Assert.IsFalse(cache.TryGetValue("7", out item)); Assert.IsFalse(cache.TryGetValue("8", out item)); Assert.IsFalse(cache.TryGetValue("9", out item)); }
public void Test_Remove() { SmartCache <int> cache = new SmartCache <int>(5); int item; Assert.AreEqual(0, cache.ItemCount); Assert.IsFalse(cache.TryGetValue("1", out item)); cache.Add("1", 1); Assert.AreEqual(1, cache.ItemCount); cache.Add("1", 1); cache.Add("2", 2); cache.Add("3", 3); cache.Add("4", 4); cache.Add("5", 5); Assert.AreEqual(5, cache.ItemCount); cache.Remove("3"); Assert.IsTrue(cache.TryGetValue("1", out item)); Assert.AreEqual(1, item); Assert.IsTrue(cache.TryGetValue("2", out item)); Assert.AreEqual(2, item); Assert.IsFalse(cache.TryGetValue("3", out item)); Assert.IsTrue(cache.TryGetValue("4", out item)); Assert.AreEqual(4, item); Assert.IsTrue(cache.TryGetValue("5", out item)); Assert.AreEqual(5, item); Assert.AreEqual(4, cache.ItemCount); cache.Remove("1"); Assert.IsFalse(cache.TryGetValue("1", out item)); Assert.IsTrue(cache.TryGetValue("2", out item)); Assert.AreEqual(2, item); Assert.IsFalse(cache.TryGetValue("3", out item)); Assert.IsTrue(cache.TryGetValue("4", out item)); Assert.AreEqual(4, item); Assert.IsTrue(cache.TryGetValue("5", out item)); Assert.AreEqual(5, item); Assert.AreEqual(3, cache.ItemCount); cache.Add("3", 3); Assert.IsFalse(cache.TryGetValue("1", out item)); Assert.IsTrue(cache.TryGetValue("2", out item)); Assert.IsTrue(cache.TryGetValue("3", out item)); Assert.IsTrue(cache.TryGetValue("4", out item)); Assert.IsTrue(cache.TryGetValue("5", out item)); Assert.AreEqual(4, cache.ItemCount); cache.Remove("6"); Assert.IsFalse(cache.TryGetValue("1", out item)); Assert.IsTrue(cache.TryGetValue("2", out item)); Assert.IsTrue(cache.TryGetValue("3", out item)); Assert.IsTrue(cache.TryGetValue("4", out item)); Assert.IsTrue(cache.TryGetValue("5", out item)); Assert.AreEqual(4, cache.ItemCount); }
public void Test_Sliding() { MockTimeProvider time = new MockTimeProvider(); time.Now = new DateTime(2000, 1, 1); SmartCache<int> cache = new SmartCache<int>(5, time); int item; cache.Add("1", 1, TimeSpan.FromMinutes(100)); cache.Add("2", 2, TimeSpan.FromMinutes(200)); cache.Add("3", 3, TimeSpan.FromMinutes(300)); time.Now += TimeSpan.FromMinutes(20); Assert.IsTrue(cache.TryGetValue("1", out item)); Assert.AreEqual(3, cache.ItemCount); time.Now += TimeSpan.FromMinutes(150); Assert.IsFalse(cache.TryGetValue("1", out item)); Assert.IsTrue(cache.TryGetValue("2", out item)); Assert.AreEqual(2, cache.ItemCount); time.Now += TimeSpan.FromDays(1); Assert.IsFalse(cache.TryGetValue("1", out item)); Assert.IsFalse(cache.TryGetValue("2", out item)); Assert.IsFalse(cache.TryGetValue("3", out item)); Assert.AreEqual(0,cache.ItemCount); }
public void Test_Remove() { SmartCache<int> cache = new SmartCache<int>(5); int item; Assert.AreEqual(0, cache.ItemCount); Assert.IsFalse(cache.TryGetValue("1", out item)); cache.Add("1", 1); Assert.AreEqual(1, cache.ItemCount); cache.Add("1", 1); cache.Add("2", 2); cache.Add("3", 3); cache.Add("4", 4); cache.Add("5", 5); Assert.AreEqual(5, cache.ItemCount); cache.Remove("3"); Assert.IsTrue(cache.TryGetValue("1", out item)); Assert.AreEqual(1, item); Assert.IsTrue(cache.TryGetValue("2", out item)); Assert.AreEqual(2, item); Assert.IsFalse(cache.TryGetValue("3", out item)); Assert.IsTrue(cache.TryGetValue("4", out item)); Assert.AreEqual(4, item); Assert.IsTrue(cache.TryGetValue("5", out item)); Assert.AreEqual(5, item); Assert.AreEqual(4, cache.ItemCount); cache.Remove("1"); Assert.IsFalse(cache.TryGetValue("1", out item)); Assert.IsTrue(cache.TryGetValue("2", out item)); Assert.AreEqual(2, item); Assert.IsFalse(cache.TryGetValue("3", out item)); Assert.IsTrue(cache.TryGetValue("4", out item)); Assert.AreEqual(4, item); Assert.IsTrue(cache.TryGetValue("5", out item)); Assert.AreEqual(5, item); Assert.AreEqual(3, cache.ItemCount); cache.Add("3", 3); Assert.IsFalse(cache.TryGetValue("1", out item)); Assert.IsTrue(cache.TryGetValue("2", out item)); Assert.IsTrue(cache.TryGetValue("3", out item)); Assert.IsTrue(cache.TryGetValue("4", out item)); Assert.IsTrue(cache.TryGetValue("5", out item)); Assert.AreEqual(4, cache.ItemCount); cache.Remove("6"); Assert.IsFalse(cache.TryGetValue("1", out item)); Assert.IsTrue(cache.TryGetValue("2", out item)); Assert.IsTrue(cache.TryGetValue("3", out item)); Assert.IsTrue(cache.TryGetValue("4", out item)); Assert.IsTrue(cache.TryGetValue("5", out item)); Assert.AreEqual(4, cache.ItemCount); }
public void Test1() { SmartCache<int> cache = new SmartCache<int>(5); int item; Assert.AreEqual(0, cache.ItemCount); Assert.IsFalse(cache.TryGetValue("1", out item)); cache.Add("1",1); Assert.AreEqual(1, cache.ItemCount); Assert.IsTrue(cache.TryGetValue("1", out item)); cache.Add("2", 2); cache.Add("3", 3); cache.Add("4", 4); cache.Add("5", 5); Assert.AreEqual(5, cache.ItemCount); Assert.IsTrue(cache.TryGetValue("1", out item)); Assert.AreEqual(1, item); Assert.IsTrue(cache.TryGetValue("2", out item)); Assert.AreEqual(2, item); Assert.IsTrue(cache.TryGetValue("3", out item)); Assert.AreEqual(3, item); Assert.IsTrue(cache.TryGetValue("4", out item)); Assert.AreEqual(4, item); Assert.IsTrue(cache.TryGetValue("5", out item)); Assert.AreEqual(5, item); cache.Add("6",6); Assert.AreEqual(5, cache.ItemCount); Assert.IsFalse(cache.TryGetValue("1", out item)); Assert.IsTrue(cache.TryGetValue("6", out item)); Assert.IsTrue(cache.TryGetValue("3", out item)); cache.Add("7", 7); cache.Add("8", 8); cache.Add("9", 9); Assert.AreEqual(5, cache.ItemCount); Assert.IsFalse(cache.TryGetValue("1", out item)); Assert.IsFalse(cache.TryGetValue("2", out item)); Assert.IsTrue(cache.TryGetValue("3", out item)); Assert.AreEqual(3, item); Assert.IsFalse(cache.TryGetValue("4", out item)); Assert.IsFalse(cache.TryGetValue("5", out item)); Assert.IsTrue(cache.TryGetValue("6", out item)); Assert.AreEqual(6, item); Assert.IsTrue(cache.TryGetValue("7", out item)); Assert.AreEqual(7, item); Assert.IsTrue(cache.TryGetValue("8", out item)); Assert.AreEqual(8, item); Assert.IsTrue(cache.TryGetValue("9", out item)); Assert.AreEqual(9, item); Assert.IsTrue(cache.TryGetValue("7", out item)); Assert.AreEqual(7, item); cache.CacheSize = 2; Assert.AreEqual(2, cache.ItemCount); Assert.IsFalse(cache.TryGetValue("1", out item)); Assert.IsFalse(cache.TryGetValue("2", out item)); Assert.IsFalse(cache.TryGetValue("3", out item)); Assert.IsFalse(cache.TryGetValue("4", out item)); Assert.IsFalse(cache.TryGetValue("5", out item)); Assert.IsFalse(cache.TryGetValue("6", out item)); Assert.IsTrue(cache.TryGetValue("7", out item)); Assert.IsFalse(cache.TryGetValue("8", out item)); Assert.IsTrue(cache.TryGetValue("9", out item)); cache.ClearCache(); Assert.AreEqual(0,cache.ItemCount); Assert.IsFalse(cache.TryGetValue("1", out item)); Assert.IsFalse(cache.TryGetValue("2", out item)); Assert.IsFalse(cache.TryGetValue("3", out item)); Assert.IsFalse(cache.TryGetValue("4", out item)); Assert.IsFalse(cache.TryGetValue("5", out item)); Assert.IsFalse(cache.TryGetValue("6", out item)); Assert.IsFalse(cache.TryGetValue("7", out item)); Assert.IsFalse(cache.TryGetValue("8", out item)); Assert.IsFalse(cache.TryGetValue("9", out item)); }
public void ThreadedRemoveTest() { SmartCache <int> cache = new SmartCache <int>(50); Thread[] threads = new Thread[50]; int exceptionCount = 0; object exceptionCountLock = new object(); ParameterizedThreadStart t = o => { try { Random random = new Random(); for (int i1 = 0; i1 < 10000; i1++) { int x; if (!cache.TryGetValue(i1.ToString(), out x)) { if (random.Next() % 4 != 0) { cache.Remove(i1.ToString()); } else { cache.Add(i1.ToString(), i1); } } else { if (random.Next() % 3 == 0) { cache.Remove(i1.ToString()); } } } } catch { lock (exceptionCountLock) exceptionCount++; throw; } }; for (int i = 0; i < 50; i++) { threads[i] = new Thread(t); threads[i].Start(); } for (int i = 0; i < 50; i++) { threads[i].Join(); } Assert.AreEqual(50, cache.ItemCount); Assert.AreEqual(0, exceptionCount); }