/// <summary> /// 哈希 /// </summary> private void ShowHash() { var cacheKey = "HSKey1"; RedisDb.HSet(cacheKey, Guid.Parse("a590cc18-9bed-46cc-b856-781b27a96798").ToString(), new AccountInfo { Name = "Test1", Age = 18 }); RedisDb.HSet(cacheKey, Guid.Parse("359bbaec-a900-4a28-8f44-39dfd9fb54fa").ToString(), new AccountInfo { Name = "Test2", Age = 20 }); RedisDb.HSet(cacheKey, Guid.Parse("7e4645de-059f-4c10-be06-6a8751032d71").ToString(), new AccountInfo { Name = "Test3", Age = 22 }); RedisDb.HSet(cacheKey, Guid.Parse("a590cc18-9bed-46cc-b856-781b27a96798").ToString(), new AccountInfo { Name = "Test4", Age = 24 }); WriteColorLine(RedisDb.HGet(cacheKey, Guid.Parse("7e4645de-059f-4c10-be06-6a8751032d71").ToString()), ConsoleColor.Green); var cacheKey2 = "HSInrKey1"; RedisDb.HSet(cacheKey2, 1.ToString(), 10); RedisDb.HSet(cacheKey2, 2.ToString(), 10); RedisDb.HSet(cacheKey2, 3.ToString(), 10); RedisDb.HIncrBy(cacheKey2, 2.ToString()); RedisDb.HIncrBy(cacheKey2, 5.ToString()); }
/// <summary> /// 单个缓存 /// </summary> /// <param name="id"></param> /// <returns></returns> private Person GetCache(int id) { var cacheKey = "PenetrationCache2"; var getCache = RedisDb.HGet(cacheKey, id.ToString()); if (getCache == null) { var dt = GetDbData(id); RedisDb.HSet(cacheKey, id.ToString(), dt); return(dt); } RedisDb.Expire(cacheKey, 10); return(Newtonsoft.Json.JsonConvert.DeserializeAnonymousType(getCache, new Person())); }
private Person GetCache(int id) { if (_localCache.ContainsKey(id)) { return(_localCache[id]); } var getH = RedisDb.HGet(_cacheKey, id.ToString()); if (!string.IsNullOrWhiteSpace(getH)) { var model = JsonConvert.DeserializeAnonymousType(getH, new Person()); //_localCache.Add(id, model); return(model); } return(GetDbData(id)); }
/// <summary> /// 全表重建缓存导致无法缓存不存在的Key /// </summary> /// <param name="id"></param> /// <returns></returns> private Person GetCache2(int id) { var cacheKey = "PenetrationCache2"; var getCache = RedisDb.HGet(cacheKey, id.ToString()); if (getCache == null) { //全表重建缓存 var pinple = RedisDb.StartPipe(); foreach (var item in GetDbAll()) { pinple.HSet(cacheKey, item.Id.ToString(), item); } pinple.EndPipe(); getCache = RedisDb.HGet(cacheKey, id.ToString()); } RedisDb.Expire(cacheKey, 10); if (getCache == null) { return(GetDbData(id)); } return(Newtonsoft.Json.JsonConvert.DeserializeAnonymousType(getCache, new Person())); }