java 在内存中存储数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14313862/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Storing data in memory
提问by Mike Mugese
I have "TABLE A"
in database which contains 3 columns "COL 1" , "COL 2" and "COL 3"
with around 1000000
(1 Million) records.
我"TABLE A"
在数据库中包含 3 列"COL 1" , "COL 2" and "COL 3"
,其中包含大约1000000
(100 万)条记录。
Against this table i have mapped a simple POJO
lets say "ClassA"
with 3 instance variables "var1" , "var2" , "var3"
and also defined there set()
and get()
methods .
对这个表我制订一个简单的POJO
比方说"ClassA"
用3个实例变量"var1" , "var2" , "var3"
,并在那里定义set()
和get()
方法。
My application contains simple JSPs , Servlets and POJOs with no fancy frameworks and no other technologies .
我的应用程序包含简单的 JSP、Servlet 和 POJO,没有花哨的框架和其他技术。
What I actually want is that when first time my application is going to be deployed on the Application Server and the very first request which it receives , only for that request (only 1 time) my Servlet is going to get the 1 Million records from TABLE A
, maps the ClassA with these records and starts populating the objects for ClassA
and keep them in a Vector or an ArrayList
, as you might have understood that my Vector/ArrayList
will now contain 1 Million objects with each object representing a record/tupple
in the "TableA"
.
我真正想要的是,当我的应用程序第一次部署在应用程序服务器上以及它收到的第一个请求时,仅针对该请求(仅 1 次),我的 Servlet 将从中获取 100 万条记录TABLE A
,在ClassA的地图与这些记录,并开始填充物的ClassA
,并让他们在一个Vector或ArrayList
,你可能已经明白,我的Vector/ArrayList
意志现在包含有代表每个对象1名亿个对象record/tupple
中"TableA"
。
OK , now come to the real and difficult part , I want this Vector/ArrayList
to be stored/persisted/added
to the application context or to the VM or to any other memory storage location (which actually I do not know) , so that each time my JSP pages/Servlets
are accessing the object in memory of getting data and not hitting the database every time .
好的,现在来到真正困难的部分,我希望这Vector/ArrayList
是stored/persisted/added
应用程序上下文或 VM 或任何其他内存存储位置(实际上我不知道),以便每次我的 JSPpages/Servlets
访问对象时在内存中获取数据而不是每次都访问数据库。
采纳答案by Nicholas Bayborodin
You have to use Java Caching System(see here) or Ehcache(see here) to store data in cache, Inversion of Controlin Springalso helps in some way but not sure whether it would help to store 1 million data in heap, as heap memory is limited.
你必须使用的Java缓存系统(见这里)或的Ehcache(见这里的高速缓存)来存储数据,控制反转在春季也有助于在某种程度上,但不知道这是否会有助于存储在堆百万的数据,如堆内存有限。
回答by Mark Bramnik
It looks like you need caching, if I get you right. I would recommend to take a look on EHcacheor Infinispanhere.
如果我没听错的话,您似乎需要缓存。我建议在这里查看EHcache或Infinispan。
Hope this helps
希望这可以帮助