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; } }
- Cache 应用
- Cache应用
- 用好Cache,优化应用
- cache单点登陆应用
- 用好Cache,优化应用
- shark应用cache
- PHP cache应用
- java cache 简单应用
- Spring4.1 cache 应用
- 应用缓存(Application Cache)
- Guava Cache应用
- 分布式cache系统的应用
- .net中Cache的应用
- 分布式cache系统的应用
- html5离线应用application cache
- html5离线应用application cache
- .net中Cache的应用
- Html5 Application Cache 离线应用
- MyBatis简介与配置MyBatis+Spring+MySql
- adb push 用法
- 使用VuGen开发Vuser脚本——lr掌握过程之3
- 第十六周 任务二
- UITextView控件的用法详解
- Cache应用
- ios线程监听模式
- UITextView实现自动隐藏键盘,自动隐藏键盘
- Couldn't register xxx.xxx.xxx with the bootstrap server
- 关于8583报文
- 小波分析基础
- P81Demo2 ASSUME DS:C1
- 二叉查找树的创建,插入,搜索,查询后继
- C++课程教学改革问卷调查