Home >Backend Development >PHP Tutorial >Using redis cache, the cache key value is an interval value (which can be processed). When an interval number in the interval is passed in, how to find the corresponding cache?

Using redis cache, the cache key value is an interval value (which can be processed). When an interval number in the interval is passed in, how to find the corresponding cache?

WBOY
WBOYOriginal
2016-12-01 01:27:461698browse

For example:
I now have an array
array(

<code>  '10000_20000'=>'上海',
  '20001_30200'=>'北京',
  '30201_30300'=>'天津',
  '30301_40000'=>'深圳'</code>

)
Now, I want to store each value in the array into the redis cache. The cache key value can be the array key value after any processing. At this time, I pass in a value of 38000. I want to find the key in the cache. The value contains a cached value of 38000. Is there any good way

Please give me some guidance!

Reply content:

For example:
I now have an array
array(

<code>  '10000_20000'=>'上海',
  '20001_30200'=>'北京',
  '30201_30300'=>'天津',
  '30301_40000'=>'深圳'</code>

)
Now, I want to store each value in the array into the redis cache. The cache key value can be the array key value after any processing. At this time, I pass in a value of 38000. I want to find the key in the cache. The value contains a cached value of 38000. Is there any good way

Please give me some guidance!

//If the array has a lot of keys but a small value, use an ordered set

<code>$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]]); // 深圳</code>

This data is irregular
My idea is, foreach, then take out the right part and compare the size. If it happens to be greater than or equal to 3800, break out of the loop and return the current key value.
Of course, the format must be consistent with what you have now, from childhood to adulthood

There is a command line in redis-cli: keys [patten], patten can be a regular expression, and the command to find the key ending with 38000 is: keys *38000. The corresponding phpRedis method names are: keys, getKeys. For details, please refer to: keys&getKeys

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn