public void 缓存壳()
        {
            //不加缓存的时候,要从数据库查询
            string str1 = "val";

            //1一般的缓存代码,如不封装还挺繁琐的
            var cacheValue = rds.Get("test1");

            if (!string.IsNullOrEmpty(cacheValue))
            {
                try
                {
                    var obj = JsonConvert.DeserializeObject(cacheValue);
                }
                catch
                {
                    //出错时删除key
                    rds.Del("test1");
                    throw;
                }
            }
            rds.Set("test1", JsonConvert.SerializeObject(str1), 10); //缓存10秒

            //2使用缓存壳效果同上,以下示例使用 string 和 hash 缓存数据
            var t1 = rds.CacheShell("test1", 10, () => str1);
            var t2 = rds.CacheShell("test", "1", 10, () => str1);
            var t3 = rds.CacheShell("test", new[] { "1", "2" }, 10, notCacheFields => new[] { ("1", str1), ("2", str1) });
Esempio n. 2
0
 /// <summary>
 /// 加缓存壳
 /// </summary>
 public static void CacheShell()
 {
     //普通kv
     var result1 = redis.CacheShell("key1", 10, () =>
     {
         return(DateTime.Now);
     });
     //hash数据
     var result2 = redis.CacheShell("key2", "time", 10, () =>
     {
         return(DateTime.Now);
     });
 }
Esempio n. 3
0
 public T TryGet <T>(string key, Func <T> createFn = null, TimeSpan?expiry = null) => _instance.CacheShell(key, (expiry.HasValue ? (int)expiry.Value.TotalSeconds : CacheShellTimeoutSeconds), createFn);