For example:
I now have an array of
Array
'10000_20000'=>'上海', '20001_30200'=>'北京', '30201_30300'=>'天津', '30301_40000'=>'深圳'
)
Now, I'm going to put each value in the array into the Redis cache, the cache key value can be the array key value after any processing, then I pass in a value of 38000, I want to find in the cache key value contains 38000 of the cache value, is there any good way?
Ask the great God for guidance!
Reply content:
For example:
I now have an array of
Array
'10000_20000'=>'上海', '20001_30200'=>'北京', '30201_30300'=>'天津', '30301_40000'=>'深圳'
)
Now, I'm going to put each value in the array into the Redis cache, the cache key value can be the array key value after any processing, then I pass in a value of 38000, I want to find in the cache key value contains 38000 of the cache value, is there any good way?
Ask the great God for guidance!
If the array key is just a lot, but the value is not large, use an ordered set
$redis->zAdd('key', 10000, '上海');$redis->zAdd('key', 20001, '北京');$redis->zAdd('key', 30201, '天津');$redis->zAdd('key', 30301, '深圳');$redis->zAdd('key', 40001, '非法');$index = 38000;$value = $redis->zRangeByScore('key', $index, '+inf', ['limit' => [0, 1]]); // 深圳
There's no regularity in this data.
My idea, foreach, then take the right part, go compare size, and if it's just greater than or equal to 3800, jump out of the loop and return the current key value.
Of course, the format must be consistent with your present, from small to large
There is a command line in REDIS-CLI: Keys [Patten], Patten can be a regular expression, find the key command ending with 38000: Keys *38000. The corresponding Phpredis method is named: Keys, Getkeys, can be consulted in detail: Keys&getkeys