I am developing a cache for generic types and have a doubt, should I cache value types, such as int, struct, etc. Cache is often used to store costly objects and value types is very cheap to create. Limiting the items that can be inserted in cache to reference types will make the code more easily to implement. Allowing cache to store only reference types is a good idea?
The reason to think in removing valur types from the cache is: The cache can load values automatically through a delegate and that delegate should never returns null. Since value types can not be compared with null and a defaut(T) could be a valid value type. How can I check if the user provided delegate returns a valid value (assuming that a exception was not raised)?
A cache should be used whenever generating the value of something that takes a long time. For example, calculating The Answer to the Ultimate Question of Life, the Universe, and Everything takes an enormous supercomputer 7.5 million years, but takes only an
intto store. This calculation is still very likely to benefit from caching, in case another user asks for The Answer to the Ultimate Question of Life, the Universe, and Everything.