0

I have the following hash:

HMSET rules:1231231234_11:00_17:00 fw 4444 dm test.abc.com days 'thu, tue, wed'
HMSET rules:1231231234_9:00_10:59 fw 2211 dm anothertest.abc.com days 'thu'

Is there anyway I can search the rules hash and find all records that have a prefix of 1231231234? Something like

HGET rules:1231231234*

OR... perhaps the way I've created the data is wrong. What's the best way to create a data set like this: (json notation)

{
    pn: 1231231234,
    rules: [{
            "expiration_date" : "",
            "days_of_week" : "Thu, Tue, Wed",
            "start_time" : "11:00",
            "end_time" : "17:00",
            "fw" : "9999"
        }, 
        {
            "rule_expiration_date" : "",
            "days_of_week" : "Thu",
            "start_time" : "9:00",
            "end_time" : "10:59",
            "fw" : "2222"

        }]
}

How this data will be used:

I will need to find the rule that applies to me, based on the current time. So for example, when my application gets a request to "process" pn 1231231234, I need to lookup all rules for that pn number, and then find which rule matches my current day of week, and time stamp.

I don't mind getting back all the rules for a given pn and then having the client code loop through to find the right rule.

EDIT 1

Using my data the way it currently has been created, I tried HSCAN like this:

127.0.0.1:6379[1]> HSCAN rules 0 MATCH 1231231234*
1) "0"
2) (empty list or set)
127.0.0.1:6379[1]> 

EDIT 2

As a test, I tried this type of a structure instead:

HMSET rules:1231231234 tue_11:00_17:00 fw 9999
HMSET rules:1231231234 wed_11:00_17:00 fw 9999
HMSET rules:1231231234 thur_11:00_17:00 fw 9999
HMSET rules:1231231234 thu_9:00_10:59 fw 2222

Then I can just see all rules for the main pn. and the use my client app to loop through the results... ?

Happydevdays
  • 1,982
  • 5
  • 31
  • 57

1 Answers1

3

You need to use scan instead of hscan.

Combining SCAN and HGETALL you can achieve this.

1) Do Scan and get all the values matching your pattern

127.0.0.1:6379> scan 0 match rules:1231231234*
1) "0"
2) 1) "rules:1231231234_11:00_17:00"
   2) "rules:1231231234_9:00_10:59"

2) Then for each key in your app logic iterate over them and do an hgetall

127.0.0.1:6379> hgetall rules:1231231234_11:00_17:00
1) "fw"
2) "4444"
3) "dm"
4) "test.abc.com"
5) "days"
6) "thu, tue, wed"

3) if it matches your criteria process.

4) Repeat the same throughout the iteration.

Hope this helps

Karthikeyan Gopall
  • 5,469
  • 2
  • 19
  • 35