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 List <string> GetAllActiveModelIds <T>() where T : IRedisModelBase { using (var Redis = RedisClientManager.GetReadOnlyClient()) { return(Redis.GetRangeFromSortedSetDesc(RedisKeyFactory.ListAllKeys <T>(), 0, -1)); } }
public bool IsActive <T>(string id) where T : IRedisModelBase { using (var Redis = RedisClientManager.GetReadOnlyClient()) { return(Redis.SortedSetContainsItem(RedisKeyFactory.ListAllKeys <T>(), GetKey <T>(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 void SetActive <T>(bool isActive, string id) where T : IRedisModel { using (var Redis = RedisClientManager.GetClient()) { if (isActive) { var model = Get <T>(id); if (model != null) { Redis.AddItemToSortedSet(RedisKeyFactory.ListAllKeys <T>(), GetKey <T>(id), model.CreateDateTime.Ticks); } } else { Redis.RemoveItemFromSortedSet(RedisKeyFactory.ListAllKeys <T>(), GetKey <T>(id)); } } }
public List <string> GetPagedModelIds <T>(int pageNum, int pageSize, string propertyName = "", bool isAsc = false) where T : IRedisModelBase { using (var Redis = RedisClientManager.GetReadOnlyClient()) { int start = (pageNum - 1) * pageSize; int end = pageNum * pageSize - 1; if (pageNum == 0) // get all { start = 0; end = -1; } if (string.IsNullOrWhiteSpace(propertyName)) { if (isAsc) { return(Redis.GetRangeFromSortedSet(RedisKeyFactory.ListAllKeys <T>(), start, end)); } else { return(Redis.GetRangeFromSortedSetDesc(RedisKeyFactory.ListAllKeys <T>(), start, end)); } } else { string queryKey = RedisKeyFactory.QueryKeyWithProperty <T>(propertyName); if (isAsc) { return(Redis.GetRangeFromSortedSet(queryKey, start, end)); } else { return(Redis.GetRangeFromSortedSetDesc(queryKey, start, end)); } } } }