I have a .NET Core 6.0 project that I use StackExchange.Redis.
First, I am wondering if I can filter the value coming with Key-Value pair.
When I get the key, I get all the values and after that I am filtering them.
Is there anyway to filter values before getting them all or I have to filter it after getting all the values ?
-- TestModel2.cs
public class TestModel2
{
public List<Product> Products { get; set; }
public List<Category> Categories { get; set; }
}
-- RedisCacheService.cs
public async Task<T> GetAsync<T>(string key) where T : class
{
string value = await _client.GetDatabase().StringGetAsync(key);
return value.ToObject<T>();
}
--ToObject
public static T ToObject<T>(this string value) where T : class
{
return string.IsNullOrEmpty(value) ? null : JsonConvert.DeserializeObject<T>(value);
}
--CacheController.cs
[HttpGet]
[Route("GetProductsByCategoryId")]
public async Task<IActionResult> GetProductsByCategoryId(int id)
{
var models = await _cacheService.GetAsync<List<TestModel2>>("models3");
if (models != null && models?.Count() > 0)
{
try
{
var model = models[0].Products.Where(x => x.CategoryId == id);
if (model != null)
{
return Ok(model);
}
}
catch (Exception)
{
return StatusCode(500);
}
}
return BadRequest("Not Found");
}