简体   繁体   English

如何在Rails上使用ruby从redis获取所有匹配的密钥

[英]How can I get all matched keys from redis using ruby on rails

How Can I get all matched keys from list of keys stored in redis using Ruby on rails. 如何使用Ruby on Rails从Redis中存储的密钥列表中获取所有匹配的密钥。 I am using Ruby 2.5.1 version. 我正在使用Ruby 2.5.1版本。

I used SCAN to query matched keys, but it returns 2 values always instead of 5. It returns all when I pass count. 我使用SCAN来查询匹配的键,但是它总是返回2个值而不是5个值。 So How can I pass count in Ruby On Rails code? 那么如何在Ruby On Rails代码中传递计数?

Without count return 2 value: 不计算返回值2:

  127.0.0.1:6379> SCAN 100 match *sa*
       1) "sasikala (6965)"
       2) "Zupaen sasi (6961)"

With count return all (5) value: 用count返回所有(5)值:

127.0.0.1:6379> SCAN 100 match *sa* count 100
           1) "Sasai (6965)"
           2) "Zupaen sasi (6961)"
           3) "Rosan (7000)"
           4) "Alisa (2363)"
           5) "Carissa (4455)"

Ruby on rails code: Ruby on Rails代码:

keys = redis.scan(0, :match => '*' + name + '*')

Kindly provide your thoughts on this. 请提供您对此的想法。

Assuming you use official redis-rb to handle redis connection, their tests contain the answer on your question: 假设您使用官方的redis-rb处理redis连接,那么他们的测试将包含您问题的答案

loop.inject([0, []]) do |(cursor, keys), _|
  cursor, new_keys = redis.scan(cursor, match: '{key}*')
  keys << new_keys
  break keys if cursor == '0'
  [cursor, keys]
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Ruby on Rails:如何在服务器端从Redis获取所有会话数据? - Ruby on Rails: How can I get all sessions data from Redis on server side? ruby / rails ruby​​ / rails如何使用Twitter API从(用户的)Twitter LIST获取所有推文? - ruby/rails ruby/rails How can I get all the tweets from Twitter LIST (of users) using the Twitter API? 使用redis ruby​​ gem,如何从redis中清除一组与模式匹配的键? - Using the redis ruby gem, how do I clear a set of keys from redis matching a pattern? 在Ruby on Rails中使用Redis的“ Keys *” - Use “Keys *” of Redis in Ruby on Rails 如何从使用Ruby on Rails中的方法获得的类模型中对Enumerable进行排序? - How can I sort an Enumerable from a class model obtained using method all in Ruby on Rails? 如何在 Rails 上使用 Ruby 从 email 中提取所有 URls/链接? - How can I extract all URls/links from an email using Ruby on Rails? 将Redis与Ruby on Rails一起使用 - Using Redis with Ruby on Rails 如何使用Ruby / Rails从网站获取内容? - How do I get content from a website using Ruby / Rails? 如何从 Redis 商店返回所有具有匹配键的值 - How to return all values with matched key from Redis store 如何在 Rails 上的 Ruby 中获取字段与 IPv6 格式匹配的所有条目? - How can I get all entries where field matches IPv6 format in Ruby on Rails?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM