简体   繁体   English

Java资源池

[英]Java resource pool

I have an app that stores data in XML. 我有一个应用程序以XML存储数据。 When user asks for data, XML is parsed into entities, which are then showed to him via something-like-servlet. 当用户请求数据时,XML被解析为实体,然后通过类似servlet的方式显示给他。

My problem is, that after every refresh, something-like-servlet has to process data again and parsing XML is expensive. 我的问题是,每次刷新后,类似servlet的服务器都必须再次处理数据,而且解析XML的成本很高。 There is also too much XML's to store all data in memory. XML太多,无法将所有数据存储在内存中。

I was thinking about something like resource pooling, where program asks pool, if there is Entity with name foo in pool. 我在考虑诸如资源池之类的问题,程序会询问池,池中是否有名称为foo的实体。 If there is, foo is returned from pool, if not, foo is created from XML and saved to pool. 如果存在,则从池中返回foo ,否则,从XML中创建foo并将其保存到池中。

Something like this: 像这样:

Entity entity = null;
entity.setId(id);
for(Entity entityFromPool : pool.getAllEntities()){
      if(entityFromPool.getId().equals(entity.getId())){
            entity = entityFromPool();
            isInPool = True;
}
if(!isInPool){
   entity = getEntityFromXML();
}

I have read about apache object pooling but if I understand it correctly, I can not store fe 5 different entities with it and then ask if entity with particular id exists in pool. 我已经阅读过关于Apache对象池的信息,但是如果我对它的理解正确,我将无法存储5个不同的实体,然后询问池中是否存在具有特定ID的实体。

Is there any library that can help with my problem? 有没有可以帮助解决我的问题的图书馆? Many thanks! 非常感谢!

If you're referring to Commons Pool, then you can take a look at the KeyedObjectPool interface. 如果您指的是Commons Pool,那么您可以看一下KeyedObjectPool接口。

A "keyed" pooling interface. “键控”池接口。

A keyed pool maintains a pool of instances for each key value. 密钥池为每个密钥值维护一个实例池。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM