Redisson客户端:检索前N个键

问题描述

我怎样才能从redisson client的N个键中找到最重要的键?

我在getKeysByPattern()方法中找到了下一个签名:

Iterable<String> getKeysByPattern(String pattern,int count);

但是看起来像count-每次向Redis请求都加载了密钥。

如何通过Redisson客户端从Redis加载前N个键?

解决方法

val luaScript = "return {redis.call('SCAN',ARGV[1],'MATCH',ARGV[2],'COUNT',ARGV[3])}" var cursor = 0 do { val data = redissonClient .getScript(StringCodec.INSTANCE) .eval(RScript.Mode.READ_ONLY,luaScript,RScript.ReturnType.MAPVALUELIST,listOf(),cursor,"some-pattern",batchSize) val redistListData = (data[0][1] as List<String>) cursor = data[0][0].toInt() } while (cursor != 0) 在这种情况下无效,对。

最好在这里使用lua脚本:

def discount_rule(booklist,price):
    discount_rates = [0,0.05,0.1,0.2,0.25]
    different_books = len(set(booklist))
    discount = discount_rates[different_books]
    discount_amount = discount * price;
    discounted_price = price - discount_amount;
    return {"grandTotal": len(books) * discounted_price,"discount": discount_amount,"book_price": price - discounted_price
            "books" : different_books
    }

  
#list of books
books = ["book 1","book 2","book 3","book 4","book 5"]
unit_price = 8
cash = discount_rule(books,unit_price)

if cash['books'] == 1:
    print ("One book is {} EURO - no discount".format(unit_price))
print("Discount : ",cash["discount"])
print("Total cost  : ",cash["grandTotal"])
if len(books) > cash["books"]:
    print("no discount if two books of the same title are purchased")
,

在这种情况下,您需要使用RKeys.getKeysWithLimit(String pattern,int limit)方法。