/// <summary> /// Adds branch. /// </summary> /// <param name="branch"></param> public void AddBranch(IRedisBranch <T> branch) { if (string.IsNullOrEmpty(branch.GetBranchId())) { throw new ArgumentException($"BranchId must be set. branch.SetBranchId(string branchId)"); } if (branch.GetEntityType() == default) { throw new ArgumentException($"EntityType must be set. branch.SetEntityType(RedisEntity entity);"); } _branches.Add(branch); }
/// <summary> /// Get entity counts in the branch. /// </summary> /// <param name="branchId">Id to identify branch.</param> /// <param name="from">Score from.</param> /// <param name="to">Score to.</param> /// <param name="groups">Groups parameters.</param> /// <returns></returns> public async Task <long> CountBySortedBranchAsync(string branchId, long from, long to, params string[] groups) { IRedisBranch <T> branch = _branches.Find(b => b.GetBranchId() == branchId); if (branch == default) { throw new KeyNotFoundException($"branchId not found: {branchId}. Possible branches: {_branches.Select(i => i.GetBranchKey())}"); } var branchKey = branch.GetBranchKey(groups); return(await _redisDatabase.SortedSetLengthAsync(branchKey, from, to)); }
/// <summary> /// Get entity by id. /// </summary> /// <param name="id"></param> /// <returns></returns> public virtual async Task <T> GetByIdAsync(string id) { IRedisBranch <T> dataBranch = _branches.Find(i => i.GetBranchId() == BRANCH_DATA); if (dataBranch == default) { throw new KeyNotFoundException("Data Branch not found."); } string redisKey = dataBranch.GetBranchKey().Replace("{propertyValue}", id); HashEntry[] hashSet = (await _redisDatabase.HashGetAllAsync(redisKey)); return(hashSet.Length == 0 ? null : hashSet.ConvertFromHashEntryList <T>()); }
/// <summary> /// Get all entity in sorted branch by branchId. /// </summary> /// <param name="branchId">Id to identify branch.</param> /// <param name="from">Score from.</param> /// <param name="to">Score to.</param> /// <param name="groups">Groups parameters.</param> /// <returns></returns> public virtual async Task <List <T> > GetBySortedBranchAsync(string branchId, long from, long to, params string[] groups) { IRedisBranch <T> branch = _branches.Find(b => b.GetBranchId() == branchId); if (branch == default) { throw new KeyNotFoundException($"branchId not found: {branchId}. Possible branches: {_branches.Select(i => i.GetBranchKey())}"); } string branchKey = branch.GetBranchKey(groups); List <T> resultList = new List <T>(); RedisValue[] ids = await _redisDatabase.SortedSetRangeByScoreAsync(branchKey, from, to); foreach (RedisValue id in ids) { T item = (await _redisDatabase.HashGetAllAsync($"{typeof(T).Name}:data:{id}")).ConvertFromHashEntryList <T>(); resultList.Add(item); } return(resultList); }