skip to Main Content

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

2

Answers


  1. Chosen as BEST ANSWER

    I also found after searching about how to filter and something like that, RediSearch also can be used and integrated with ASP.NET Core Project...

    If you are using Docker, RedisLabs/redismod is useful for it...


  2. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search