找到你要的答案

Q:Hibernate cache vs Map - Storage purpose

Q:Hibernate的缓存和地图存储的目的

Let say i have a table which has Master Data seeded 1 time, and rarely it gets updated. It has a flat structure entity, nothing sort of Lazy attributes.

Address{
     id,
     country,
     state,
     city,
     pincode,
     area
}

Case -1 : Table has 1000 records

Case-2 : Table has 1 million records

Use Case - Need to get full Address object by id. No updation in transaction in this table, just used for display purpose, LOV's, reference Id etc.

  1. Considering performance, memory usages, GC, search speed etc which one is better using Hibernate cache(id, object) for this or just using static HashMap (is as key, Address object as value).

  2. I know Hibernate 2nd level cache (ehcache,Infinispan etc) also uses Map structure deep inside libraries. So does it add any useful in above cases, to add extra layer of Hibernate.

比方说我有一个主数据1次播的表,很少有更新。它有一个扁平的结构实体,没有什么懒惰的属性。

Address{
     id,
     country,
     state,
     city,
     pincode,
     area
}

案例1:表有1000个记录

分析:表有100万条记录

用例需要ID没有更新事务表中得到充分的解决对象,仅用于显示目的,爱的,参考ID等。

  1. 考虑到性能,内存使用,气相色谱,搜索速度等,哪一个是更好的使用Hibernate缓存(ID、对象)这还是使用静态HashMap(是为关键,解决对象的值)。

  2. 我知道Hibernate第二级缓存(Ehcache,Infinispan等)也使用地图结构深处的图书馆。所以它在上面加上任何有用的情况下,添加额外的层,Hibernate。

answer1: 回答1:

Ehcache ,Hazelcast(for 2nd level cache) etc has more features than a static Hashmap.

1.limit the maximum number of elements in memory

2.overflow to disk (if the above number is exceeded)

3.set a time-to-live and time-to-idle for elements

4.allows replication within a cluster

If you want these features then you should go for this.Otherwise static hashmap can do your job. Make it refresh after some time if your records gets updated.

One more thing,in both the cases data will be stored in memory or you can say on Heap. But if you want to store data OFF Heap then read about MapDB.

I have worked on hazelcast as 2nd level cache provider.It gives distributed cache as well and work well in clustering environments.

Check these links Disk based HashMap Ehcache Vs Static map cache implementation

Ehcache,Hazelcast(第二级缓存)等具有更多的功能比静态Hashmap。

1.limit元素在内存的最大数量

2.overflow磁盘(如果以上的数目超过了)

3.set生存时间和空闲时间的元素

4.allows复制集群内

如果你想要这些功能,那么你应该这样做。否则静态HashMap能做你的工作。使刷新后一段时间,如果您的记录得到更新。

还有一件事,在这两种情况下数据都会存储在内存中或者可以在堆上说。但如果你想存储数据堆,然后阅读mapdb。

我曾在hazelcast作为第二级缓存提供了分布式缓存以及在集群环境下工作。

Check these links Disk based HashMap Ehcache Vs Static map cache implementation

java  hibernate  dictionary  ehcache  second-level-cache