Overview: MSDataShape 이용하여 관계형으로 Table 2개를 캐싱해본다.
1.Net Framework 4.6 이상에서 설치가능
2.설치
NuGet 패캐지 관리에서 설치 및 참조 추가 확인
3.참조 확인
4. Simple Source
1 2 3 4 5 |
OleDbDataAdapter adapter = new OleDbDataAdapter("SHAPE {SELECT Account_ID, Account_Number, Balance FROM [Bank].[dbo].[Account]} " + "APPEND ({SELECT IDX, Account_ID, Amount, New_Balance, Transaction_Date FROM [Bank].[dbo].[Transaction]} AS Transaction " + "RELATE Account_ID TO Account_ID)", connectionString); |
5. Process diagram
6.DB
7.WebService Call (DataSet)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
using System.Web.Services; using System.Data; namespace WebService1 { public class Service1 : System.Web.Services.WebService { [WebMethod()] public DataSet GetProductCatalog() { // Return the complete DataSet (from the cache, if possible). //return Function.GetCache(); return CacheClass.GetCustomerDataSet(); } [WebMethod()] public string[] GetProductList() { // Get the customer table (from the cache if possible). DataTable dt = CacheClass.GetCustomerDataSet().Tables[0]; // Create an array that will hold the name of each customer. string[] names = new string[dt.Rows.Count]; // Fill the array. int i = 0; foreach (DataRow row in dt.Rows) { names[i] = row["FirstName"].ToString(); i += 1; } return names; } } } |
8.Cache Insert (DataSet)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 |
using System; using System.Data; using System.Data.SqlClient; using System.Runtime.Caching; namespace WebService1 { public class CacheClass { private static string _key = "Customers"; private static readonly MemoryCache _cache = MemoryCache.Default; //private static string connectionString = "Data Source=localhost;" + "Initial Catalog=Northwind;user ID=sa"; private static string connectionString = "Provider=MSDataShape;Data Provider=SQLOLEDB.1;Password=*******;Persist Security Info=True;User ID=MYID;Initial Catalog=MYDB;Data Source=MYCOM"; public static DataSet GetCustomerDataSet() { if (!(_cache.Get(_key) is DataSet ds)) { // Recreate the item. ds = new DataSet(); // Execute the command. try { using (OleDbConnection conn = new OleDbConnection(connectionString)) { OleDbDataAdapter adapter = new OleDbDataAdapter("SHAPE {SELECT Account_ID, Account_Number, Balance FROM [Bank].[dbo].[Account]} " + "APPEND ({SELECT IDX, Account_ID, Amount, New_Balance, Transaction_Date FROM [Bank].[dbo].[Transaction]} AS Transaction " + "RELATE Account_ID TO Account_ID)", connectionString); //conn.Open(); adapter.Fill(ds, "Accounts"); adapter.Dispose(); conn.Close(); conn.Dispose(); } var cacheItemPolicy = new CacheItemPolicy() { //Set your Cache expiration. AbsoluteExpiration = DateTime.Now.AddMinutes(60) }; _cache.Add(_key, ds, cacheItemPolicy); } catch (Exception err) { System.Diagnostics.Debug.WriteLine(err.ToString()); } } return _cache.Get(_key) as DataSet; } } } |