dotnetcore / EasyCaching

:boom: EasyCaching is an open source caching library that contains basic usages and some advanced usages of caching which can help us to handle caching more easier!
MIT License
1.92k stars 324 forks source link

FAQ: How to use EasyCaching to query and return array of (cached) objects? #512

Closed hubert-associates closed 7 months ago

hubert-associates commented 7 months ago

Hi, I didn't find this in the doc. I'm using EasyCache C# default RedisProvider with the protobuf "proto" serializer. Works fine. Now, I'd like to query my (EasyCaching) redis using a wildcard in the key and get the resulting list/array of hits back. How can I do this using EasyCaching "best practice"? Thanks for a tip!

catcherwong commented 7 months ago

@hubert-associates Thanks for your interest in this project.

  1. EasyCaching provides GetByPrefix(Async) that can use a wildcard with prefix to query some cacehd objects.
  2. Using .Database to get the raw redis client instance, then you can do what you want.
hubert-associates commented 7 months ago

Thank you for the quick answer. I was unaware of the .Database to provide the instance reference. I looked at the src/ tests

For GetByPrefixAsync. Isn't the prefix just a REDIS pattern? It uses this I believe? But they say there "Don't use KEYS in your regular application code. If you're looking for a way to find keys in a subset of your keyspace, consider using SCAN or sets."

To use .Database. Nice. it looks like a cast does the trick here

catcherwong commented 7 months ago

For GetByPrefixAsync. Isn't the prefix just a REDIS pattern? It uses this I believe? But they say there "Don't use KEYS in your regular application code. If you're looking for a way to find keys in a subset of your keyspace, consider using SCAN or sets."

The prefix use Redis pattern. It will use scan at first if the redis server supports this command, otherwise, it will use keys.

To use .Database. Nice. it looks like a cast does the trick here

YES

hubert-associates commented 7 months ago

Perfect! Great solution. Thank you.