Exemple #1
0
        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);
            }
        }
Exemple #2
0
 public List <string> GetAllActiveModelIds <T>() where T : IRedisModelBase
 {
     using (var Redis = RedisClientManager.GetReadOnlyClient())
     {
         return(Redis.GetRangeFromSortedSetDesc(RedisKeyFactory.ListAllKeys <T>(), 0, -1));
     }
 }
Exemple #3
0
 public bool IsActive <T>(string id) where T : IRedisModelBase
 {
     using (var Redis = RedisClientManager.GetReadOnlyClient())
     {
         return(Redis.SortedSetContainsItem(RedisKeyFactory.ListAllKeys <T>(), GetKey <T>(id)));
     }
 }
Exemple #4
0
        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));
                    }
                }
            }
        }
Exemple #5
0
 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));
         }
     }
 }
Exemple #6
0
        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));
                    }
                }
            }
        }