For example, I now have an array ({code ...}) now, I want to save each value in the array to the redis Cache. the cached key value can be the value of the array key after any processing. at this time, I input a value of 38000, I want to find the cache value with the key value containing 38000 in the cache. what is there... for example:
I now have an array
Array (
'2018 _ 20180101' => 'Shanghai', '2018 _ 20180101' => 'Beijing', '2018 _ 20180101' => 'Tianjin ', '1970 _ 000000' => 'shenzhen'
)
Now, I want to save each value in the array to the redis Cache. the cached key value can be the value of the array key after any processing. at this time, I input a value of 38000, I want to find the cache value with the key value containing 38000 in the cache. Is there any good solution?
Please kindly advise!
Reply content:
For example:
I now have an array
Array (
'2018 _ 20180101' => 'Shanghai', '2018 _ 20180101' => 'Beijing', '2018 _ 20180101' => 'Tianjin ', '1970 _ 000000' => 'shenzhen'
)
Now, I want to save each value in the array to the redis Cache. the cached key value can be the value of the array key after any processing. at this time, I input a value of 38000, I want to find the cache value with the key value containing 38000 in the cache. Is there any good solution?
Please kindly advise!
// If there are many keys in the array but the value is not large, use an ordered set.
$ Redis-> zAdd ('key', 10000, 'Shanghai'); $ redis-> zAdd ('key', 20001, 'Beijing '); $ apsaradb for redis-> zAdd ('key', 30201, 'Tianjin '); $ apsaradb for redis-> zAdd ('key', 30301, 'shenzhen '); $ redis-> zAdd ('key', 40001, 'invalid '); $ index = 38000; $ value = $ redis-> zRangeByScore ('key', $ index, '+ inf', ['limit' => [0, 1]); // Shenzhen
This data is irregular.
In my opinion, foreach, then take out the right part and compare the size. if it is just greater than or equal to 3800, it will jump out of the loop and return the current key value.
Of course, the format must be consistent with your current one, from small to large.
Redis-cli has a command line: keys [patten], patten can be a regular expression, and the key command ending with 38000 is: keys * 38000. The corresponding phpRedis method is named keys and getKeys. for details, refer to keys & getKeys.