public void TestIncrement() { Parallel.For(0, 10, i => { var cacheMgr = CacheManagerFactory.CreateManager("redis") as IDistributedCacheManager; var inc = cacheMgr.TryIncrement("inc1", () => 0); Console.WriteLine(inc); }); }
public Products TestCache2() { var cacheMgr = CacheManagerFactory.CreateManager(); using (var db = new DbContext()) { return(GetProduct(() => db.Products.FirstOrDefault())); } }
public override void Add(string connectionId, IClientProxy handler) { var cacheMgr = CacheManagerFactory.CreateManager(); //在redis缓存里存放连接标识对应的aliveKey,即服务标识,以方便后面查找 cacheMgr.Add(prefix + connectionId, aliveKey, new RelativeTime(TimeSpan.FromDays(5))); base.Add(connectionId, handler); }
/// <summary> /// 将客户端连接标识与用户标识进行关联。 /// </summary> /// <param name="connectionId">客户端连接标识。></param> /// <param name="identity">用户标识。</param> public virtual void Add(string connectionId, T identity) { var section = ConfigurationUnity.GetSection <CachingConfigurationSection>(); var cacheMgr = CacheManagerFactory.CreateManager(); cacheMgr.Add(prefix1 + connectionId, identity, new RelativeTime(TimeSpan.FromDays(5))); cacheMgr.Add(prefix2 + identity, connectionId, new RelativeTime(TimeSpan.FromDays(5))); }
public void TestExpired() { var cacheMgr = CacheManagerFactory.CreateManager("redis"); var value = cacheMgr.TryGet("test3", () => 100, () => new RelativeTime(TimeSpan.FromSeconds(2))); Assert.AreEqual(true, cacheMgr.Contains("test3")); Thread.Sleep(3000); Assert.AreEqual(false, cacheMgr.Contains("test3")); }
public void TestDecrement() { Parallel.For(0, 10, i => { var cacheMgr = CacheManagerFactory.CreateManager("redis") as IDistributedCacheManager; var dec = cacheMgr.TryDecrement("dec1", () => 100); Console.WriteLine(dec); }); }
public void TestTryAdd() { var cache = CacheManagerFactory.CreateManager(); var student = cache.TryGet <Student>("s0", () => new Student { Name = "fireasy", Address = "aa" }); Assert.IsNotNull(student); Assert.AreEqual("fireasy", student.Name); }
public void TestAdvanceDelay() { var cacheMgr = CacheManagerFactory.CreateManager("redis"); var value = cacheMgr.TryGet("test1", () => 100, () => new RelativeTime(TimeSpan.FromSeconds(1))); Thread.Sleep(1100); value = cacheMgr.TryGet("test1", () => 100, () => new RelativeTime(TimeSpan.FromSeconds(1))); Thread.Sleep(720); value = cacheMgr.TryGet("test1", () => 100, () => new RelativeTime(TimeSpan.FromSeconds(1))); }
/// <summary> /// 通过客户端连接标识查找用户标识。 /// </summary> /// <param name="connectionId">客户端连接标识。></param> /// <returns></returns> public T FindIdentity(string connectionId) { var cacheMgr = CacheManagerFactory.CreateManager(); if (cacheMgr.TryGet(prefix1 + connectionId, out T value)) { return(value); } return(default);
public void TestString() { var cacheMgr = CacheManagerFactory.CreateManager("redis"); var str = "fireasy"; cacheMgr.Add("tt1", str); str = cacheMgr.Get <string>("tt1"); Assert.AreEqual("fireasy", str); }
/// <summary> /// 通过客户端连接标识查找用户标识。 /// </summary> /// <param name="connectionId">客户端连接标识。></param> /// <returns></returns> public virtual T FindIdentity(string connectionId) { var cacheMgr = CacheManagerFactory.CreateManager(); var hashSet = cacheMgr.GetHashSet <string, T>(WS_SESSION_KEY); if (hashSet.TryGet(connectionId, out T value)) { return(value); } return(default);
/// <summary> /// 移除指定的客户端连接标识。 /// </summary> /// <param name="connectionId">客户端连接标识。></param> public void Remove(string connectionId) { var cacheMgr = CacheManagerFactory.CreateManager(); var identity = cacheMgr.Get <T>(prefix1 + connectionId); if (identity != null) { cacheMgr.Remove(prefix1 + connectionId); cacheMgr.Remove(prefix2 + identity); } }
public void TestHashset() { var cacheMgr = CacheManagerFactory.CreateManager("redis"); var set = cacheMgr.GetHashSet <string, Test1>("bee"); var a1 = set.TryGet("1", () => new Test1 { B = "aaaa1" }); var a2 = set.TryGet("2", () => new Test1 { B = "bbbb1" }); }
/// <summary> /// 移除指定的客户端连接标识。 /// </summary> /// <param name="connectionId">客户端连接标识。></param> public virtual void Remove(string connectionId) { var cacheMgr = CacheManagerFactory.CreateManager(); var hashSet1 = cacheMgr.GetHashSet <string, T>(WS_SESSION_KEY); var hashSet2 = cacheMgr.GetHashSet <string, string>(WS_IDENTITY_KEY); if (hashSet1.TryGet(connectionId, out T identity)) { hashSet1.Remove(connectionId); hashSet2.Remove(identity.ToString()); } }
/// <summary> /// 判断 <paramref name="code"/> 是否和缓存里一致。 /// </summary> /// <param name="key">验证码的key。</param> /// <param name="code">验证码。</param> /// <returns></returns> public static bool Validate(string key, string code) { var cacheMgr = CacheManagerFactory.CreateManager(); var vcode = string.Empty; if (cacheMgr.TryGet("Validate_" + key, out vcode)) { return(code.Equals(vcode, StringComparison.OrdinalIgnoreCase)); } return(false); }
public void TestBytes() { var cacheMgr = CacheManagerFactory.CreateManager("redis"); var test = new Test1 { A = new byte[] { 45, 33, 45, 23, 122, 178, 213 }, B = "fireasy" }; cacheMgr.Add("tt2", test); test = cacheMgr.Get <Test1>("tt2"); Assert.AreEqual("fireasy", test.B); }
/// <summary> /// 获取类型所关联的缓存键列表。 /// </summary> /// <param name="type"></param> /// <returns></returns> public static List <string> GetCacheKeys(Type type) { var cacheMgr = CacheManagerFactory.CreateManager(); var hashSet = cacheMgr.GetHashSet <string, List <string> >(ROOT_KEY); if (hashSet.TryGet(type.Name, out List <string> keys)) { return(keys); } return(null); }
internal static void Bind(IServiceCollection services, IConfiguration configuration) { ConfigurationUnity.Bind <LoggingConfigurationSection>(configuration); ConfigurationUnity.Bind <CachingConfigurationSection>(configuration); ConfigurationUnity.Bind <ContainerConfigurationSection>(configuration); if (services != null) { services.AddSingleton(typeof(ILogger), s => LoggerFactory.CreateLogger()); services.AddSingleton(typeof(ICacheManager), s => CacheManagerFactory.CreateManager()); } }
public void TestParallel() { var cacheMgr = CacheManagerFactory.CreateManager("redis"); int Get() { Console.WriteLine("get"); return(100); } Parallel.For(0, 3, (i, s) => Console.WriteLine(cacheMgr.TryGet("test", () => Get(), () => new RelativeTime(TimeSpan.FromSeconds(5))))); }
public void TestAdd() { var cache = CacheManagerFactory.CreateManager(); cache.Add <Student>("s7", new Student { Name = "fireasy", Address = "aa" }); var student = cache.Get <Student>("s7"); Assert.IsNotNull(student); Assert.AreEqual("fireasy", student.Name); Assert.AreEqual(true, cache.Contains <Student1>("s7")); }
/// <summary> /// 从路径的多个配置文件中读取容器。 /// </summary> /// <param name="path">存放路径。</param> /// <param name="pattern">配置文件的通配符。</param> /// <returns></returns> public static Container GetContainer(string path, string pattern) { var cacheMgr = CacheManagerFactory.CreateManager(); return(cacheMgr.TryGet <Container>(path + "/" + pattern, () => { var container = new Container(); #if !SILVERLIGHT InitPathContainer(path, pattern, container); #endif return container; }, () => NeverExpired.Instance)); }
public void TestContains() { var cacheMgr = CacheManagerFactory.CreateManager("redis"); var value = cacheMgr.TryGet("test1", () => 100); var value1 = cacheMgr.TryGet("test3", () => 100); var value2 = cacheMgr.TryGet("test4", () => 100); Assert.AreEqual(true, cacheMgr.Contains("test1")); Assert.AreEqual(false, cacheMgr.Contains("test2")); var keys = cacheMgr.GetKeys("test*"); Console.WriteLine(keys.Count()); }
private Products GetProduct(Func <Products> factory) { var cacheMgr = CacheManagerFactory.CreateManager(); if (cacheMgr.Contains("p")) { return((Products)cacheMgr.Get("p")); } var product = factory(); cacheMgr.Add("p", product); return(product); }
public void TestClear() { var cacheMgr = CacheManagerFactory.CreateManager("redis"); cacheMgr.Add("test4", 100); cacheMgr.Add("test5", 100); cacheMgr.Add("test6", 100); cacheMgr.Clear(); Assert.AreEqual(false, cacheMgr.Contains("test4")); Assert.AreEqual(false, cacheMgr.Contains("test5")); Assert.AreEqual(false, cacheMgr.Contains("test6")); }
private int GetRecoredCount(CommandContext context) { ICacheManager cacheManager; if (Expiration != null && (cacheManager = CacheManagerFactory.CreateManager()) != null) { var key = context.Command.Output(); return(cacheManager.Contains <int>(key) ? cacheManager.Get <int>(key) : cacheManager.Add(key, GetRecordCountFromDatabase(context), new RelativeTime((TimeSpan)Expiration))); } return(GetRecordCountFromDatabase(context)); }
/// <summary> /// 尝试通过表达式获取执行后的结果缓存。 /// </summary> /// <typeparam name="T"></typeparam> /// <param name="expression"></param> /// <param name="func"></param> /// <returns></returns> internal static T TryGet <T>(Expression expression, Func <T> func) { var section = ConfigurationUnity.GetSection <TranslatorConfigurationSection>(); var option = section == null ? TranslateOptions.Default : section.Options; //没有开启数据缓存 if (!option.DataCacheEnabled) { return(func()); } var cacheMgr = CacheManagerFactory.CreateManager(); if (cacheMgr == null) { return(func()); } var cacheKey = ExpressionKeyGenerator.GetKey(expression, "Exec"); Reference(cacheKey, expression); var segment = SegmentFinder.Find(expression); var pager = segment as DataPager; var cacheFunc = new Func <CacheItem <T> >(() => { var data = func(); var total = 0; if (pager != null) { total = pager.RecordCount; } return(new CacheItem <T> { Data = data, Total = total }); }); var cacheItem = cacheMgr.TryGet(cacheKey, cacheFunc, () => new RelativeTime(TimeSpan.FromSeconds(option.DataCacheExpired))); if (pager != null) { pager.RecordCount = cacheItem.Total; } return(cacheItem.Data); }
/// <summary> /// 通过用户标识查找户端连接标识。 /// </summary> /// <param name="identity">用户标识。</param> /// <returns></returns> public string FindConnection(T identity) { if (identity == null) { return(string.Empty); } var cacheMgr = CacheManagerFactory.CreateManager(); if (cacheMgr.TryGet(prefix2 + identity, out string key)) { return(key); } return(string.Empty); }
public Products TestCache1() { var cacheMgr = CacheManagerFactory.CreateManager(); using (var db = new DbContext()) { if (cacheMgr.Contains("p")) { return((Products)cacheMgr.Get("p")); } var product = db.Products.FirstOrDefault(); cacheMgr.Add("p", product); return(product); } }
/// <summary> /// 移除实体类型对应的缓存键。 /// </summary> /// <param name="entityType"></param> private static void RemoveCacheKeys(Type entityType) { var keys = CacheKeyManager.GetCacheKeys(entityType); if (keys == null) { return; } var cacheMgr = CacheManagerFactory.CreateManager(); foreach (var key in keys) { cacheMgr.Remove(key); } }
public async Task TestContains() { var cacheMgr = CacheManagerFactory.CreateManager("redis"); var value = await cacheMgr.TryGetAsync("test1", () => Task.FromResult(100)); var value1 = await cacheMgr.TryGetAsync("test3", () => Task.FromResult(100)); Thread.Sleep(10000); var value2 = await cacheMgr.TryGetAsync("test4", () => Task.FromResult(100)); //Assert.AreEqual(true, await cacheMgr.ContainsAsync("test1")); //Assert.AreEqual(false, await cacheMgr.ContainsAsync("test2")); //var keys = cacheMgr.GetKeys("test*"); //Console.WriteLine(keys.Count()); }