带有定时/瞬态条目的映射 - 内存不是问题

发布于 2024-10-09 03:36:57 字数 647 浏览 0 评论 0原文

我正在尝试创建一个地图,其中条目会超时并在一定时间段后被删除。

基本上, Map.put(K key, V value, long **time**) - 该条目将立即放入地图中,并在时间(毫秒)后过期。我不需要在将来的任何时候恢复已删除的条目,但我想确保它不再出现在地图中。

例如:map.put("foo", "bar", 60l * 1000l) 会让这个键值对在地图中生存一分钟(60long 和 1000long)。

尝试:使用 ConcurentMap 并通过以下方式实现 Map.put(K key, V value, long **time**)
1. 调用super.put(key, value)
2. 创建一个休眠时间(毫秒)的线程
3. 调用remove(key)删除该条目。

问题:请发表评论/让我知道这在线程安全性、一致性或我的尝试中是否存在任何缺陷方面是否是一个好主意。如果您认为有更好的方法来实现此目的,请提供任何建议。

编辑:感谢您的回复,内存不是这里的问题,我真的只关心条目的短暂寿命。谢谢。

I'm trying to create a map in which the entries time out and get removed after a certain time period.

Basically, <K, V> Map.put(K key, V value, long **time**) - the entry will be put in the map instantly and will expire after time (ms). I do not need to recover the removed entry at any point in the future but I would like to make sure it's no longer in the map.

For example: map.put("foo", "bar", 60l * 1000l) will let this key-value pair live in the map for a minute (60long and 1000long).

Attempt: use a ConcurentMap and implement Map.put(K key, V value, long **time**) via the following:
1. call super.put(key, value)
2. create a thread that sleeps for time (ms)
3. call remove(key) to remove the entry.

Question: please comment/let me know whether this is a good idea in terms of thread-safety, consistency or any flaws in my attempt. If you think there's a better way to accomplish this, please provide any advice.

Edit: Thanks for the replies, memory is not the issue here, I really only care about the short life-span of the entries. Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

ˉ厌 2024-10-16 03:36:57

时间是您要解决的问题的重要组成部分吗?或者它是一个实施细节?如果您要解决的问题是内存使用问题,那么另外两种可能性就会浮现在脑海中:

  • LRU 映射。网络上有很多此类内容。
  • 由 WeakReference 或 SoftReference 对象支持的 Map,允许 GC 收集这些项目。

编辑

在这种情况下,一些现有的实现可能会节省您一些时间。例如:

Is the time an essential part of the problem you're solving? Or is it an implementation detail? If the problem you're solving is one of memory use, two other possibilities spring to mind:

  • An LRU Map. There are a number of these on the web.
  • A Map backed by WeakReference or SoftReference objects, which allow the GC to collect those items.

EDIT

In that case, there are some existing implementations that might save you some time. For instance:

如梦初醒的夏天 2024-10-16 03:36:57

如果您这样做是因为某些内存问题(地图是临时缓存),您可能应该考虑使用软引用(文档)。

If you are doing this because of some memory issue (the map is a temporal cache), you should probably consider using soft references (documentation).

作死小能手 2024-10-16 03:36:57

查看番石榴 - 这是 Google 的一个收藏库。特别是,您需要查看 CacheBuilder 及其计算图。它的功能之一是“基于时间的条目过期,自上次访问或上次写入以来测量”,听起来正是您正在寻找的。

(编辑讨论 CacheBuilder;自从我发布此内容以来它是新的并且与问题更相关)

Take a look at Guava - it's a collection library from Google. In particular, you want to look at CacheBuilder and its computing maps. One of its features is "time-based expiration of entries, measured since last access or last write", sounds like exactly what you're looking for.

(Edited to talk about CacheBuilder; it's new since I posted this and is more relevant to the question)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文