Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LRU byte Cache java

Tags:

java

I need to implement a cache in java with a maximum size, would like to do it using the real size of the cache in the memory and not the number of elements in the cache. This cache will basically have String as key and String as value. I have already implemented the cache using the LinkedHashMap structure of java but the question is how to know the actual size of the cache so that i can adapt the policy to drop an object when the size is too big.

Wanted to compute it using the getObjectSize() of the instrumentation package but it seems not working as desired.

When I do getObjectSize( a string ) whatever the size of the string is, it returns the same size : 32. I guess it's just using the reference size of the string or something like that and not the content. So don't know how to solve this problem efficiently.

Do you have any ideas ?

Thanks a lot!

like image 337
Abbadon Avatar asked Apr 17 '11 10:04

Abbadon


1 Answers

You might want to consider using Ehcache with memory based cache sizing.

like image 80
WhiteFang34 Avatar answered Sep 29 '22 06:09

WhiteFang34