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)?
source to share
The cache should be used when generating a value for something that takes a long time. For example, the computation "The answer to the ultimate question of life", "The universe and everything" takes a huge supercomputer 7.5 million years, but only takes int
storage. This calculation is still very beneficial for caching if another user asks for Life's Ultimate Question Answer, Universe and Everything.
source to share
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.
source to share
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.
source to share
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.
source to share
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());
}
source to share
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:
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;
source to share