skip to Main Content

So, I have a heap (thousands) of hashes, with names like this:

state:city:street

And within each one is something like:

signId => 10,
damaged => 1

I essentially want to do some trickery that I am not sure is possible.

I want to get all values for signId but only if the signId entry exists but the damaged entry does not exist…

For all hashes that fall into the wildcard state:city:*

And all in PHP / Laravel / Predis

2

Answers


  1. You will have to get the keys first as keys state:city:*

    Then, you will have to do HGETALL state:city:{name} one by one i.e in a loop.

    Login or Signup to reply.
  2. There is no built-in feature or a function to do them in redis. I don’t recommend you to use keys in production. As it is mentioned on redis documentation;

    keys should be used in production environment with extreme care. It may ruin performance when it is executed against large databases.

    I will recommend three options;

    • scan is a better alternative to the keys since it doesn’t block the server for a long time. On your application level you may execute the hash keys matching to your pattern and then using HGET (O(1)) two times and making comparison. HGETALL is also another option if your hash keys don’t have many fields.

    • Another option is to keep track of your hash keys matching a pattern in a separate set/list. Whenever you call HMSET or HSET you also push that key in a set/list. By keeping tracking of the keys, you no longer need to use scan later. You may just get them in a paginated way to execute related hash commands. As the recommendation one, this also require application level querying instead of data layer querying.

    • Since you need to query with multiple conditions it may be better to keep them in a relational database(postgresql, mysql etc). Redis is good to serve your hashes, but when you need to query your hashes the way you want to query then it is better to re-model your data.

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