Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

ASP.NET Core StackExchange.Redis Filtering Value

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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");
}

>Solution :

If you use a single string, then no: redis doesn’t have inbuilt filtering commands (unless you use something like RedisJSON on the server, but that isn’t part of core redis). Redis doesn’t have rich columnar filtering like you might find in, say, a SQL database. The idea is that you create your own explicit indexing using redis primitives. Storing all the data in a single string and fetching the entire thing out each time is not optimal.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading