[英]Implementing shared data structure (e.g. cache) as a concurrentHashMap in a singleton bean
I am implementing an HTTP API using the Spring MVC framework.我正在使用 Spring MVC 框架实现 HTTP API。
I want to store some data between requests and between sessions.我想在请求之间和会话之间存储一些数据。 The data needs to be readable and modifiable by multiple requests in completely independent sessions, but it only needs to exist in-memory while the application is running, it does not need to be persisted to a database, and it does not need to be shared between any scaled-up, multi-node, multi-process server backend design, just one per (eg) Tomcat instance is completely fine.
数据需要在完全独立的会话中被多个请求读取和修改,但只需要在应用程序运行时存在于内存中,不需要持久化到数据库,也不需要共享在任何放大的、多节点、多进程的服务器后端设计之间,每个(例如)Tomcat 实例只有一个就可以了。 Consider for example a cache or something logging some short-lived metrics about the application-specific data coming in through the requests.
例如,考虑一个缓存或记录一些关于通过请求传入的应用程序特定数据的短期指标。
I am assuming the usual way would be to use an in-memory database or something like Redis.我假设通常的方法是使用内存数据库或类似 Redis 的东西。 However, this being my first venture into web stuff and coming from c++ parallel computing personally, this seems like an extremely over-engineered and inefficient solution to me.
然而,这是我第一次涉足网络领域,并且个人来自 c++ 并行计算,这对我来说似乎是一个极度过度设计和低效的解决方案。
Can I not just create a singleton bean containing a ConcurrentHashMap of my required types, inject it as a dependency into my Controller, and be done with it?我是否可以不只是创建一个包含所需类型的 ConcurrentHashMap 的单例 bean,将其作为依赖项注入到我的 Controller 中,然后完成它? I never see anyone talk about this anywhere, even though it seems to be the simplest solution by far to me.
我从来没有看到有人在任何地方谈论过这个,尽管它似乎是迄今为止对我来说最简单的解决方案。 Is there something about how Spring MVC or Tomcat works that makes this impossible?
关于 Spring MVC 或 Tomcat 的工作方式是否有什么使这成为不可能的?
Basically, yes.基本上,是的。 "A singleton ConcurrentHashMap" can be used as a cache.
“单例 ConcurrentHashMap”可以用作缓存。
But, I'd go with something that works like a map but has an API that is specifically tailored to caches.但是,我会使用类似于地图但具有专门为缓存量身定制的 API 的东西。 Fortunately, such a thing exists.
幸运的是,这样的事情是存在的。
Guava is a 'general utilities' project (just a bunch of useful utility classes, lots of em now seem a bit pointless, in the sense that java.util
and co have these too, but guava is over 10 years old, and everything it has didn't exist back then) - and one of the most useful things it has is a 'Cache' class. Guava 是一个“通用实用程序”项目(只是一堆有用的实用程序类,很多 em 现在看起来有点毫无意义,因为
java.util
和 co 也有这些,但是 guava 已经超过 10 年了,它的一切那时还不存在)-它拥有的最有用的东西之一是“缓存”类。 It's a Map with bonus features.这是一张带有奖励功能的地图。
I strongly suggest you use it and follow its API designs.我强烈建议你使用它并遵循它的 API 设计。 It's got a few things that map doesn't have:
它有一些地图没有的东西:
.get()
like with map, it's a variant where you provide the key as well as a computation function that would calculate the value;.get()
,它是一种变体,您可以在其中提供密钥以及计算值的计算函数; the Cache object will just return the cache value if it exists, but if not, it will run the computation, store it in the cache, and return that. I believe you can also get just the guava cache stuff on its own, but if not - you know where to look: Add guava as a dependency to your project, fire up an instance of CacheBuilder
, read the javadocs , and you're off :)我相信你也可以自己获得 guava 缓存的东西,但如果没有 - 你知道在哪里看:将 guava 作为依赖项添加到项目中,启动
CacheBuilder
的实例, 阅读 javadocs ,然后你就走了:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.