ash*_*shK 5 java spring redis spring-data-redis
我正在做Spring Redis
,我把钥匙作为
redistemplate.opsForHash().put("Customer", Customer.class, List<Customers>)
Run Code Online (Sandbox Code Playgroud)
我想从 搜索List<>
,
ScanOptions options = ScanOptions.scanOptions().match(pattern).count(1).build();
Cursor<Entry<Object, Object>> keys = redistemplate.opsForHash().scan("Customer", options);
Run Code Online (Sandbox Code Playgroud)
也不工作。请帮忙!!
首先,扫描操作匹配keye而不匹配值。当使用哈希来存储值时,它应该如下所示:
redisTemplate.opsForHash().put("key", keyInsideHash, value);
因此,Redis 记录的实际键是key
(您使用它来获取哈希)。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;
}
});
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
11113 次 |
最近记录: |