【问题标题】:How to store result of any redis command in a kay and how to store table data in redis structures?如何将任何redis命令的结果存储在kay中以及如何将表数据存储在redis结构中?
【发布时间】:2012-11-21 08:13:23
【问题描述】:

大家好,我是 redis 新手,我想在我们的应用程序中使用 redis 来进行频繁的数据库插入/更新和删除。

我在 postgres 中有一张这样的表格。

  testtbl                                                   

 unixtime   |   code    | flag1 |count1     |   count2  |   flag2
_________________________________________________________________
1353475056  |   1234    |  A    |60         |   8955    |     N   
1353475060  |   5248    |  B    |131        |   22500   |     F   
1353475056  |   7267    |  C    |36         |   10130   |     X   
1353475056  |   1908    |  B    |0          |       0   |     N   
1353475060  |   9290    |  E    |90         |   11905   |     X   
1353475056  |   6123    |  F    |1          |   702     |     F   
1353475060  |   4145    |  G    |117        |   47920   |     X   
1353475099  |   7000    |  L    |43         |   21720   |     F   
1353475099  |   3256    |  D    |40         |   3915    |     N   

我尝试使用 redis 哈希存储这些记录,如下所示

hmset testtbl:1 unixtime 1353475056 code 1234 flag1 A count1  60 count2  8955 flag2 N
hmset testtbl:2 unixtime 1353475060 code 5248 flag1 B count1 131 count2 22500 flag2 F
hmset testtbl:3 unixtime 1353475056 code 7267 flag1 C count1  36 count2 10130 flag2 X
hmset testtbl:4 unixtime 1353475056 code 1908 flag1 B count1   0 count2     0 flag2 N
hmset testtbl:5 unixtime 1353475060 code 9290 flag1 E count1  90 count2 11905 flag2 X
hmset testtbl:6 unixtime 1353475056 code 6123 flag1 F count1   1 count2   702 flag2 F
hmset testtbl:7 unixtime 1353475060 code 4145 flag1 G count1 117 count2 47920 flag2 X
hmset testtbl:8 unixtime 1353475099 code 7000 flag1 L count1  43 count2 21720 flag2 F
hmset testtbl:9 unixtime 1353475099 code 3256 flag1 D count1  40 count2  3915 flag2 N

要首先获取任何记录,我需要找到该记录的密钥,并且 然后使用该关键特定记录需要像这样获取

redis 127.0.0.1:6379> hgetall testtbl:3
 1) "unixtime"
 2) "1353475056"
 3) "code"
 4) "7267"
 5) "flag1"
 6) "C"
 7) "count1"
 8) "36"
 9) "count2"
10) "10130"
11) "flag2"
12) "X"

现在问题是
1) 如何将任何键的值分配给另一个键或将任何命令的结果存储在 kay 中?
例如。如果我想在 redis 中将“TIME”命令的结果存储到 key testtbl:在这种情况下是 unixtime
2) 有没有其他有效的方法来存储这些数据?
3) 如何获取 unixtime = 1353475056 或 flag2 = "N" 的所有记录
4)redis中是否有批量获取实用程序?

【问题讨论】:

    标签: redis


    【解决方案1】:

    1) 只能在客户端实现。

    2, 3) 这取决于场景。对于第三个问题,可以使用排序集来存储unixtime:

    ZADD testtbl.unixtime 1353475056 testtbl:1 1353475060 testtbl:2 1353475056 testtbl:3 ...
    

    获取所有unixtime = 1353475056的记录:

    ZRANGEBYSCORE testtbl.unixtime 1353475056 1353475056
    1) "testtbl:1"
    2) "testtbl:3"
    

    使用一个集合来存储flag2:

    SADD testtbl.flag2:N testtbl:1 testtbl:4 testtbl:9
    

    获取所有flag2 = "N"的记录

    SMEMBERS testtbl.flag2:N
    1) "testtbl:1"
    2) "testtbl:4"
    3) "testtbl:9"
    

    【讨论】:

      猜你喜欢
      • 2023-03-17
      • 1970-01-01
      • 2013-10-14
      • 2013-07-28
      • 2016-02-13
      • 1970-01-01
      • 1970-01-01
      • 2021-08-22
      • 1970-01-01
      相关资源
      最近更新 更多