I am currently implementing a custom c# cache provider that I can plug in to my site that will do basic caching operation such as adding, retrieving, removing from caching.
I have been thinking about this and wondering to improve performance, that I carry out the adding to cache asynchronously. So as such, when a piece of data, be it a custom object or whatever that I want to insert in to the cache, then I do so by starting a new thread and let it run in the background.
Are there pros and cons to this approach and is it a bad or good idea? I would like some views on the subject. Of course proper locking like follows would need to be implemented when inserting to the .new cache for pesudo example:
- Check if in 开发者_如何学Pythoncache, if not proceed
- start async thread
- check again if in cache
- lock cache
- check again if in cache
- add to cache
You should consider doing some performance evaluation and tuning before deciding to add asynchrony to such a design. Adding threads to a process does not always make it perform better ... and in some instances can actually make matters worse. Asynchrony also makes code more complicated, harder to maintain, and verify correctness - there has to be a significant benefit to overcome these costs.
One of the downsides to an asynchronous design is that there is no opportunity for the calling code to respond to any problems resulting from the attempt to add to the cache. Often this is not an issue, but sometimes it's desireable to allow the caller to participate in determining how to handle cache collisions or other conflicts.
That said, if you ultimately determine that asynchronous operations would actually improve performance, you should consider using the thread pool, rather than a dedicated thread to add items. Using the thread pool is more efficient that starting dedicated threads, and is generally more scalable as a result. This can be easily achievedin C# using async delegate invocation:
// acquire a delegate to the add method
Action<object> addToThreadPool = YourCache.Add;
// fire and forget, async invoke:
addToThreadPool.BeginInvoke( someItemToAdd, addToThreadPool.EndInvoke, null );
I think the real question here is are you adding something to your code due to premature optimization or because there is a true performance need.
Typically you want to just insert to cache and get it done. Spawning a new thread, and handling that thread and associated interactions not only adds complication, but overhead to the process..
Barring that I would say that LBuskin ht the name on the head about the use of a threadpool to handle it.
精彩评论