DEV Community

Discussion on: Implementing an LRU Cache

Collapse
 
turnerj profile image
James Turner

What are your thoughts on actually considering cache size? Like the implementation shown is a limit of a number of items though as each item could be of any size, it could be under utilised or over utilised.

Do you think measuring the actual size of a cache item is something to consider or should effort instead be profiling their application and what they cache to take a "best guess" at how many would fit in memory?

Collapse
 
katkelly profile image
Katherine Kelly

That's a good point you make about the differing item sizes. The generic cache implementation assumes an average size but there is probably an approach you could utilize to take specific item size into account.

Collapse
 
turnerj profile image
James Turner

Thanks for the reply. Do you think it is worth measuring the actual item size or do you think it adds unnecessary complexity to a caching solution?

I ask this because I have my own caching solution in C# and I am contemplating calculating the item size. Problem is, calculating the size remotely accurately is both time expensive and memory intensive (eg. serializing the whole thing to a string and measuring the length allocates a lot of memory).

Thread Thread
 
katkelly profile image
Katherine Kelly

Personally, I think it adds unnecessary complexity and wouldn't do it myself but I think it's great you're considering the tradeoffs. Best of luck with whatever you choose for your caching solution!