Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/redis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Spring RedisTemplate按值获取哈希键_Spring_Redis - Fatal编程技术网

Spring RedisTemplate按值获取哈希键

Spring RedisTemplate按值获取哈希键,spring,redis,Spring,Redis,我对Spring和Redis都是新手。我想知道是否有办法通过值获取密钥 我的密钥的模式如下:“{typeOfFile}:{id}:{filename}” 文件类型可以是“图像”、“html”或“pdf” 例如,我想获取具有给定fileHash和内容的图像类型文件的密钥。我是这样做的: private String getKeyByVal(final String givenFileHash, final String content) { // get all keys that sta

我对Spring和Redis都是新手。我想知道是否有办法通过值获取密钥

我的密钥的模式如下:“{typeOfFile}:{id}:{filename}”

文件类型可以是“图像”、“html”或“pdf”

例如,我想获取具有给定fileHash和内容的图像类型文件的密钥。我是这样做的:

private String getKeyByVal(final String givenFileHash, final String content) {
    // get all keys that starts with "image"
    Set<String> keys = redisTemplate.keys("image*");
    if (keys != null) {
        for (String key : keys) {
            Map<Object, Object> val = redisTemplate.opsForHash().entries(key);
            // check if the value of KEY is equal to the given fileHash
            if (val.get("fileHash").equals(givenFileHash) && val.get("content").equals(content)) {
               return key;
            }
        }
    }
}
私有字符串getKeyByVal(最终字符串givenFileHash,最终字符串内容){
//获取所有以“图像”开头的键
Set keys=redisTemplate.keys(“image*”);
如果(键!=null){
用于(字符串键:键){
Map val=redisTemplate.opsForHash().entries(键);
//检查KEY的值是否等于给定的fileHash
如果(val.get(“fileHash”).equals(givenFileHash)和&val.get(“content”).equals(content)){
返回键;
}
}
}
}
然而,我被告知这是相当昂贵的,因为我得到了所有以“image”开头的密钥,并手动检查所有这些密钥


现在我在想,如果我能按值得到钥匙可能会更好。这样就更容易得到它的所有属性。这在Redis中是可能的吗?

不,这在Redis中是不可能的。但是,您可以同时存储反向贴图,如下所示:

    fileHash -> "{typeOfFile}:{id}:{filename}"

此解决方案假定文件哈希是唯一的。如果散列不是唯一的,则可以使用相同的散列存储一组ID,检索每个ID的内容并进行比较。仍然比原始解决方案快得多。

不,这在Redis中是不可能的。但是,您可以同时存储反向贴图,如下所示:

    fileHash -> "{typeOfFile}:{id}:{filename}"

此解决方案假定文件哈希是唯一的。如果散列不是唯一的,则可以使用相同的散列存储一组ID,检索每个ID的内容并进行比较。仍然比原始解决方案快得多。

可能重复的可能重复的非常感谢您的回复。我会尽量采纳你的建议。非常感谢你的回复。我将尽力采纳你的建议。