Cache缓存优化
2024-09-25 21:02:38
降低数据库压力
<appSettings><add key="ModelCache" value=""/></appSettings> //设置实体缓存时间 public RupengWang.Model.Course GetModelByCache(long Id)
{
string CacheKey = "CourseModel-" + Id;
object objModel = Maticsoft.Common.DataCache.GetCache(CacheKey);
if (objModel == null)
{
objModel = dal.GetModel(Id);
if (objModel != null)
{
int ModelCache = Maticsoft.Common.ConfigHelper.GetConfigInt("ModelCache");
Maticsoft.Common.DataCache.SetCache(CacheKey, objModel, DateTime.Now.AddMinutes(ModelCache), TimeSpan.Zero);
}
}
return (RupengWang.Model.Course)objModel;
}
2.MVC中缓存
/// <summary>
/// 从缓存中获得自定义匿名对象
/// </summary>
/// <returns></returns>
public object GetModelByCache()
{
string CacheKey = "MyProductAndGroupModel_";
object objModel = HttpRuntime.Cache[CacheKey]; //获得当前应用程序的缓存
if (objModel == null)
{
objModel = GetDefineModelByEdm();
if (objModel != null)
{
//int ModelCache = Convert.ToInt32(ConfigurationManager.AppSettings["ModelCache"]);
HttpRuntime.Cache.Insert(CacheKey, objModel, null, DateTime.Now.AddMinutes(), TimeSpan.Zero);
}
}
return objModel;
}
3 WebForm中所写最简单的缓存:
public partial class WebForm_cache : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
#region 缓存
List<object> list = (List<object>)HttpRuntime.Cache["tc_students"];
if (list == null || list.Count <= ) //如果缓存中没有就从DB中获取
{
List<object> listDB = new MyORM_BLL().SelectAllModel(typeof(TC_STUDENT));
if (listDB.Count > ) //把数据库中获得的数据进行缓存
{
HttpRuntime.Cache.Insert("tc_students", listDB, null, DateTime.Now.AddSeconds(), TimeSpan.Zero);
}
list = listDB;
}
Repeater1.DataSource = list;
Repeater1.DataBind();
#endregion
}
} WebForm_cache.aspx.cs
参考:
http://www.cnblogs.com/zgx/archive/2009/03/16/1413643.html
http://bbs.csdn.net/topics/310107146
最新文章
- python中的Ellipsis
- 我的屌丝giser成长记-工作篇之A公司
- Windows7 忘记密码的解决办法
- EditText键盘弹出时,会将布局底部的导航条顶上去(解决方法之一)
- collection view 开发笔记
- github不小心同步覆盖了本地文件
- Windows7 sp1 64位下安装配置eclipse+jdk+CDT+minGW
- Android开发之自定义圆角矩形图片ImageView的实现 - Jamy Cai
- 五子棋Web版的开发(三)
- Android NDK进入发展
- dos cmd重启2003命令shutdown -r -t 0
- IO口输入输出模式理解
- springboot整合mq接收消息队列
- MVC编程实例----简易电子商务网站(一)
- embed标签详解
- [转] iOS9系统自带字体
- PHP-问题处理验证码无法显示出来
- [20180312]进程管理其中的SQL Server进程占用内存远远大于SQL server内部统计出来的内存
- Arduino IDE for ESP8266 项目(2)wifi扫描
- Hive 组内计无重复数,追加每条记录后面