Beispiel #1
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));
                    }
                }
            }
        }
Beispiel #2
0
 public int GetLengthOfQueue <T>(string queueId)
 {
     using (var Redis = RedisClientManager.GetReadOnlyClient())
     {
         return(Redis.GetListCount(RedisKeyFactory.QueueKey <T>(queueId)));
     }
 }
Beispiel #3
0
 public int RemoveItemFromQueue <T>(string queueId, T queueItem)
 {
     using (var Redis = RedisClientManager.GetClient())
     {
         return(Redis.RemoveItemFromList(RedisKeyFactory.QueueKey <T>(queueId), JsonConvert.SerializeObject(queueItem)));
     }
 }
Beispiel #4
0
 public void AddItemToQueue <T>(string queueId, T queueItem)
 {
     using (var Redis = RedisClientManager.GetClient())
     {
         Redis.AddItemToList(RedisKeyFactory.QueueKey <T>(queueId), JsonConvert.SerializeObject(queueItem));
     }
 }
Beispiel #5
0
 public bool IsActive <T>(string id) where T : IRedisModelBase
 {
     using (var Redis = RedisClientManager.GetReadOnlyClient())
     {
         return(Redis.SortedSetContainsItem(RedisKeyFactory.ListAllKeys <T>(), GetKey <T>(id)));
     }
 }
Beispiel #6
0
 public List <string> FuzzyFindIdsByCondition <T>(string property, string valuePattern)
     where T : IRedisModelBase
 {
     using (var Redis = RedisClientManager.GetReadOnlyClient())
     {
         return(Redis.GetUnionFromSets(KeyFuzzyFind(RedisKeyFactory.QueryKeyWithPropertyAndValue <T>(property, valuePattern)).ToArray()).ToList());
     }
 }
Beispiel #7
0
 public List <string> GetAllSubModelIds <TModel>(string modelId)
     where TModel : IRedisModelBase
 {
     using (var Redis = RedisClientManager.GetReadOnlyClient())
     {
         return(Redis.GetHashKeys(RedisKeyFactory.SubModelKey <TModel>(modelId)));
     }
 }
Beispiel #8
0
 public int GetAllCount <T>()
     where T : IRedisModelBase
 {
     using (var Redis = RedisClientManager.GetReadOnlyClient())
     {
         return(Redis.Get <int>(RedisKeyFactory.ListAllNumKeys <T>()));
     }
 }
Beispiel #9
0
 public bool ExistSubModel <TModel, TSubModel>(string modelId, string subModelId)
     where TModel : IRedisModelBase
     where TSubModel : IRedisModelBase
 {
     using (var Redis = RedisClientManager.GetReadOnlyClient())
     {
         return(Redis.HashContainsEntry(RedisKeyFactory.SubModelKey <TModel>(modelId), GetKey <TSubModel>(subModelId)));
     }
 }
Beispiel #10
0
 public virtual bool SetSubModel <TModel, TSubModel>(string modelId, TSubModel subModel)
     where TModel : IRedisModelBase
     where TSubModel : IRedisModelBase
 {
     using (var Redis = RedisClientManager.GetClient())
     {
         return(Redis.SetEntryInHash(RedisKeyFactory.SubModelKey <TModel>(modelId), GetKey <TSubModel>(subModel), JsonConvert.SerializeObject(subModel)));
     }
 }
Beispiel #11
0
 public virtual bool DeleteSubModel <TModel, TSubModel>(string modelId, string subModelId)
     where TModel : IRedisModelBase
     where TSubModel : IRedisModelBase
 {
     using (var Redis = RedisClientManager.GetClient())
     {
         return(Redis.RemoveEntryFromHash(RedisKeyFactory.SubModelKey <TModel>(modelId), GetKey <TSubModel>(subModelId)));
     }
 }
        public static List <T> IdsToValues <T>(this List <string> ids, bool needKeyFormat = false)
        {
            if (needKeyFormat)
            {
                for (int i = 0; i < ids.Count; i++)
                {
                    ids[i] = RedisKeyFactory.ModelKey <T>(ids[i]);
                }
            }

            return(GetValues <T>(ids));
        }
Beispiel #13
0
        public T RetrieveItemFromQueue <T>(string queueId)
        {
            using (var Redis = RedisClientManager.GetClient())
            {
                var result = Redis.BlockingDequeueItemFromList(RedisKeyFactory.QueueKey <T>(queueId), new TimeSpan(0));

                if (result != null)
                {
                    return(JsonConvert.DeserializeObject <T>(result));
                }

                return(default(T));
            }
        }
Beispiel #14
0
        public List <string> FindIdsByConditions <T>(List <KeyValuePair <string, string> > conditions)
            where T : IRedisModelBase
        {
            List <string> conditionSets = new List <string>();

            foreach (var c in conditions)
            {
                conditionSets.Add(RedisKeyFactory.QueryKeyWithPropertyAndValue <T>(c.Key, c.Value));
            }
            using (var Redis = RedisClientManager.GetReadOnlyClient())
            {
                return(Redis.GetIntersectFromSets(conditionSets.ToArray()).ToList());
            }
        }
Beispiel #15
0
        public List <string> GetFilteredIdsByPropertyFromSets <T>(List <string> ids, string propertyName)
            where T : IRedisModelBase
        {
            using (var Redis = RedisClientManager.GetClient())
            {
                string tempSetKey = string.Format("TempSet:{0}", Guid.NewGuid().ToString());

                Redis.AddRangeToSet(tempSetKey, ids);

                var filteredIds = Redis.GetDifferencesFromSet(tempSetKey, RedisKeyFactory.QueryKeyWithProperty <T>(propertyName)).ToList();
                Redis.Remove(tempSetKey);

                return(filteredIds);
            }
        }
Beispiel #16
0
        public int NextId <T>()
            where T : IRedisModelBase
        {
            using (var Redis = RedisClientManager.GetClient())
            {
                int id = Redis.Get <int>(RedisKeyFactory.NextKey <T>()) + 1;

                while (IsExist <T>(id.ToString()))
                {
                    id++;
                    Redis.IncrementValue(RedisKeyFactory.NextKey <T>());
                }

                return(id);
            }
        }
Beispiel #17
0
        public void DoIndexBySet <T>(string idVal, string propertyName, string value, bool isRemoveIndex = false)
            where T : IRedisModelBase
        {
            string queryKey = RedisKeyFactory.QueryKeyWithPropertyAndValue <T>(propertyName, value);

            using (var Redis = RedisClientManager.GetClient())
            {
                if (isRemoveIndex)
                {
                    Redis.RemoveItemFromSet(queryKey, idVal);
                }
                else
                {
                    Redis.AddItemToSet(queryKey, idVal);
                }
            }
        }
Beispiel #18
0
 public TSubModel GetSubModel <TModel, TSubModel>(string modelId, string subModelId, bool isFullSubModelKey = false)
     where TModel : IRedisModelBase
     where TSubModel : IRedisModelBase
 {
     using (var Redis = RedisClientManager.GetReadOnlyClient())
     {
         var subModelJSONString = Redis.GetValueFromHash(RedisKeyFactory.SubModelKey <TModel>(modelId), isFullSubModelKey ? subModelId : GetKey <TSubModel>(subModelId));
         if (string.IsNullOrWhiteSpace(subModelJSONString))
         {
             return(default(TSubModel));
         }
         else
         {
             return(JsonConvert.DeserializeObject <TSubModel>(subModelJSONString));
         }
     }
 }
Beispiel #19
0
        public List <string> FindIdsByValueRange <T>(string propertyName, DateTime?start, DateTime?end)
            where T : IRedisModelBase
        {
            if (!start.HasValue)
            {
                start = DateTime.MinValue;
            }

            if (!end.HasValue)
            {
                end = DateTime.MaxValue;
            }
            using (var Redis = RedisClientManager.GetReadOnlyClient())
            {
                return(Redis.GetRangeFromSortedSetByLowestScore(RedisKeyFactory.QueryKeyWithProperty <T>(propertyName), start.GetValueOrDefault().Ticks, end.GetValueOrDefault().Ticks));
            }
        }
Beispiel #20
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));
         }
     }
 }
Beispiel #21
0
        public List <T> GetAllItemsFromQueue <T>(string queueId)
        {
            using (var Redis = RedisClientManager.GetReadOnlyClient())
            {
                var      result = Redis.GetAllItemsFromList(RedisKeyFactory.QueueKey <T>(queueId));
                List <T> items  = new List <T>();

                if (result != null)
                {
                    foreach (var json in result)
                    {
                        items.Add(JsonConvert.DeserializeObject <T>(json));
                    }
                    return(items);
                }

                return(items);
            }
        }
Beispiel #22
0
        public List <string> GetIntersectIdsByPropertyFromSets <T>(List <string> ids, params string[] propertyName)
            where T : IRedisModelBase
        {
            using (var Redis = RedisClientManager.GetClient())
            {
                string tempSetKey = string.Format("TempSet:{0}", Guid.NewGuid().ToString());

                Redis.AddRangeToSet(tempSetKey, ids);
                List <string> keys = new List <string>();
                foreach (var item in propertyName)
                {
                    keys.Add(RedisKeyFactory.QueryKeyWithProperty <T>(item));
                }
                keys.Add(tempSetKey);
                var filteredIds = Redis.GetIntersectFromSets(keys.ToArray()).ToList();
                Redis.Remove(tempSetKey);

                return(filteredIds);
            }
        }
Beispiel #23
0
        public List <T> GetValuesByIds <T>(List <string> ids, bool needKeyFormat = false)
        {
            using (var Redis = RedisClientManager.GetReadOnlyClient())
            {
                List <T> results = new List <T>();
                if (needKeyFormat)
                {
                    for (int i = 0; i < ids.Count; i++)
                    {
                        ids[i] = RedisKeyFactory.ModelKey <T>(ids[i]);
                    }
                    results = Redis.GetValues <T>(ids);
                }
                else
                {
                    results = Redis.GetValues <T>(ids);
                }

                return(results == null ? new List <T>() : results);
            }
        }
Beispiel #24
0
        public List <TSubModel> GetAllSubModelsByType <TModel, TSubModel>(string modelId)
            where TModel : IRedisModelBase
            where TSubModel : IRedisModelBase
        {
            List <TSubModel> subModels = new List <TSubModel>();
            var subModelIds            = GetAllSubModelIdsByType <TModel, TSubModel>(modelId).ToArray();

            using (var Redis = RedisClientManager.GetReadOnlyClient())
            {
                List <string> values = Redis.GetValuesFromHash(RedisKeyFactory.SubModelKey <TModel>(modelId), subModelIds);
                foreach (var v in values)
                {
                    if (!string.IsNullOrWhiteSpace(v))
                    {
                        subModels.Add(JsonConvert.DeserializeObject <TSubModel>(v));
                    }
                }

                return(subModels);
            }
        }
Beispiel #25
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));
                    }
                }
            }
        }
Beispiel #26
0
        public List <string> GetSortedIdsByProperty <T>(List <string> ids, string propertyName, int startNum = 0, int num = 0, bool isDesc = true)
            where T : IRedisModelBase
        {
            using (var Redis = RedisClientManager.GetClient())
            {
                string tempKey = string.Format("Temp:{0}", Guid.NewGuid().ToString());

                Redis.AddRangeToSortedSet(tempKey, ids, 0.0);

                string intoSetKey = string.Format("OUT:{0}", Guid.NewGuid().ToString());
                Redis.StoreIntersectFromSortedSets(intoSetKey, tempKey, RedisKeyFactory.QueryKeyWithProperty <T>(propertyName));
                Redis.Remove(tempKey);

                if (startNum > 0)
                {
                    startNum--;
                }
                else
                {
                    startNum = 0;
                }

                int endNum = -1;
                if (num == 0) // get all
                {
                    startNum = 0;
                }
                else
                {
                    endNum = startNum + num - 1;
                }
                var sortedIds = isDesc ? Redis.GetRangeFromSortedSetDesc(intoSetKey, startNum, endNum) : Redis.GetRangeFromSortedSet(intoSetKey, startNum, endNum);
                Redis.Remove(intoSetKey);

                return(sortedIds);
            }
        }
Beispiel #27
0
        public void DoIndexBySortedSet <T>(string idVal, string propertyName, object value, bool isRemoveIndex = false)
            where T : IRedisModelBase
        {
            string queryKey = RedisKeyFactory.QueryKeyWithProperty <T>(propertyName);

            using (var Redis = RedisClientManager.GetClient())
            {
                if (isRemoveIndex)
                {
                    Redis.RemoveItemFromSortedSet(queryKey, idVal);
                }
                else
                {
                    if (value.GetType().Equals(typeof(DateTime)))
                    {
                        Redis.AddItemToSortedSet(queryKey, idVal, ((DateTime)value).Ticks);
                    }
                    else
                    {
                        Redis.AddItemToSortedSet(queryKey, idVal, Convert.ToDouble(value));
                    }
                }
            }
        }
Beispiel #28
0
 protected string GetKey <T>(T model) where T : IRedisModelBase
 {
     return(RedisKeyFactory.ModelKey <T>(model.Id));
 }
Beispiel #29
0
 private string GetKey <T>(string id)
 {
     return(RedisKeyFactory.ModelKey <T>(id));
 }