public void AddItemToQueue <T>(string queueId, T queueItem) { using (var Redis = RedisClientManager.GetClient()) { Redis.AddItemToList(RedisKeyFactory.QueueKey <T>(queueId), JsonConvert.SerializeObject(queueItem)); } }
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 bool IsActive <T>(string id) where T : IRedisModelBase { using (var Redis = RedisClientManager.GetReadOnlyClient()) { return(Redis.SortedSetContainsItem(RedisKeyFactory.ListAllKeys <T>(), GetKey <T>(id))); } }
public List <string> GetAllActiveModelIds <T>() where T : IRedisModelBase { using (var Redis = RedisClientManager.GetReadOnlyClient()) { return(Redis.GetRangeFromSortedSetDesc(RedisKeyFactory.ListAllKeys <T>(), 0, -1)); } }
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 RemoveItemFromQueue <T>(string queueId, T queueItem) { using (var Redis = RedisClientManager.GetClient()) { return(Redis.RemoveItemFromList(RedisKeyFactory.QueueKey <T>(queueId), JsonConvert.SerializeObject(queueItem))); } }
public void SetItemInQueue <T>(string queueId, int index, T queueItem) { using (var Redis = RedisClientManager.GetClient()) { Redis.SetItemInList(RedisKeyFactory.QueueKey <T>(queueId), index, JsonConvert.SerializeObject(queueItem)); } }
public int GetLengthOfQueue <T>(string queueId) { using (var Redis = RedisClientManager.GetReadOnlyClient()) { return(Redis.GetListCount(RedisKeyFactory.QueueKey <T>(queueId))); } }
public int GetSubModelCount <TModel>(string modelId) { using (var Redis = RedisClientManager.GetReadOnlyClient()) { return(Redis.GetHashCount(RedisKeyFactory.SubModelKey <TModel>(modelId))); } }
private void BuildIndexForDynamicElements <T, TCustomProperty>(string modelId, TCustomProperty customProperty, bool isRemoveIndex = false) where T : IRedisModelBase where TCustomProperty : IRedisModelBase, IRedisCustomProperty { if (!customProperty.IsQueriable) { return; } if (customProperty.Value != null) { if (customProperty.Value.GetType().IsEnumerableType()) { foreach (var v in customProperty.Value as IEnumerable) { DoIndexBySet <T>(RedisKeyFactory.ModelKey <T>(modelId), customProperty.Id, v.ToString(), isRemoveIndex); } } else if (customProperty.Value.GetType().IsValueType&& !customProperty.Value.GetType().Equals(typeof(bool))) { DoIndexBySortedSet <T>(RedisKeyFactory.ModelKey <T>(modelId), customProperty.Id, customProperty.Value, isRemoveIndex); } else { DoIndexBySet <T>(RedisKeyFactory.ModelKey <T>(modelId), customProperty.Id, customProperty.Value.ToString(), isRemoveIndex); } } }
public List <string> GetAllSubModelIds <TModel>(string modelId) where TModel : IRedisModelBase { using (var Redis = RedisClientManager.GetReadOnlyClient()) { return(Redis.GetHashKeys(RedisKeyFactory.SubModelKey <TModel>(modelId))); } }
public int GetAllCount <T>() where T : IRedisModelBase { using (var Redis = RedisClientManager.GetReadOnlyClient()) { return(Redis.Get <int>(RedisKeyFactory.ListAllNumKeys <T>())); } }
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()); } }
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))); } }
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 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))); } }
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)); }
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()); } }
public T DequeueItemFromList <T>(string queueId) { using (var Redis = RedisClientManager.GetClient()) { var result = Redis.DequeueItemFromList(RedisKeyFactory.QueueKey <T>(queueId)); if (result != null) { return(JsonConvert.DeserializeObject <T>(result)); } return(default(T)); } }
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); } }
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); } }
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); } } }
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)); } }
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)); } } }
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 <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); } }
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); } }
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); } }
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); } }
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)); } } } }