Using Redis cache, the cache key value is the interval value (can be processed), how to find the corresponding cache when an interval number in the interval is passed in

Source: Internet
Author: User
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

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.