----------------------------------------------------------------------------------------------------------------------------

Caching concept for file storages that are static / read-only

In particular situations it might be practical to avoid retrieving information from the filestorage over and over again, but to use caching instead. This is only applicable ofcourse, once you are sure this is an acceptable solution for you case. Basically caching of information should only be used when the information in the store is 'static' data that is requested over and over again frequently. The word 'static' is used to describe the fact that the items in the filestorage will be considered read only (write once, read many). For example, if you would have a filestorage that contains a list of menu item pictures for your web application which you want to show to your customers for each request, it might be worth using caching.

The caching feature is scheduled for release 1.3.1 (following code can already be used using the latest version of the SVN repository in CodePlex if you want to experience using it)...

In the following static method you can see we are testing the speed difference between retrieving all items from a filestorage without, and with caching. First we retrieve all items from a certain filestorage a single time (we do so, to make the measurements more realistic; to avoid the harddisc from using its cache). Afterwards we repeat the retrieval of all items 'n' times without caching, and then 'n' times with caching. The time difference in seconds is measured for both scenario's and outputted on the console screen. Depending on the filestorage you're using the speed difference (the gain) can be quite high.


        public static void TestFileStorageCaching()
        {
            string filestorageName = @"your_file_storage";
            List<Guid> guids = FileStorageFacade.GetAllDataIdentifiersBasedUponFileStorageIndexFile(filestorageName);
            ICacheContainer cont = CacheContainerFactory.CreateNonExpiringCacheContainer();

            // first read all items from the cache, to prevent diffs in performance when starting the actual measurements
            foreach(var g in guids)
            {
                byte[] useless = FileStorageFacade.GetBytesData(filestorageName, g);
                FileStorageFacade.GetBytesData(filestorageName, g);
            }

            DateTime dt;

            dt = DateTime.Now;
            // read n times using non-caching
            for (int counter = 1; counter < 10; counter++)
            {
                foreach (var g in guids)
                {
                    byte[] useless = FileStorageFacade.GetBytesData(filestorageName, g);
                }
            }

            TimeSpan duration = DateTime.Now - dt;
            Console.WriteLine(duration.Seconds);

            dt = DateTime.Now;
            // read n times using caching
            for (int counter = 1; counter < 10; counter++)
            {
                foreach (var g in guids)
                {
                    byte[] useless = FileStorageCacherFacade.GetBytesData(filestorageName, g, cont);
                }
            }

            duration = DateTime.Now - dt;
            Console.WriteLine(duration.Seconds);

            // press enter to quit
            Console.ReadLine();
        }


ICacheContainer

The ICacheContainer is the object that contains the actual cache objects. We use an interface rather than hardcoded implementation, to enable different behaviours for different situations. Sometimes you would never want cache to become expired, but other times you want items to expire based on some conditions (like; the amount of items already in the cache, the 'age' of the cached data, or whatever other criteria you want to use). There are some default build-in CacheContainer sub types you can use, or when these do not meet your requirements, you can build your own.

Pro's of caching
  • Increased performance of your application.

Con's of caching
  • Memory consumption.

----------------------------------------------------------------------------------------------------------------------------

Last edited May 30, 2009 at 10:43 AM by barkgj, version 5

Comments

No comments yet.