Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Programmatic Caching

    • 0
    • 1
    • 1
    • 0
    • 0
    • 0
    • 0
    • 0
    • 343
    Comment on it

    The first thing we need to understand is the need of caching. This can be understood by a very simple and most common usage of search functionality. For example we need to search all products that come under furniture by typing in one of the furniture product "Chair". So, if we type in "Chair" the expected result is to get all products resembling "Chair".


    The orthodox method to do it is to get typed in text, find its category and then display all related products of that category. So, once you type in "Chair" it will show you all related products, then you try to search "Bed" (that too comes under furniture) it will again get all the same products. But when you typed "Chair" the code will hit the database and bring you the result and when you typed in "Bed" it will again hit the database for the same result. So, we are getting same result while we are hitting database every-time and the performance time is negotiated.


    We can increase the performance by using caching. When a user types in "Chair" we hit the database and get all related results, we cache this programmatically. Now when user types in "Bed" we don't have to hit the database and will show the result from the cache itself. This can remarkably increase the search performance. The best way to do "Programmatic Caching" is to use Dictionary.


    **How to use Dictionary for Caching purpose**: We will take the above example and implement Dictionary. When a user search for chair it will hit the database to get all products for furniture, now before showing that to the user we will create a dictionary for furniture and all related products. Now if the user tries to search any other product we will check whether our dictionary contains the category if yes we will simply show the user the corresponding list otherwise hit the database and store that in the dictionary for further use.


    Code Snippet


    Dictionary<string,List<Products>> productsDict = new Dictionary<string,List<Products>>();
    
    public List<Products> GetRelatedProducts(SearchedText)
    {
    List<Products> relatedProducts = new List<Products>();
    string Category = GetCategory(SearchedText);
    if(productDict.ContainsKey(Category))
    {
    relatedProducts = productDict[Category];
    }
    else
    {
    relatedProducts = GetRelatedProductsFromDatabase(Category);
    productDict[Category] = relatedProducts;
    }
    return relatedProducts;
    }
    


    This small piece of code will do "Programmatic Caching" of the result and will be used in similar search options increasing the search performance and avoiding several database connections.


    Hoping this will help you optimizing your code. Happy Coding.....

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: