简体   繁体   English

如何在应用程序级别缓存复杂的Java对象

[英]How to cache complex java objects at application level

I am looking for a solution to cache complex objects in java at application level. 我正在寻找在应用程序级别在Java中缓存复杂对象的解决方案。

For example, i want to load some data set from database to memory and make my application services to work on top of the objects loaded into memory. 例如,我想将一些数据集从数据库加载到内存,并使我的应用程序服务在加载到内存的对象之上工作。 I considered this in order to improve performance. 我考虑过这一点是为了提高性能。

But I am wondering how to keep the relationships of an object in a cache. 但是我想知道如何在缓存中保留对象的关系。

That is for example, 例如,

If a category has many items, what is the best approach to store the relationship. 如果一个类别包含许多项目,那么存储关系的最佳方法是什么。 My idea is to store category and items separately and store the relationship in some other place. 我的想法是分别存储类别和项目,并将关系存储在其他位置。 This is because I use hibernate and the associations are configured to lazy loading. 这是因为我使用了休眠模式,并且关联配置为延迟加载。

Please share your thoughts to approach this problem. 请分享您的想法以解决此问题。

Are you sure you need custom cache implementation on application level? 您确定需要在应用程序级别上实现自定义缓存吗?

Hibernate have first and second level cache, doesn't it enough for you? Hibernate具有一级和二级缓存,对您来说还不够吗? Think about how much memory you will use to store all your hibernate objects in your own, hand-written cache. 考虑一下将用多少内存将所有休眠对象存储在自己的手写高速缓存中。 If it also will read-write, complicity of that decision can bring you more problems than benefits. 如果它也可以读写,那么该决定的共谋可能给您带来更多的问题,而不是带来的好处。 And it will be wheel reinvention, of course. 当然,这将是车轮的重新发明。

Read about how second level cache work in hibernate. 了解二级缓存如何在休眠状态下工作。 http://www.javalobby.org/java/forums/t48846.html and other perfomance tuning: http://docs.jboss.org/hibernate/orm/3.3/reference/en/html/performance.html http://www.javalobby.org/java/forums/t48846.html和其他性能调整: http : //docs.jboss.org/hibernate/orm/3.3/reference/en/html/performance.html

Iteration over lazy loaded collecitons depends of which pattern you're using to work with sesions. 延迟加载的大学校园网的迭代取决于您使用的处理方式。 Good example: https://community.jboss.org/wiki/OpenSessionInView 很好的例子: https : //community.jboss.org/wiki/OpenSessionInView

Firstly if you are aiming for complex objects, dont cache them in memory. 首先,如果您针对复杂的对象,请不要将其缓存在内存中。 It will significantly degrade performance as number of objects increase. 随着对象数量的增加,它将大大降低性能。 Go for a concept of object repositories. 寻找对象存储库的概念。 You can store objects in any complex form and it will suit your requirements 您可以以任何复杂的形式存储对象,这将满足您的要求

Checkout following ir techs. 通过ir techs结帐。 noDB 无数据库

Or JORM JORM

Take a look at ehcache , as suggested in comments if you are looking for a cache lib Here ehcache 如果要查找缓存库,请看注释中建议的ehcache此处ehcache

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

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