nohros
nohros

Reputation: 327

Caching Value Types c#

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)?

Upvotes: 3

Views: 1624

Answers (6)

phoog
phoog

Reputation: 43056

With regard to your edit: you presuppose a falsehood. Non-nullable value types can be compared with null; the result of the comparison is always false. Consider:

bool IsItNull<T>(T value)
{
    return value == null;
}

Console.WriteLine(IsItNull(new object())); // False
Console.WriteLine((string)null); // True
Console.WriteLine(5); // False

Comparing value types with null is not limited to generic methods:

http://ideone.com/c74Tc

If your validation requirements are more complex, you could accept a validation function along with the delegate that computes the value to be cached:

Func<string> generator = () => GetStringFromDatabase();
Func<string, bool> validator = s => s != null;

For an integer, if you know that the value must be non-negative:

Func<int> generator = () => GetAnIntFromDatabase();
Func<int, bool> validator = i => i >= 0;

Upvotes: 0

Om Deshmane
Om Deshmane

Reputation: 808

And as for your problem of using null as a special value, you can allow nullable and reference types. You could overload all public generic methods on the basis of generic constraints only, so following works

    private static void Perform<T>(T input) 
    {
        Console.WriteLine(typeof(T));
    }

    public static void Test<T>(T input) where T : class
    {
        Perform(input);
    }

    public static void Test<T>(T? input) where T : struct
    {
        Perform(input);
    }

    public static void Tester()
    {
        Test((int ?)2);
        Test(new object());
    }

Upvotes: 0

Sandeep
Sandeep

Reputation: 7334

I assume that you are trying to cache a value which is costly to compute. The computed value can be anything. It can be an Integer or a complex object. You should decide on Caching an item if it is difficult to compute, but not on if the computed value is Value type or Reference Type.

If the application is targeted to .net 3.5 or higher, Here is a good starting point.

Upvotes: 0

Jacob Krall
Jacob Krall

Reputation: 28835

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 int to 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.

Upvotes: 7

Michael Stum
Michael Stum

Reputation: 181034

Well, conceptually you are not caching the value. You are caching the result of a computation. You don't say "Hey Cache, give me the number 4!", but rather "Hey Cache, give me the population of Nebraska on January 6, 1932" which should return an int, but that int may be hard to compute.

Upvotes: 5

Tim M.
Tim M.

Reputation: 54387

Usually the determination to cache should be based on the expense of retrieving the data, how often it changes, and the frequency of use, not the type of data.

For example, if it takes 5 seconds to calculate a single integer, then by all means cache that value. In comparison, it may be cheap to retrieve a table of data from the database every time you need it and it's not worth the effort/memory to cache it.

Upvotes: 1

Related Questions