Spring Boot @RedisHash创建多个键而不是一个。

huangapple go评论67阅读模式
英文:

Spring boot @RedisHash creates multiple keys instead of one

问题

我正在使用 spring-data-redis 来向 Redis 中添加和检索数据。但一旦数据写入到 Redis 中,我可以看到多个键而不是一个。只有在过期时才会删除一个键,其余的键仍然存在于 Redis 中。

要保存数据,可以使用以下代码:

@RedisHash(timeToLive = 60, value="vault_token")
@Data
public class VaultTokenModel {

    private Integer id;

    private String token;
}

保存数据的方法如下:

VaultTokenModel vaultTokenModel = new VaultTokenModel();
vaultTokenModel.setId(1);
vaultTokenModel.setToken(getVaultToken());
VaultTokenModel savedToken = vaultTokenRepository.save(vaultTokenModel);
return savedToken.getToken();

但是当我打开 redis-cli 并运行 KEYS * 命令时,我可以看到以下键:

  1. "vault_token:1"
  2. "com.highpeak.tlp.attributemanager.db.model.VaultTokenModel:1:phantom"
  3. "vault_token:1:phantom"
  4. "com.highpeak.tlp.attributemanager.db.model.VaultTokenModel"
  5. "vault_token"

并且在设置 timeToLive 后,如果我再次运行相同的命令,我可以看到:

  1. "com.highpeak.tlp.attributemanager.db.model.VaultTokenModel:1:phantom"
  2. "vault_token:1:phantom"
  3. "com.highpeak.tlp.attributemanager.db.model.VaultTokenModel"
  4. "vault_token"

这些附加的键是什么,为什么它们以完整的包名称和随机唯一标识符 "phantom" 创建?

谢谢。

英文:

I am using spring-data-redis to add and retrieve data fro Redis. But once the data is written to the redis, I can see multiple keys instead of one. On expiration only 1 key is getting removed and rest of the keys are still present in Redis.

@RedisHash( timeToLive = 60, value="vault_token" )
@Data
public class VaultTokenModel {

    private Integer id;

    private String token;
}

To save,

VaultTokenModel vaultTokenModel = new VaultTokenModel();
                vaultTokenModel.setId(1);
                vaultTokenModel.setToken(getVaultToken());
                VaultTokenModel savedToken = vaultTokenRepository.save(vaultTokenModel);
                return savedToken.getToken();

But when I open the redis-cli and run KEYS *, I can see,

1) "vault_token:1"
2) "com.highpeak.tlp.attributemanager.db.model.VaultTokenModel:1:phantom"
3) "vault_token:1:phantom"
4) "com.highpeak.tlp.attributemanager.db.model.VaultTokenModel"
5) "vault_token"

And after the set timeToLive, if I run the same command again, I can see,

1) "com.highpeak.tlp.attributemanager.db.model.VaultTokenModel:1:phantom"
2) "vault_token:1:phantom"
3) "com.highpeak.tlp.attributemanager.db.model.VaultTokenModel"
4) "vault_token"

What are these additional keys and why are they getting created with full package name and with a random unique identifier phantom.

Thank You

答案1

得分: 1

这些多个键由Spring Data Redis维护,只是一些元信息。
如果你感兴趣,你可以在redis命令中输入

type [whatever-key]

来查找键的类型,然后发出相应的get命令。
例如,如果

type vault_token

返回结果为哈希,
那么你可以使用

hgetall vault_token

查看里面的内容。

你看到的幻象是Spring Data Redis缓存的一些信息,一段时间后将被删除。更多信息,请查看https://stackoverflow.com/questions/46467965/delete-key-value-from-redis-phantom-key-not-deleted

英文:

These multiple keys are maintained by Spring Data Redis, just some meta info.
If you're curious, you can issue

type [whatever-key]

in the redis command, to find the type of the key, then issue a corresponding get command.
For example, if

type vault_token

returns the result as hash,
then you use

hgetall vault_token

to look at what's inside.

The phantom thing you see is just some info cached by Spring Data Redis and will be deleted after a while. For more info, check https://stackoverflow.com/questions/46467965/delete-key-value-from-redis-phantom-key-not-deleted

huangapple
  • 本文由 发表于 2020年8月12日 18:49:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/63374955.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定