Implementing Simple Cache for CodeSmith and NetTiers (ASP.NET 2.0)

In this article, I will show a simple way to implement Cache for a GetData type call in the SqlClient layer of the generated code. I won’t go through all the details of getting to this point but will show the code and briefly explain how it works. I’m new to CodeSmith so I’m sure there is a better way to solve this problem, but for now, this is one technique that works.

Just to give a lay of the land, my project is called peterkellnerblog and it has several subprojects that each create their own dll’s. They are:

  • c:\..\..\peterkellnerblog.Website
  • peterkellnerblog.Data
  • peterkellnerblog.Data.SqlClient
  • peterkellnerblog.Entities
  • peterkellnerblog.Web

    The project we are going to focus on is peterkelnerblog.Data.SqlClient. In this project, there already is a method that gets data with the following signature (this file is SqlWpCommentProviderBase.generate.cs).

     public override peterkellnerblog.Entities.TList<WpComments>

                GetByCommentPostID(

                TransactionManager transactionManager,

                System.Int32 commentPostID, int start,

                int pageLength, out int count)

            {

     

    This file you are not meant to change (notice that is has the word "generated" in the name). The file you can modify, which is SqlWpCommentProvider.cs. What you essentially do is call the above file and add Cache to the end of the name. That way, if you want a Cached method, you use the new name instead. Here is the file below.

     #region Using directives

     

    using System;

    using System.Data;

    using System.Collections;

    using System.Diagnostics;

    using Microsoft.Practices.EnterpriseLibrary.Data;

    using System.ComponentModel;

     

    using peterkellnerblog.Entities;

    using peterkellnerblog.Data;

    using System.Web.Caching;

    using System.Web;

     

    #endregion

     

    namespace peterkellnerblog.Data.SqlClient

    {

        ///<summary>

        /// This class is the SqlClient Data Access Logic Component implementation for the <see cref="WpComments"/> entity.

        ///</summary>

        [DataObject]

        [CLSCompliant(true)]

        public partial class SqlWpCommentsProvider: SqlWpCommentsProviderBase

        {

            /// <summary>

            /// Creates a new <see cref="SqlWpCommentsProvider"/> instance.

            /// Uses connection string to connect to datasource.

            /// </summary>

            /// <param name="connectionString">The connection string to the database.</param>

            /// <param name="useStoredProcedure">A boolean value that indicates if we use the stored procedures or embedded queries.</param>

            /// <param name="providerInvariantName">Name of the invariant provider use by the DbProviderFactory.</param>

            public SqlWpCommentsProvider(string connectionString, bool useStoredProcedure, string providerInvariantName): base(connectionString, useStoredProcedure, providerInvariantName){}

     

            /// <summary>

            ///

            /// </summary>

            /// <param name="transactionManager"></param>

            /// <param name="commentPostID"></param>

            /// <param name="start"></param>

            /// <param name="pageLength"></param>

            /// <param name="count"></param>

            /// <returns></returns>

            public override peterkellnerblog.Entities.TList<WpComments>

                GetByCommentPostIDCache(TransactionManager transactionManager,

                System.Int32 commentPostID, int start, int pageLength,

                out int count)

            {

                int newCnt;

                string cacheKey = "WpComments_GetByCommentPostID" +

                    commentPostID.ToString() + "_" +

                    start.To
    String() + "_" +

                    pageLength.ToString();

     

                TList<WpComments> tListWpComments = null;

                if (HttpContext.Current.Cache[cacheKey] != null)

                {

                    tListWpComments =

                        (TList<WpComments>)HttpContext.Current.Cache[cacheKey];

                    count = tListWpComments.Count;

                }

                else

                {

                    tListWpComments = base.GetByCommentPostID

                        (transactionManager, commentPostID, start, pageLength,

                        out newCnt);

                    HttpContext.Current.Cache.Insert(cacheKey, tListWpComments,

                        null, DateTime.Now.AddSeconds(15), TimeSpan.FromDays(0));

                    count = newCnt;

                }

                count = tListWpComments.Count;

                return tListWpComments;

            }

        }

    }

    The above method simply use the built in ASP.NET Cache class.

About Peter Kellner

Follow me:


Comments

  1. this is embarrasing. I should have grabbed the cache key first (HttpContext.Current.Cache[cacheKey]). It could expire after the if. What was I thinking! and no concurrency check either!!! shame on me :(

  2. Try to use an open source alternative, SmartCode, the engine is more powerfull than codesmith and is compatible..

    http://www.kontac.net/WelcomeSC.aspx

Your Comments

*

Protected with IP Blacklist CloudIP Blacklist Cloud

Follow

Get every new post delivered to your Inbox

Join other followers: