public virtual string Add <T>(T model) where T : IRedisModel { using (var Redis = RedisClientManager.GetClient()) { if (model == null) { return(null); } if (string.IsNullOrWhiteSpace(model.Id)) { model.Id = NextId <T>().ToString(); } if (Get <T>(model.Id) != null) { return(null); } string modelKey = GetKey <T>(model); model.ModuleName = CurrentMode; Redis.Set <T>(modelKey, model); Redis.AddItemToSortedSet(RedisKeyFactory.ListAllKeys <T>(), modelKey, model.CreateDateTime.Ticks); Redis.IncrementValue(RedisKeyFactory.ListAllNumKeys <T>()); Redis.IncrementValue(RedisKeyFactory.NextKey <T>()); BuildIndex <T>(model); return(model.Id); } }
public virtual void Delete <T>(T model, bool IsRemoveSubModel = true) where T : IRedisModelBase { using (var Redis = RedisClientManager.GetClient()) { if (model != null) { string modelKey = GetKey <T>(model); Redis.Remove(modelKey); Redis.RemoveItemFromSortedSet(RedisKeyFactory.ListAllKeys <T>(), modelKey); Redis.IncrementValueBy(RedisKeyFactory.ListAllNumKeys <T>(), -1); if (GetAllCount <T>() == 0) { Redis.Remove(RedisKeyFactory.ListAllNumKeys <T>()); } BuildIndex <T>(model, true); if (IsRemoveSubModel) { Redis.Remove(RedisKeyFactory.SubModelKey <T>(model.Id)); } } } }
public int GetAllCount <T>() where T : IRedisModelBase { using (var Redis = RedisClientManager.GetReadOnlyClient()) { return(Redis.Get <int>(RedisKeyFactory.ListAllNumKeys <T>())); } }