<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="http://forums.asp.net/utility/FeedStylesheets/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/"><channel><title>Architecture</title><link>http://forums.asp.net/16.aspx</link><description>Discuss and debate ASP.NET application designs. &lt;a href="http://aspadvice.com/SignUp/list.aspx?l=8&amp;c=17" target="_blank"&gt;Email List&lt;/a&gt;</description><dc:language>en</dc:language><generator>CommunityServer 2007 SP1 (Build: 20510.895)</generator><item><title>Re: .NET Pet Shop 4.0 Caching</title><link>http://forums.asp.net/thread/3270849.aspx</link><pubDate>Thu, 02 Jul 2009 09:17:06 GMT</pubDate><guid isPermaLink="false">4c671506-2930-414c-a40b-8bf57ded5924:3270849</guid><dc:creator>vivek_iit</dc:creator><slash:comments>0</slash:comments><comments>http://forums.asp.net/thread/3270849.aspx</comments><wfw:commentRss>http://forums.asp.net/commentrss.aspx?SectionID=16&amp;PostID=3270849</wfw:commentRss><description>&lt;p&gt;Caching will definitely improve performance, but the actual tests would depend on how much load you have on the server, how complicated or time consuming your queries are etc etc. Simply testing caching on a standaloen server with one request will not help you much. The concept is simple: instead of fetching from the DB the code will fetch the data from the server memory, and the time difference would be the time your app would have taken to get the data from the DB.&lt;/p&gt;&lt;p&gt;Also, try to use SqlCacheDependency to invalidate the cache based on DB changes, so that you dont need to set any time limits on the cached data expiry. It will automatically happen once a record in the &amp;quot;monitored&amp;quot; DB table changes.&lt;/p&gt;&lt;p&gt;HTH,&lt;/p&gt;&lt;p&gt;Vivek&lt;br /&gt;&lt;/p&gt;</description></item><item><title>.NET Pet Shop 4.0 Caching</title><link>http://forums.asp.net/thread/3261012.aspx</link><pubDate>Fri, 26 Jun 2009 20:36:28 GMT</pubDate><guid isPermaLink="false">4c671506-2930-414c-a40b-8bf57ded5924:3261012</guid><dc:creator>kliszaq</dc:creator><slash:comments>0</slash:comments><comments>http://forums.asp.net/thread/3261012.aspx</comments><wfw:commentRss>http://forums.asp.net/commentrss.aspx?SectionID=16&amp;PostID=3261012</wfw:commentRss><description>&lt;p&gt;Hello everyone,&lt;/p&gt;&lt;p&gt;I was wondering how the caching in the .NET Pet Shop will increase the speed of the app. I was truly amazed with the results. The code below explains my test approach. The class offering the Cache storage is based on the Pet Shop example.&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;
using System;&lt;br /&gt;using System.Data;&lt;br /&gt;using System.Data.SqlClient;&lt;br /&gt;&lt;br /&gt;namespace ConsoleApplication1&lt;br /&gt;{&lt;br /&gt;   class Program&lt;br /&gt;   {&lt;br /&gt;      static void Main(string[] args) {&lt;br /&gt;&lt;br /&gt;         int[] lengths = PrepareParamsArray();&lt;br /&gt;&lt;br /&gt;         DateTime start = DateTime.Now;&lt;br /&gt;         for (var t1 = 0; t1 &amp;lt; lengths.Length; t1++) {&lt;br /&gt;            GetWithCache(lengths[t1]);&lt;br /&gt;         }&lt;br /&gt;         DateTime stop = DateTime.Now;&lt;br /&gt;         TimeSpan test1 = stop - start;&lt;br /&gt;         Console.WriteLine(&amp;quot;Test with cache time: {0} ms&amp;quot;, test1.TotalMilliseconds);&lt;br /&gt;&lt;br /&gt;         start = DateTime.Now;&lt;br /&gt;         for (var t2 = 0; t2 &amp;lt; lengths.Length; t2++) {&lt;br /&gt;            GetWithoutCache(lengths[t2]);&lt;br /&gt;         }&lt;br /&gt;         stop = DateTime.Now;&lt;br /&gt;         TimeSpan test2 = stop - start;&lt;br /&gt;         Console.WriteLine(&amp;quot;Test without cache time: {0} ms&amp;quot;, test2.TotalMilliseconds);&lt;br /&gt;&lt;br /&gt;         start = DateTime.Now;&lt;br /&gt;         for (var t3 = 0; t3 &amp;lt; lengths.Length; t3++) {&lt;br /&gt;            GetWithCacheLonger(lengths[t3]);&lt;br /&gt;         }&lt;br /&gt;         stop = DateTime.Now;&lt;br /&gt;         TimeSpan test3 = stop - start;&lt;br /&gt;         Console.WriteLine(&amp;quot;Test with cache long time: {0} ms&amp;quot;, test3.TotalMilliseconds);&lt;br /&gt;&lt;br /&gt;         start = DateTime.Now;&lt;br /&gt;         for (var t2 = 0; t2 &amp;lt; lengths.Length; t2++) {&lt;br /&gt;            GetWithoutCacheLonger(lengths[t2]);&lt;br /&gt;         }&lt;br /&gt;         stop = DateTime.Now;&lt;br /&gt;         TimeSpan test4 = stop - start;&lt;br /&gt;         Console.WriteLine(&amp;quot;Test without cache long time: {0} ms&amp;quot;, test4.TotalMilliseconds);&lt;br /&gt;&lt;br /&gt;         Console.ReadKey();&lt;br /&gt;      }&lt;br /&gt;&lt;br /&gt;      private static SqlParameter[] GetWithCache(int i) {&lt;br /&gt;         SqlParameter[] parms = TestSqlParams.GetParameters(&amp;quot;TEST2&amp;quot; + i);&lt;br /&gt;&lt;br /&gt;         if (parms == null) {&lt;br /&gt;            parms = Get2Params(i);&lt;br /&gt;            TestSqlParams.StoreParameters(&amp;quot;TEST2&amp;quot; + i, parms);&lt;br /&gt;         }&lt;br /&gt;&lt;br /&gt;         return parms;&lt;br /&gt;      }&lt;br /&gt;&lt;br /&gt;      private static SqlParameter[] GetWithoutCache(int i) {&lt;br /&gt;         return Get2Params(i);&lt;br /&gt;      }&lt;br /&gt;      &lt;br /&gt;      private static SqlParameter[] GetWithCacheLonger(int i) {&lt;br /&gt;         SqlParameter[] parms = TestSqlParams.GetParameters(&amp;quot;TEST10&amp;quot; + i);&lt;br /&gt;&lt;br /&gt;         if (parms == null) {&lt;br /&gt;            parms = Get10Params(i);&lt;br /&gt;            TestSqlParams.StoreParameters(&amp;quot;TEST10&amp;quot; + i, parms);&lt;br /&gt;         }&lt;br /&gt;&lt;br /&gt;         return parms;&lt;br /&gt;      }&lt;br /&gt;&lt;br /&gt;      private static SqlParameter[] GetWithoutCacheLonger(int i) {&lt;br /&gt;         return Get10Params(i);&lt;br /&gt;      }&lt;br /&gt;&lt;br /&gt;      private static SqlParameter[] Get2Params(int i) {&lt;br /&gt;         var parms = new[] {&lt;br /&gt;					new SqlParameter(&amp;quot;@Quantity&amp;quot; + i, SqlDbType.Int),&lt;br /&gt;					new SqlParameter(&amp;quot;@ItemId&amp;quot; + i, SqlDbType.VarChar, 10)};&lt;br /&gt;&lt;br /&gt;         return parms;&lt;br /&gt;      }&lt;br /&gt;&lt;br /&gt;      private static SqlParameter[] Get10Params(int i) {&lt;br /&gt;         var parms = new[] {&lt;br /&gt;					new SqlParameter(&amp;quot;@Quantity&amp;quot; + i, SqlDbType.Int),&lt;br /&gt;					new SqlParameter(&amp;quot;@ItemId&amp;quot; + i, SqlDbType.VarChar, 10),&lt;br /&gt;               new SqlParameter(&amp;quot;@Desc&amp;quot; + i, SqlDbType.Char, 120),&lt;br /&gt;               new SqlParameter(&amp;quot;@Amount&amp;quot; + i, SqlDbType.Int),&lt;br /&gt;               new SqlParameter(&amp;quot;@Price&amp;quot; + i, SqlDbType.Money),&lt;br /&gt;               new SqlParameter(&amp;quot;@ShipNo&amp;quot; + i, SqlDbType.VarChar, 40),&lt;br /&gt;               new SqlParameter(&amp;quot;@ShipPlace&amp;quot; + i, SqlDbType.NChar, 30),&lt;br /&gt;               new SqlParameter(&amp;quot;@Available&amp;quot; + i, SqlDbType.Bit),&lt;br /&gt;               new SqlParameter(&amp;quot;@CanSellPlace&amp;quot; + i, SqlDbType.NText, 1024),&lt;br /&gt;               new SqlParameter(&amp;quot;@Guid&amp;quot; + i, SqlDbType.UniqueIdentifier)&lt;br /&gt;         };&lt;br /&gt;&lt;br /&gt;         return parms;&lt;br /&gt;      }&lt;br /&gt;&lt;br /&gt;      public static int[] PrepareParamsArray() {&lt;br /&gt;         const int arrLength = 1000000;&lt;br /&gt;         var parmsLen = new int[arrLength];&lt;br /&gt;&lt;br /&gt;         for (var i = 0; i &amp;lt; arrLength; i++) {&lt;br /&gt;            parmsLen[i] = i % 45;&lt;br /&gt;         }&lt;br /&gt;         return parmsLen;&lt;br /&gt;      }&lt;br /&gt;&lt;br /&gt;   }&lt;br /&gt;}&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt; And the class that based on the Hashtable implementation.&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;using System;&lt;br /&gt;using System.Collections;&lt;br /&gt;using System.Data.SqlClient;&lt;br /&gt;&lt;br /&gt;namespace ConsoleApplication1&lt;br /&gt;{&lt;br /&gt;class TestSqlParams&lt;br /&gt;{&lt;br /&gt;private static Hashtable cache = Hashtable.Synchronized(new Hashtable());&lt;br /&gt;&lt;br /&gt;public static void StoreParameters(string key, params SqlParameter[] parameters)&lt;br /&gt;{&lt;br /&gt;cache[key] = parameters;&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;public static SqlParameter[] GetParameters(string key)&lt;br /&gt;{&lt;br /&gt;var cached = (SqlParameter[]) cache[key];&lt;br /&gt;if (cached == null) return null;&lt;br /&gt;&lt;br /&gt;var cloned = new SqlParameter[cached.Length];&lt;br /&gt;&lt;br /&gt;for(int index = 0, cacheLen = cached.Length; index &amp;lt; cacheLen; index++)&lt;br /&gt;{&lt;br /&gt;cloned[index] = ((SqlParameter) ((ICloneable) cached[index]).Clone());&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;return cloned;&lt;br /&gt;}&lt;br /&gt;}&lt;br /&gt;}&lt;/p&gt;&lt;p&gt;&lt;br /&gt; &lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt; &lt;br /&gt;&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;Result:&lt;/p&gt;&lt;p&gt;Test with cache time: 3432 ms&lt;br /&gt;Test without cache time: 1070 ms&lt;br /&gt;Test with cache long time: 16982 ms&lt;br /&gt;Test without cache long time: 6349 ms&lt;/p&gt;&lt;p&gt;&lt;br /&gt;&lt;/p&gt;&lt;p&gt;So is there any error in my way of thinking or just in the test implementation?&lt;br /&gt;&lt;/p&gt;</description></item></channel></rss>