问题
I'm using a Spring Redis repository and I'm puzzled with the delete operation and the phantom key.
When a delete is performed, the phantom key is not deleted, is it a normal behaviour ? If yes, is it possible to force a deletion of the phantom key when the original key is deleted from the code.
I was expecting that a delete removes the original key AND the associated phantom key.
I planned to use to timeToLive feature to ensure that keys not deleted by my application will expire after a while.
Annotation set on the concerned domain object
@RedisHash(value = "requestContext", timeToLive = 9000)
The delete is performed on this way:
repository.delete(id)
Thank in advance for your help.
回答1:
Phantom key is not deleted immediately when you delete key/value from Redis. Spring Data Redis manages phantom key to manage secondary indexes along with other functionalities. Spring Data Redis persists a copy of the original hash as phantom hash with a slightly longer TTL (5 minutes). That mean the :phantom TTL in Redis is 5 minutes more than the regular key TTL. When the original hash expires, Spring Data Redis loads the phantom hash to perform cleanups such as removing references from secondary indexes etc. Read more: Redis key Expirations
When the expiration is set to a positive value the according EXPIRE command is executed. Additionally to persisting the original, a phantom copy is persisted in Redis and set to expire 5 minutes after the original one. This is done to enable the Repository support to publish RedisKeyExpiredEvent holding the expired value via Springs ApplicationEventPublisher whenever a key expires even though the original values have already been gone. Expiry events will be received on all connected applications using Spring Data Redis repositories.
来源:https://stackoverflow.com/questions/46467965/delete-key-value-from-redis-phantom-key-not-deleted