Double check locking Extension Method for ASP.NET Cache
On my current project, we need to cache some of the results retreived when querying into SharePoint. As the retrieving operation is quite expensive (as always with SharePoint) and that performances are very important, we must make sure that it is only executed once.
So, the way to do this is to use the double check locking mechanism. The idea is to check if the cache contains the item sought, if it does not contain it, lock then check again, in case another thread added it while we were acquiring the lock.
I wrote a generic extension method to do this, to which the lock handle to lock on is given as a parameter, as well as the function used to retrieve the item if it is not in the cache. In this case, the retrieved item will be stored in the cache (and of course returned to the called).
Code speaks louder than words:
public static class CacheExtension { public static T SetAndGetItemFromCache<T>(this Cache cache, String cacheKey, Object lockHandle, Func<Cache, T> addItemToCache) { Object item = cache.Get(cacheKey); if (item == null) { lock (lockHandle) { item = cache.Get(cacheKey); if (item == null) { item = addItemToCache(cache); } else { //The sought item has been added meanwhile in another thread/process } } } else { //Cache contains the key, return it } if (item is T) { return (T)item; } else { //The Object in the cache is not of type T, some other component might be using that key throw new ApplicationException(String.Format("Object retreived from the cache is not of the expected {0} type. Another component might be using the same key to store in the cache.", typeof(T).FullName)); } } }
This can be used on any System.Web.Caching.Cache object.
Two Quick Tips on Web Setup in Visual Studio
How to change the name of the Virtual Directory
By default, when you create a Web Setup Project in Visual Studio, the name of the Virtual Directory it will deploy to is the name that you gave to the Web Setup Project. This can be changed in the property window of the "Web Application Folder" element. At the bottom of the property window, there is a property called VirtualDirectory. If you want the setup to install your application in a subdirectory, you can specify directories using the / character.
Simple, but took me some time to find it (I don’t use Web Setup Project very often I have to say).
When I deploy (or when I run the Setup), some of the files are not copied
I had this problem lately. My web project contained a .scv file, and the file was never copied to the IIS virtual directory when using the Deploy… option of Visual Studio, nor was it copied by the Web Setup Project. Actually, this has to do with the Build Action property of the file. If it is set to "None", the file will not be copied. It has to be set to "Content" for the file to be copied as is.
When I create a .svc file in my projects, I generally select "Text File" and name it with svc extension. By default, text files have their Build Action set to "None".
Here are two links to articles that contain very useful information on Web Setup Projects:
