HttpContext.Cache vs Reflection: Which is More Efficient?

I am using reflection to get authorization attributes from controllers and methods.

Since I will need to get this information over and over again, I'm wondering if it's faster to cache it or just keep using reflection to get it.

Any thoughts?

0


source to share


2 answers


In general, Reflection is not recommended when speed is reached, but you should consider the design time of a cache that actually works (thread safe, very fast, which gives you the current value, not the old inconsistent value) and debug it.



It is very difficult to say which architecture will give the best performance, but you can easily write a small test running two different scenarios with a small set of simple cases. You may find that the speed has not changed at all, or that it differs significantly. In any case, you have your answer.

0


source


this answer comes a little late, but I recently thought about one bit in the project myself and ended up making the cache using the following extension method GetOrCreate don't forget to lock the dictionary if you do.

You can use ImmutableAVLTree instead using lock-free strategy



Finally, there are some useful utilities to use reflection here

This is part of the complete structure, but you can just copy that bit of code if that's the only bit you need.

0


source







All Articles