除了内存数据库之外,用作缓存的另一种选择是什么?

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

Alternate to Hashmap as cache other than in memory databases.?

问题

我使用哈希映射作为缓存来存储 id 和名称,因为它经常被使用并且在应用程序的整个生命周期内都存在。
对于使用该应用程序的每个用户,大约会有 5000 多个(取决于工作区)id 和名称被存储在哈希映射中。在某个时候会抛出 java.lang.OutOfMemoryError 异常,因为我在哈希映射中保存了很多 (id, 名称)。

我不想清空我的哈希映射缓存值,但我知道为了高效,我们必须使用 LRU 方法或其他方法来清除缓存。

注意:我不想使用 Redis、Memcached 或任何内存中的键值存储。

> 案例用途:slack 每条消息中都会返回用户名称的 id。
>
> 例如:Hello @john doe = 返回 Hello @dxap123。
>
> 我不想为每条消息都发起 API 请求来获取用户名。

有人可以为我提供另一种高效的方法,或者纠正我在方法中的错误吗?

英文:

I am using hashmap as a cache to store id and name. because it is frequently used and it lives through the lifetime of the application.
for every user using the application, around 5000 and more (depends on workspace) ids and names get stored in hashmap. At some point java.lang.OutOfMemoryError exception gets thrown. since I am saving a lot of (id, name) in hashmap.

I don't want to clear my hashmap cache value. but I know to be efficient we have to clear cache using the LRU approach or other approaches.

Note: I don't want to use Redis, Memcached, or any in-memory key-value store.

> Usecase: slack will return id in place of the user name in every
> message.
>
> for eg: Hello @john doe = return Hello @dxap123.
>
> I don't want an API hit for every message to get the user name.

Can somebody provide me an alternate efficient approach or correct me if I am doing something wrong in my approach.?

答案1

得分: 3

像其他人说的那样,5000不应该导致内存不足,但是如果您不限制地图的大小,最终会出现内存不足错误。您应该缓存最近使用或最频繁使用的值,以优化地图的大小。

Google Guava库提供了缓存实现,我认为适合您的用例。

https://github.com/google/guava/wiki/CachesExplained

英文:

Like others have said 5000 shouldn't give you out of memory, but if you don't keep a limit on the size of the map eventually you will get out of memory error. You should cache the values that are most recently used or most frequently used to optimize the size of the map.

Google guava library has cache implementations which i think would fit your usecase

https://github.com/google/guava/wiki/CachesExplained

答案2

得分: 1

对于5000个键值对,不应出现 OutOfMemoryException。如果出现相同情况,则说明您没有正确管理HashMap。如果您有超过5000个项目并且想要HashMap的替代方案,您可以使用 ehcache,这是一个广泛采用的Java缓存,具有分层存储选项,而不是使用内存缓存技术。

Ehcache支持的内存区域包括:

堆内存存储(On-Heap Store): 使用Java堆内存存储缓存条目,并与应用程序共享内存。缓存也会受到垃圾回收的扫描。这种内存非常快速,但也非常有限。

堆外内存存储(Off-Heap Store): 使用RAM存储缓存条目。这种内存不会受到垃圾回收的影响。虽然速度相当快,但比堆内存的速度要慢,因为缓存条目在使用之前必须移动到堆内存中。

磁盘存储(Disk Store): 使用硬盘存储缓存条目。比RAM慢得多。建议使用专用的SSD仅用于缓存。

您可以在这里找到文档:http://www.ehcache.org/documentation/

如果您正在使用 spring-boot,可以按照这篇文章来实现相同的功能。

https://springframework.guru/using-ehcache-3-in-spring-boot/

英文:

For 5000 key-value pairs, It should not through OutOfMemoryException. If It is throwing the same you are not managing the HashMap properly. If you have more than 5000 items and want an alternate for hashmap you can use ehcache, A widely adopted Java cache with tiered storage options instead of going with in-memory cache technologies.

The memory areas supported by Ehcache include:

On-Heap Store: Uses the Java heap memory to store cache entries and shares the memory with the application. The cache is also scanned by the garbage collection. This memory is very fast, but also very limited.

Off-Heap Store: Uses the RAM to store cache entries. This memory is not subject to garbage collection. Still quite fast memory, but slower than the on-heap memory, because the cache entries have to be moved to the on-heap memory before they can be used.

Disk Store: Uses the hard disk to store cache entries. Much slower than RAM. It is recommended to use a dedicated SSD that is only used for caching.

You can find the documentation here. http://www.ehcache.org/documentation/

If you are using spring-boot you can follow this article to implement the same.

https://springframework.guru/using-ehcache-3-in-spring-boot/

答案3

得分: 0

如果这些“names”不是唯一的,那么在将“name”插入映射之前,尝试使用String.intern()对其进行调用,这将减少内存使用量。

英文:

If the "names" are not unique, then try with calling on it String.intern() before inserting the "name" to the map, this would then reduce the memory usage.

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

发表评论

匿名网友

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

确定