我在我的redis服务器中有这些密钥和它们的数据:
AdminIpoChangeEntity:0
AdminIpoChangeEntity:1
AdminIpoChangeEntity:2
AdminIpoChangeEntity:3我只想把AdminIpoChangeEntity传递给我的方法,我的方法应该返回四个记录。这是我的redis存储库:
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);
}我需要这个:
public async Task<OperationResult<IList<TEntity>>> GetAllAsync()
{
var value = await RedisDb.StringGetAsync(Key(nameof(TEntity)));
throw new NotImplementedException();
}我的实体名是AdminIpoChangeEntity
发布于 2022-03-04 12:44:44
我应该找到与我的值匹配的所有键,然后将它们传递到getstring以查找所有值,下面是我的最后代码:
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);
}https://stackoverflow.com/questions/71329501
复制相似问题