使用.net core中的部分密钥获取redis中的数据
Get data in redis with part of a key in .net core
我的 redis 服务器中有这些密钥及其数据:
AdminIpoChangeEntity:0
AdminIpoChangeEntity:1
AdminIpoChangeEntity:2
AdminIpoChangeEntity:3
我只想将 AdminIpoChangeEntity
传递给我的方法,我的方法应该 return 四个记录。
这是我的 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
我应该找到所有与我的值匹配的键,然后将它们传递给 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);
}
我的 redis 服务器中有这些密钥及其数据:
AdminIpoChangeEntity:0
AdminIpoChangeEntity:1
AdminIpoChangeEntity:2
AdminIpoChangeEntity:3
我只想将 AdminIpoChangeEntity
传递给我的方法,我的方法应该 return 四个记录。
这是我的 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
我应该找到所有与我的值匹配的键,然后将它们传递给 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);
}