0

I'm trying to write a couple of caching algorithms but currently I'm stuck on an algorithm based on the size of an object in cache.

Based on the previous questions, I draw the conclusion that there is no good way to get the size of an object. So how is an algorithm based on object size supposed to work?

Community
  • 1
  • 1
  • Is serialization an option? If yes, then it could be; Serialize -> Get Number of Bytes -> Your Cache Algo – Yahya Mar 18 '13 at 11:00

1 Answers1

0

You cannot do it using size of an object, at least not to the very nitty-gritty precision.

Your options are:

  • Use an object size estimate (such as based on the serialised object size) - this might be quite slow, inefficient and only roughly accurate

  • Use some sort of a counter as an indicator of object size, such as when you mutate an object, you increment or decrement a counter to reflect the size of a change

  • Use another criteria for caching, such as time when an object was inserted into a cache or frequency of access requests made to the object

oleksii
  • 35,458
  • 16
  • 93
  • 163