C # cache types

I am designing a cache for generic types and am in doubt if I should cache value types like int, struct, etc. The cache is often used to store expensive objects and the value types are very cheap to create. Limiting the items that can be cached to reference types will make the code easier to implement. Is it a good idea to cache only reference types?

The reason to think about how to remove valerian types from the cache is: The cache can automatically load values ​​through the delegate and that the delegate should not return null. Since value types cannot be compared to null, and defaut (T) can be a valid value type. How can I check if a user-supplied delegate is returning a valid value (assuming no exception was thrown)?

+3


source to share


6 answers


+7


source


Well, conceptually you are not caching value. You are caching the result of the calculation. You don't say "Hey cache give me number 4!", But rather "Hey cache, give me the population of Nebraska January 6, 1932", which should return an int, but that int can be difficult to compute.



+5


source


Typically, the definition of caching should be based on data retrieval cost, rate of change and frequency of use, not data type.

For example, if it takes 5 seconds to compute a single integer, then it will cache that value anyway. In comparison, it can be cheap to fetch the data table from the database every time you need it, and it isn't worth the effort / memory to cache it.

+1


source


I am assuming you are trying to execute a cache

value that is expensive to compute. The calculated value can be anything. It can be a whole or a complex object. You must decide to cache the item if it is difficult to compute, but not included if the computed value Value type or Reference Type

.

If the application targets .net 3.5 or higher, here's a good starting point.

0


source


And as for your problem of using null as a custom value, you can allow nullable and referenced types. You can overload all public generic methods based on generic constraints only, so the 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());
    }

      

0


source


As for your edit: you are assuming a lie. Non-elementary values ​​of types can be compared to zero; the comparison result 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 zero is not limited to generic methods:

http://ideone.com/c74Tc

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

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

      

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

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

      

0


source







All Articles