Redis中的unlink哈希 – 哈希是否被视为一个整体?

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

Redis unlink hash - is the hash treated as a whole or not?

问题

在Redis中,哈希表的情况如下:

ha -> {
  k1 -> v1
  k2 -> v2
  k3 -> v3
}

当执行unlink ha时,Redis会如何实际删除它?

我能想到的可能方式:

  • A. 首先删除单个键ha,然后稍后在另一个线程中将整个值(包括3个子键)作为一个整体删除。
  • B. 删除键ha,以及子键k1k2k3。然后稍后删除子键的3个值?

那么是哪种情况,或者还有其他方式?

根据上述代码,似乎更像是情况B。这是有道理的,因为哈希表存储对值的引用,而不是在单个内存块中,因此需要逐个释放值,对吗?


相关的Redis源代码

https://github.com/redis/redis/blob/3ca451c46fed894bf49e7561fa0282d2583f1c06/src/dict.c#L553

英文:

e.g:
In redis, the hash is:

ha -> {
  k1 -> v1
  k2 -> v2
  k3 -> v3
}

When unlink ha, how would redis actually delete it?
(assume values of the 3 sub keys are large enough to make redis decide to remove them in a separate thread).

The possible ways I can image:

  • A. Remove single key ha, then later remove the whole value (with 3 sub keys) as a whole, in another thread.
  • B. Remove key ha, and sub keys k1, k2, k3. Then later remove the 3 values of the sub keys?

So which is the case, or there are more ways?


Relevant redis source code

https://github.com/redis/redis/blob/3ca451c46fed894bf49e7561fa0282d2583f1c06/src/dict.c#L553

According to the code above, seems it's more like case B.
That make sense, since hashtable store reference to value, not in a single memory block, thus need to free the values 1 by 1, right ?

答案1

得分: 3

当调用unlink来移除一个键(哈希或其他结构)时,Redis会逻辑上删除它,从数据库中移除它(键、子键和值),并将其放入待删除列表。会有一个后台任务在另一个线程中懒惰地删除这些已被移除的项目。当Redis执行真正的删除工作时,它会删除与该键有关的所有内容(键、子键和值)。

英文:

When you call unlink to remove a key (hash or any other structure), Redis deletes it logically by removing it (key, sub key, and value) from the DB, and putting it into a to-be-deleted list. There will be a background job that deletes these lazily removed items from another thread. When Redis does the real delete job, it delete everything about the key (key, sub key, and value).

huangapple
  • 本文由 发表于 2023年6月6日 16:25:29
  • 转载请务必保留本文链接:https://go.coder-hub.com/76412736.html
匿名

发表评论

匿名网友

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

确定