I have these keys in my redis server with their data :
AdminIpoChangeEntity:0
AdminIpoChangeEntity:1
AdminIpoChangeEntity:2
AdminIpoChangeEntity:3
I want to pass just AdminIpoChangeEntity
to my method and my method should return four records .
Here is my redis repository :
private IDatabase RedisDb => _connection.Value.GetDatabase();
//
private string Key(TEntity entity) => $"{typeof(TEntity).Name}:{entity.Id}";
private string Key(string id) => $"{typeof(TEntity).Name}:{id}";
public async Task<OperationResult> AddOrUpdateAsync(TEntity entity)
{
var val = System.Text.Json.JsonSerializer.Serialize(entity);
var res = await RedisDb.StringSetAsync(Key(entity), val);
return OperationResult<TEntity>.Succeed();
}
public async Task<OperationResult<TEntity>> GetByIdAsync(string id)
{
var value = await RedisDb.StringGetAsync(Key(id));
if (value.HasValue)
{
var result = System.Text.Json.JsonSerializer.Deserialize<TEntity>(value.ToString());
return OperationResult<TEntity>.Succeed(result);
}
return OperationResult<TEntity>.Succeed(null);
}
And I need this one :
public async Task<OperationResult<IList<TEntity>>> GetAllAsync()
{
var value = await RedisDb.StringGetAsync(Key(nameof(TEntity)));
throw new NotImplementedException();
}
My entity name is AdminIpoChangeEntity
CodePudding user response:
I should find all keys that match with my value and after that pass them into getstring to find all values ,here is my final code :
public async Task<OperationResult<IList<TEntity>>> GetAllAsync()
{
EndPoint endPoint = _connection.Value.GetEndPoints().First();
RedisKey[] keys = _connection.Value.GetServer(endPoint).Keys(pattern: typeof(TEntity).Name "*").ToArray();
var value = await RedisDb.StringGetAsync(keys);
if (value.Count()>0)
{
var result= value.Select(d => System.Text.Json.JsonSerializer.Deserialize<TEntity>(d)).ToArray();
// var result = System.Text.Json.JsonSerializer.Deserialize<IList<TEntity>>(value.ToString());
return OperationResult<IList<TEntity>>.Succeed(result);
}
return OperationResult<IList<TEntity>>.Succeed(null);
}