{"id":8409,"date":"2023-02-07T15:14:00","date_gmt":"2023-02-07T06:14:00","guid":{"rendered":"https:\/\/www.auctionpro.co.kr\/?p=8409"},"modified":"2023-02-07T18:02:01","modified_gmt":"2023-02-07T09:02:01","slug":"memory-cache-datatable","status":"publish","type":"post","link":"https:\/\/www.auctionpro.co.kr\/?p=8409","title":{"rendered":"Memory Cache (DataTable)"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">Overview :  DataTable \ub85c Cache \ud654 \ud574\ubcf4\uae30<br><\/h2>\n\n\n\n<h2 class=\"wp-block-heading\">1.Net Framework 4.6 \uc774\uc0c1\uc5d0\uc11c \uc124\uce58\uac00\ub2a5<\/h2>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/www.auctionpro.co.kr\/wp-content\/uploads\/2023\/01\/image-9.png\"><img loading=\"lazy\" decoding=\"async\" width=\"910\" height=\"577\" src=\"https:\/\/www.auctionpro.co.kr\/wp-content\/uploads\/2023\/01\/image-9.png\" alt=\"\" class=\"wp-image-8234\" srcset=\"https:\/\/www.auctionpro.co.kr\/wp-content\/uploads\/2023\/01\/image-9.png 910w, https:\/\/www.auctionpro.co.kr\/wp-content\/uploads\/2023\/01\/image-9-300x190.png 300w, https:\/\/www.auctionpro.co.kr\/wp-content\/uploads\/2023\/01\/image-9-768x487.png 768w, https:\/\/www.auctionpro.co.kr\/wp-content\/uploads\/2023\/01\/image-9-660x418.png 660w\" sizes=\"auto, (max-width: 910px) 100vw, 910px\" \/><\/a><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">2.\uc124\uce58<\/h2>\n\n\n\n<h3 class=\"wp-block-heading\">NuGet \ud328\uce90\uc9c0 \uad00\ub9ac\uc5d0\uc11c \uc124\uce58 \ubc0f \ucc38\uc870 \ucd94\uac00 \ud655\uc778<\/h3>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/www.auctionpro.co.kr\/wp-content\/uploads\/2023\/01\/image-8.png\"><img loading=\"lazy\" decoding=\"async\" width=\"902\" height=\"602\" src=\"https:\/\/www.auctionpro.co.kr\/wp-content\/uploads\/2023\/01\/image-8.png\" alt=\"\" class=\"wp-image-8233\" srcset=\"https:\/\/www.auctionpro.co.kr\/wp-content\/uploads\/2023\/01\/image-8.png 902w, https:\/\/www.auctionpro.co.kr\/wp-content\/uploads\/2023\/01\/image-8-300x200.png 300w, https:\/\/www.auctionpro.co.kr\/wp-content\/uploads\/2023\/01\/image-8-768x513.png 768w, https:\/\/www.auctionpro.co.kr\/wp-content\/uploads\/2023\/01\/image-8-660x440.png 660w\" sizes=\"auto, (max-width: 902px) 100vw, 902px\" \/><\/a><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">3.\ucc38\uc870 \ud655\uc778<\/h2>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/www.auctionpro.co.kr\/wp-content\/uploads\/2023\/01\/image-11.png\"><img loading=\"lazy\" decoding=\"async\" width=\"327\" height=\"531\" src=\"https:\/\/www.auctionpro.co.kr\/wp-content\/uploads\/2023\/01\/image-11.png\" alt=\"\" class=\"wp-image-8243\" srcset=\"https:\/\/www.auctionpro.co.kr\/wp-content\/uploads\/2023\/01\/image-11.png 327w, https:\/\/www.auctionpro.co.kr\/wp-content\/uploads\/2023\/01\/image-11-185x300.png 185w\" sizes=\"auto, (max-width: 327px) 100vw, 327px\" \/><\/a><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">4. Simple Source<\/h2>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:c# decode:true \" title=\"System.Runtime.Caching.MemoryCache\">var cache = new System.Runtime.Caching.MemoryCache(\"MyTestCache\");\ncache[\"ObjectList\"] = list;                 \/\/ add\nlist = (List&lt;object&gt;)cache[\"ObjectList\"]; \/\/ retrieve\ncache.Remove(\"ObjectList\");                 \/\/ remove<\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">5. Process diagram<\/h2>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/www.auctionpro.co.kr\/wp-content\/uploads\/2023\/02\/image-20.png\"><img loading=\"lazy\" decoding=\"async\" width=\"446\" height=\"726\" src=\"https:\/\/www.auctionpro.co.kr\/wp-content\/uploads\/2023\/02\/image-20.png\" alt=\"\" class=\"wp-image-8412\" srcset=\"https:\/\/www.auctionpro.co.kr\/wp-content\/uploads\/2023\/02\/image-20.png 446w, https:\/\/www.auctionpro.co.kr\/wp-content\/uploads\/2023\/02\/image-20-184x300.png 184w\" sizes=\"auto, (max-width: 446px) 100vw, 446px\" \/><\/a><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">6.DB<\/h2>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/www.auctionpro.co.kr\/wp-content\/uploads\/2023\/01\/image-6.png\"><img loading=\"lazy\" decoding=\"async\" width=\"388\" height=\"213\" src=\"https:\/\/www.auctionpro.co.kr\/wp-content\/uploads\/2023\/01\/image-6.png\" alt=\"\" class=\"wp-image-8219\" srcset=\"https:\/\/www.auctionpro.co.kr\/wp-content\/uploads\/2023\/01\/image-6.png 388w, https:\/\/www.auctionpro.co.kr\/wp-content\/uploads\/2023\/01\/image-6-300x165.png 300w\" sizes=\"auto, (max-width: 388px) 100vw, 388px\" \/><\/a><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">7. Insert Query<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">\ub9c1\ud06c : \ub9c1\ud06c :\u00a0<a href=\"https:\/\/github.com\/iammukeshm\/RepositoryPatternWithCachingAndHangfire\/blob\/master\/SampleCustomers.sql\">https:\/\/github.com\/iammukeshm\/RepositoryPatternWithCachingAndHangfire\/blob\/master\/SampleCustomers.sql<\/a><\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:tsql decode:true \">insert into Customers (FirstName, LastName, Contact, Email, DateOfBirth) values ('Caressa', 'Walsh', '868-449-3013', 'cwalsh0@google.es', '3\/23\/1996');\ninsert into Customers (FirstName, LastName, Contact, Email, DateOfBirth) values ('Rhea', 'Andrichuk', '733-892-8926', 'randrichuk1@elpais.com', '12\/11\/1997');\ninsert into Customers (FirstName, LastName, Contact, Email, DateOfBirth) values ('Amelia', 'Epp', '247-422-8619', 'aepp2@spiegel.de', '7\/16\/1999');\ninsert into Customers (FirstName, LastName, Contact, Email, DateOfBirth) values ('Cesaro', 'Goodwin', '606-804-3382', 'cgoodwin3@salon.com', '8\/16\/1990');\ninsert into Customers (FirstName, LastName, Contact, Email, DateOfBirth) values ('Lira', 'Crookall', '320-145-2453', 'lcrookall4@g.co', '9\/14\/1996');\ninsert into Customers (FirstName, LastName, Contact, Email, DateOfBirth) values ('Tye', 'Collin', '548-245-2593', 'tcollin5@dailymail.co.uk', '4\/9\/1993');\ninsert into Customers (FirstName, LastName, Contact, Email, DateOfBirth) values ('Clare', 'Kelson', '324-249-1362', 'ckelson6@amazon.co.jp', '11\/26\/1992');\ninsert into Customers (FirstName, LastName, Contact, Email, DateOfBirth) values ('Wilmer', 'Swaite', '657-532-9884', 'wswaite7@addthis.com', '9\/22\/1990');\ninsert into Customers (FirstName, LastName, Contact, Email, DateOfBirth) values ('Delphine', 'Gasson', '392-230-1556', 'dgasson8@51.la', '8\/7\/1994');\ninsert into Customers (FirstName, LastName, Contact, Email, DateOfBirth) values ('Manon', 'Yeowell', '789-838-9532', 'myeowell9@jimdo.com', '10\/11\/1999');<\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">8.WebService Call (DataTable)<\/h2>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:c# decode:true \">using System;\nusing System.Collections.Generic;\nusing System.Linq;\nusing System.Web;\nusing System.Web.Services;\nusing System.Data;\nusing System.Data.SqlClient;\n\nnamespace WebService1\n{   \n     public class Service1 : System.Web.Services.WebService\n    {\n\n        [WebMethod()]\n        public DataTable GetProductCatalog()\n        {\n            \/\/ Return the complete DataSet (from the cache, if possible).         \n            \/\/return Function.GetCache();\n            return CacheClass.GetCustomerDataSet();\n        }\n\n        [WebMethod()]\n        public string[] GetProductList()\n        {\n            \/\/ Get the customer table (from the cache if possible).         \n            DataTable dt = CacheClass.GetCustomerDataSet();\n            \/\/ Create an array that will hold the name of each customer.         \n            string[] names = new string[dt.Rows.Count];\n            \/\/ Fill the array.         \n            int i = 0;\n\n            foreach (DataRow row in dt.Rows)\n            {\n                names[i] = row[\"FirstName\"].ToString();\n                i += 1;\n            }\n            return names;\n        }\n    }\n}<\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">9.Cache Insert (DataSet)<\/h2>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:c# decode:true \">using System;\nusing System.Collections.Generic;\nusing System.Linq;\nusing System.Web;\nusing System.Data;\nusing System.Data.SqlClient;\nusing System.Runtime.Caching;\n\nnamespace WebService1\n{\n    public class CacheClass\n    {\n        private static string _key = \"Customers\";\n        private static readonly MemoryCache _cache = MemoryCache.Default;\n\n        \/\/private static string connectionString = \"Data Source=localhost;\" + \"Initial Catalog=Northwind;user ID=sa\";\n        private static string connectionString = \"Server=SQLOLEDB.1;Password=*******;Persist Security Info=True;User ID=MYID;Initial Catalog=MYDB;Data Source=MYCOM\";\n\n        public static DataTable GetCustomerDataSet()\n        {\n            \/\/ Check for cached item.         \n            \/\/DataSet ds = HttpContext.Current.Cache[\"Products\"] as DataSet;\n            \/\/_cache.Remove(_key);\n            \/\/if (!(_cache.Get(_key) is DataSet ds))\n            if (!_cache.Contains(_key))\n            {\n                \/\/ Recreate the item.             \n                string SQL = \"SELECT * FROM Customers\";\n                \/\/ Create ADO.NET objects.             \n                SqlConnection con = new SqlConnection(connectionString);\n                SqlCommand com = new SqlCommand(SQL, con);\n                SqlDataAdapter adapter = new SqlDataAdapter(com);\n\n                DataTable ds = new DataTable();\n                ds.TableName = _key;\n\n                \/\/ Execute the command.             \n                try\n                {\n                    con.Open();\n                    adapter.Fill(ds);\n                    \/\/ Store the item in the cache (for 60 Minutes).                 \n                    \/\/Do what you need to do here. Database Interaction, Serialization,etc.\n                    var cacheItemPolicy = new CacheItemPolicy()\n                    {\n                        \/\/Set your Cache expiration.\n                        AbsoluteExpiration = DateTime.Now.AddMinutes(60)\n                    };\n\n                    _cache.Add(_key, ds, cacheItemPolicy);\n\n                }\n                catch (Exception err)\n                {\n                    System.Diagnostics.Debug.WriteLine(err.ToString());\n                }\n                finally\n                {\n                    con.Close();\n                }\n            }\n            return _cache.Get(_key) as DataTable;\n        }\n\n    }\n}<\/pre><\/div>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/www.auctionpro.co.kr\/wp-content\/uploads\/2023\/02\/image-19.png\"><img loading=\"lazy\" decoding=\"async\" width=\"802\" height=\"418\" src=\"https:\/\/www.auctionpro.co.kr\/wp-content\/uploads\/2023\/02\/image-19.png\" alt=\"\" class=\"wp-image-8393\" srcset=\"https:\/\/www.auctionpro.co.kr\/wp-content\/uploads\/2023\/02\/image-19.png 802w, https:\/\/www.auctionpro.co.kr\/wp-content\/uploads\/2023\/02\/image-19-300x156.png 300w, https:\/\/www.auctionpro.co.kr\/wp-content\/uploads\/2023\/02\/image-19-768x400.png 768w, https:\/\/www.auctionpro.co.kr\/wp-content\/uploads\/2023\/02\/image-19-660x344.png 660w\" sizes=\"auto, (max-width: 802px) 100vw, 802px\" \/><\/a><figcaption class=\"wp-element-caption\">DataSet<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">10.\ud654\uba74<\/h2>\n\n\n\n<figure class=\"wp-block-image size-full\"><a href=\"https:\/\/www.auctionpro.co.kr\/wp-content\/uploads\/2023\/01\/image-7.png\"><img loading=\"lazy\" decoding=\"async\" width=\"527\" height=\"177\" src=\"https:\/\/www.auctionpro.co.kr\/wp-content\/uploads\/2023\/01\/image-7.png\" alt=\"\" class=\"wp-image-8220\" srcset=\"https:\/\/www.auctionpro.co.kr\/wp-content\/uploads\/2023\/01\/image-7.png 527w, https:\/\/www.auctionpro.co.kr\/wp-content\/uploads\/2023\/01\/image-7-300x101.png 300w\" sizes=\"auto, (max-width: 527px) 100vw, 527px\" \/><\/a><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">11.Result<\/h2>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:xhtml decode:true \">&lt;ArrayOfString xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\" xmlns:xsd=\"http:\/\/www.w3.org\/2001\/XMLSchema\" xmlns=\"http:\/\/tempuri.org\/\"&gt;\n&lt;string&gt;Caressa&lt;\/string&gt;\n&lt;string&gt;Rhea&lt;\/string&gt;\n&lt;string&gt;Amelia&lt;\/string&gt;\n&lt;string&gt;Cesaro&lt;\/string&gt;\n&lt;string&gt;Lira&lt;\/string&gt;\n&lt;string&gt;Tye&lt;\/string&gt;\n&lt;string&gt;Clare&lt;\/string&gt;\n&lt;string&gt;Wilmer&lt;\/string&gt;\n&lt;string&gt;Delphine&lt;\/string&gt;\n&lt;string&gt;Manon&lt;\/string&gt;\n&lt;string&gt;Bev&lt;\/string&gt;\n&lt;string&gt;Lodovico&lt;\/string&gt;\n<\/pre><\/div>\n\n\n\n<h2 class=\"wp-block-heading\">12.Cache Insert advance<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Multi Cache, stored procedure<\/strong><\/p>\n\n\n\n<div class=\"wp-block-urvanov-syntax-highlighter-code-block\"><pre class=\"lang:vim decode:true \">using System;\nusing System.Data;\nusing System.Data.SqlClient;\nusing System.Runtime.Caching;\nusing System.Configuration;\n\nnamespace ConsoleHostRule\n{\n    class CacheClass\n    {   \n        private static readonly MemoryCache _cache = MemoryCache.Default;\n\n        private static double CacheTime = double.Parse(ConfigurationManager.AppSettings[\"CacheTime\"]);\/\/CacheTime\n        private static string connectionString = ConfigurationManager.ConnectionStrings[\"Customers\"].ToString();\/\/connectionString\n\n        public static DataTable GetCacheData(string _key)\n        {\n            if (!_cache.Contains(_key))\n            {\n                try\n                {\n                    using (SqlConnection conn = new SqlConnection(connectionString))\n                    {\n                        SqlCommand cmd = conn.CreateCommand();\n                        cmd.Connection = conn;\n                        cmd.CommandType = CommandType.StoredProcedure;\n                        cmd.CommandText = \"cache_\" + _key;  \/\/StoredProcedure Name\n                        SqlDataAdapter adapter = new SqlDataAdapter\n                        {\n                            SelectCommand = cmd\n                        };\n\n                        conn.Open();\n\n                        adapter.Fill(ds);\n                        cmd.Dispose();\n\n                        conn.Close();\n                        conn.Dispose();\n                    }\n\n                   var cacheItemPolicy = new CacheItemPolicy()\n                    {\n                        \/\/Set your Cache expiration.\n                        AbsoluteExpiration = DateTime.Now.AddMinutes(60)\n                    };\n \n                    _cache.Add(_key, ds, cacheItemPolicy);\n\n                }\n                catch (Exception err)\n                {\n                    System.Diagnostics.Debug.WriteLine(err.ToString());\n                }\n            }\n\n            return _cache.Get(_key) as DataTable;\n        }\n\n    }\n}<\/pre><\/div>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n\n\n\n<p class=\"wp-block-paragraph\">source&nbsp; :&nbsp;https:\/\/stackoverflow.com\/questions\/41684213\/looking-for-a-very-simple-cache-example?utm_medium=organic&amp;utm_source=google_rich_qa&amp;utm_campaign=google_rich_qa<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">\ucc38\uc870 : https:\/\/swconsulting.tistory.com\/801<\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>Overview : DataTable \ub85c Cache \ud654 \ud574\ubcf4\uae30 1.Net Framework 4.6 \uc774\uc0c1\uc5d0\uc11c \uc124\uce58\uac00\ub2a5 2.\uc124\uce58 NuGet \ud328\uce90\uc9c0 \uad00\ub9ac\uc5d0\uc11c \uc124\uce58 \ubc0f \ucc38\uc870 \ucd94\uac00 \ud655\uc778 3.\ucc38\uc870 \ud655\uc778 4. Simple <a class=\"mh-excerpt-more\" href=\"https:\/\/www.auctionpro.co.kr\/?p=8409\" title=\"Memory Cache (DataTable)\">[&#8230;]<\/a><\/p>\n<\/div>","protected":false},"author":6,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[237],"tags":[],"class_list":["post-8409","post","type-post","status-publish","format-standard","hentry","category-memory-cache"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/www.auctionpro.co.kr\/index.php?rest_route=\/wp\/v2\/posts\/8409","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.auctionpro.co.kr\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.auctionpro.co.kr\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.auctionpro.co.kr\/index.php?rest_route=\/wp\/v2\/users\/6"}],"replies":[{"embeddable":true,"href":"https:\/\/www.auctionpro.co.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=8409"}],"version-history":[{"count":0,"href":"https:\/\/www.auctionpro.co.kr\/index.php?rest_route=\/wp\/v2\/posts\/8409\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.auctionpro.co.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=8409"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.auctionpro.co.kr\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=8409"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.auctionpro.co.kr\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=8409"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}