Cache应用

来源:互联网 发布:黄庭坚书法知乎 编辑:程序博客网 时间:2024/05/22 10:42

1.首先从数据库中取数据,代码如下:

private const string CacheAllProduct = "MyProduct";        private const string ConnectionString = "";        private const string StoredProcedureName_GetProduct = "GetAllProduct";        private const string DataTableProductId = "ProductId";        private const string DataTableProductName = "ProductName";        private const string DataTableProductPrice = "ProductPrice";        public static DataTable GetAllProduct(out SqlCacheDependency sqlCacheDependency)        {            DataTable dt = new DataTable(CacheAllProduct);            using (SqlConnection sqlCon = new SqlConnection(ConnectionString))            {                SqlCommand sqlCmd = new SqlCommand(StoredProcedureName_GetProduct, sqlCon);                sqlCacheDependency = new SqlCacheDependency(sqlCmd);                sqlCmd.CommandType = CommandType.StoredProcedure;                try                {                    sqlCon.Open();                    SqlDataReader reader = sqlCmd.ExecuteReader();                    dt.Load(reader);                    reader.Close();                }                catch                {                    throw;                }            }            return dt;        }

2.下面是往Cache中塞值并且转换成为数据字典,如下:

在这里强调一点,对于HttpRuntime.Cache.Insert(string key, object value, CacheDependency dependencies);的第三个参数官方的解释是这样的:

dependencies:
             The file or cache key dependencies for the inserted object. When any dependency
             changes, the object becomes invalid and is removed from the cache. If there
             are no dependencies, this parameter contains null.

也就是说对于Cache,它存在一个内部的机制如果SqlDependency发生变化就会清空Cache。

public static Dictionary<string, Product> Products        {            get            {                if (HttpRuntime.Cache[CacheAllProduct] == null)                {                    SqlCacheDependency sqlCacheDependency;                    DataTable dt = GetAllProduct(out sqlCacheDependency);                    HttpRuntime.Cache.Insert(CacheAllProduct, ConvertToProductsDictionary(dt), sqlCacheDependency);                }                return HttpRuntime.Cache[CacheAllProduct] as Dictionary<string, Product>;            }        }

 

3.下面是转换的方法:

private static Dictionary<string, Product> ConvertToProductsDictionary(DataTable dataTable)        {            Dictionary<string, Product> productDictionary = new Dictionary<string, Product>();            foreach (var item in ConvertToProductList(dataTable))            {                if (!productDictionary.ContainsValue(item))                {                    productDictionary.Add(item.ProductId.ToUpper().Trim(), item);                }            }            return productDictionary;        }        private static List<Product> ConvertToProductList(DataTable dataTable)        {            List<Product> products = new List<Product>();            foreach (DataRow row in dataTable.Rows)            {                products.Add(ConvertToProduct(row));            }            return products;        }        private static Product ConvertToProduct(DataRow row)        {            Product result = new Product()            {                ProductId = row[DataTableProductId].ToString(),                ProductName = row[DataTableProductName].ToString(),            };            if (row[DataTableProductPrice] != DBNull.Value)            {                result.ProductPrice = Convert.ToDecimal(row[DataTableProductPrice]);            }            return result;        }    }


 

4.最后是数据库对应的实体:

public class Product    {        public string ProductId { get; set; }        public string ProductName { get; set; }        public decimal ProductPrice { get; set; }    }


 

5.由于SqlDependency只支持简单的查询语句,所以如果涉及多张表时候参考这里:http://blog.csdn.net/cai15191466621/article/details/7493308