/// <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()); }
private void SetCache3(Person model) { lock (_lock3) { Task.Run(() => { Thread.Sleep(1 * 1000); //全表重建缓存 var cacheKey = $"BrokeBigKey:{SimpleHash(model.Id)}"; RedisDb.HSet(cacheKey, model.Id.ToString(), model); RedisDb.Expire(cacheKey, 1 + new Random().Next(0, 20) / 10); }); } }
/// <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 void SetCache2(Person model) { lock (_lock2) { var allData = GetDbAll(); Task.Run(() => { var cacheKey = "BrokeBigKey"; Thread.Sleep(1 * 1000); //全表重建缓存 RedisDb.HSet(cacheKey, model.Id.ToString(), model); RedisDb.Expire(cacheKey, 1); }); } }