缓存密钥设计

时间:2019-06-12 09:15:29

标签: java caching redis jedis

我们有一个要求,其中每个键必须存储许多值(一对多)。而且我们必须为一个请求的键返回n(每个请求中的n个值会有所不同)的值,并且一旦返回这些值就应该将其删除,即每个值仅消耗一次,因此下一个请求将不会获得这些值。 Redis。

示例问题:-

Ex:-键:-“ 12344”值:-“ a”,“ b”,“ c”,“ d”,“ f”,“ g”,“ h”       键:-“ 12355”值:-“ m”,“ n”,“ o”,“ p”,“ q”

请求1:-获得键12344的2个值         结果:-a,b(顺序可以是任何东西,计数应为2)         返回2个值后,缓存应包含键:-“ 12344”值:-c,d,f,g,h(a,b被删除),以便下一个请求将不会获取这些值

请求2:-获得4个值12344         结果:-c,f,g,h(顺序可以是任意值,计数应为4)         返回值后,缓存应包含键:-“ 12344”值:-d

我们提出了三种方法来处理这种情况:-

1. First option is using list or set or sorted set data structure of redis to store the values and when a request comes for key(Ex:-12344) and n values. Then return and delete those values , so that next request will not get those values. But it is not scalable when there is huge request(ex:- 100000/sec)

2. Second option is creating key like ex:- 12344_a,12344_b. and  store it as (key:12344_a, value:a)(key:12344_b, value:b) and  get n values using :- Key wildcard SCAN Count option. And delete those returned values using pipeline. Wild card search is not time efficient. 

3. Third option is creating key like ex:- 12344_1,12344_2 and store it as (key:12344_1, value:a), (key:12344_2, value:b)

要执行此操作,我们必须存储上次重新调整的键计数Ex:-最后返回5,例如,当新请求出现2个值时,我们必须搜索键12344_6、12344_7等,然后返回这些值并存储将计数器更新为7。并使用管道删除这些值。这样下一个请求将不会获得这些值。但是使用这种方法,我们必须为每个密钥存储最后返回的计数器。

那么对于这个问题还有其他更好的方法吗?

0 个答案:

没有答案