5

我在做Spring Redis,我把钥匙当作

redistemplate.opsForHash().put("Customer", Customer.class, List<Customers>)

我想从List<>,

ScanOptions options = ScanOptions.scanOptions().match(pattern).count(1).build();

Cursor<Entry<Object, Object>> keys = redistemplate.opsForHash().scan("Customer", options);

也不工作。请帮忙!!

4

3 回答 3

4

首先,扫描操作匹配keye 不匹配值。当使用 Hash 存储值时,它应该如下所示: redisTemplate.opsForHash().put("key", keyInsideHash, value); 所以 Redis 记录的实际键是key(您使用它来获取 Hash)。keyInsideHash是您需要存储的实际值的键。所以首先获取哈希然后从中获取值。例如:redisTemplate.opsForHash().put("customerKey1", "FirstName", "MyFirstName");redisTemplate.opsForHash().put("customerKey1", "LastName", "MyLastName");"customerKey1"在此示例中,我们将两个条目存储在相同的哈希中,["FirstName","MyFirstName"]并且["LastName", "MyLastName"]. 在您的情况下,您有一个 List 而不是"MyFirstName". 如果您需要扫描哈希,请执行以下操作(扫描哈希内的键而不是值):

saveToDbCacheRedisTemplate.execute(new RedisCallback<List<String>>() {

            @Override
            public List<String> doInRedis(RedisConnection connection) throws DataAccessException {
                ScanOptions options = ScanOptions.scanOptions().match("pattern").count(1).build();;
                Cursor<Entry<byte[], byte[]>> entries = connection.hScan("customerKey1".getBytes(), options);
                List<String> result = new ArrayList<String>();
                if(entries!=null)
                    while(entries.hasNext()){
                        Entry<byte[], byte[]> entry = entries.next();
                        byte[] actualValue = entry.getValue();
                        result.add(new String(actualValue));
                    }

                return result;
            }

        });
于 2016-02-25T04:59:00.230 回答
1

使用 spring redistemplate 时,请尝试在下面迭代 redis 哈希中的字段和值

  /**
   * hScan provided by redis template is for iterating fields and values within a redis HashMap, not among keys
   * @param key
   * @return
   */
  public Map<String, String> hscanAll(final String key) {
    return hscanPattern(key, "*");
  }

  /**
   * Returns the map with fields that match the given {@Code fieldPattern} and values from the HashMap of the given redis {@Code key}
   * @param key
   * @param fieldPattern
   * @return
   */
  public Map<String, String> hscanPattern(final String key, final String fieldPattern) {
    return (Map<String, String>)
        redisTemplate.execute(
            (RedisCallback<Map<String, String>>)
                connection -> {
                  Map<String, String> map = new HashMap<>();
                  try (Cursor<Map.Entry<byte[], byte[]>> cursor =
                      connection.hScan(
                          key.getBytes(),
                          new ScanOptions.ScanOptionsBuilder().match(fieldPattern).count(200).build())) {

                    while (cursor.hasNext()) {
                      Map.Entry<byte[], byte[]> entry = cursor.next();
                      map.put(
                          new String(entry.getKey(), "Utf-8"),
                          new String(entry.getValue(), "Utf-8"));
                    }
                  } catch (Exception e) {
                    log.error(e.getMessage(), e);
                    throw new RuntimeException(e);
                  }
                  return map;
                });
  }

于 2020-10-15T09:39:45.290 回答
1

今天遇到了这个问题,发现我的redis服务器版本是 redis_version:2.6.16redis.io说这个命令从2.8.0开始就可以使用了。请参阅:https ://redis.io/commands/scan 。

希望能帮到你!

于 2017-04-07T09:05:43.227 回答