降低数据库压力
<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

Cache缓存优化的更多相关文章

  1. django缓存优化中caches参数如何配置?

    在python开发中,如果运营django进行编写,为了提升效率,常常需要优化缓存,缓存优化中必须掌握的caches参数相关知识: CACHES 配置参数概述 - 格式 CACHES 字典配置格式如下 ...

  2. PHP服务缓存优化之ZendOpcache、xcache、eAccelerator

    PHP服务缓存优化原理 Nginx 根据扩展名或者过滤规则将PHP程序请求传递给解析PHP的FCGI,也就是php-fpm进程 缓存操作码(opcode) Opcode,PHP编译后的中间文件,缓存给 ...

  3. 注释驱动的 Spring cache 缓存介绍

    概述 Spring 3.1 引入了激动人心的基于注释(annotation)的缓存(cache)技术,它本质上不是一个具体的缓存实现方案(例如 EHCache 或者 OSCache),而是一个对缓存使 ...

  4. [转]注释驱动的 Spring cache 缓存介绍

    原文:http://www.ibm.com/developerworks/cn/opensource/os-cn-spring-cache/ 概述 Spring 3.1 引入了激动人心的基于注释(an ...

  5. iOS网络加载图片缓存策略之ASIDownloadCache缓存优化

    iOS网络加载图片缓存策略之ASIDownloadCache缓存优化   在我们实际工程中,很多情况需要从网络上加载图片,然后将图片在imageview中显示出来,但每次都要从网络上请求,会严重影响用 ...

  6. 注释驱动的 Spring cache 缓存介绍--转载

    概述 Spring 3.1 引入了激动人心的基于注释(annotation)的缓存(cache)技术,它本质上不是一个具体的缓存实现方案(例如 EHCache 或者 OSCache),而是一个对缓存使 ...

  7. MySQL优化二(连接优化和缓存优化)

    body { font-family: Helvetica, arial, sans-serif; font-size: 14px; line-height: 1.6; padding-top: 10 ...

  8. MySQL优化-一 、缓存优化

    body { font-family: Helvetica, arial, sans-serif; font-size: 14px; line-height: 1.6; padding-top: 10 ...

  9. Java系统高并发之Redis后端缓存优化

    一:前端优化 暴露接口,按钮防重复(点击一次按钮后就变成禁用,禁止重复提交) 采用CDN存储静态化的页面和一些静态资源(css,js等) 二:Redis后端缓存优化 Redis 是完全开源免费的,遵守 ...

随机推荐

  1. 20145231《Java程序设计》课程总结

    20145231 <Java程序设计>课程总结 每周读书笔记链接汇总 ● 20145231<Java程序设计>第一周学习总结 ●20145231<Java程序设计> ...

  2. HashSet,TreeSet和LinkedHashSet的区别

    1. Set接口 Set不允许包含相同的元素,如果试图把两个相同元素加入同一个集合中,add方法返回false. Set判断两个对象相同不是使用==运算符,而是根据equals方法.也就是说,只要两个 ...

  3. iOS_AutoLayout自动布局

    目录: 一.什么是AutoLayout? 二.创建autoLayout的方法 三.VFL语言     一.什么是AutoLayout? Autolayout是一种“自动布局”技术,专门用来布局UI界面 ...

  4. JSP DAO(Model)

    示例代码: 1. Users类 package com.po; public class Users { private String username; private String passwor ...

  5. Spark-运行时架构

    Spark运行时架构 在分布式环境下,Spark集群采用的时主/从结构.在一个Spark集群中,有一个节点负责中央协调,调度各个分布式工作节点.这个中央协调节点被称为驱动器(Driver),与之对应的 ...

  6. Ubuntu 安装mysql

    ubuntu上安装mysql非常简单只需要几条命令就可以完成. 1. sudo apt-get install mysql-server   2. apt-get isntall mysql-clie ...

  7. YII2笔记之一

    安装advanced:执行init 执行yii.bat 创建数据库  修改common/config/main-local.php中的db配置  执行migratebasic:web目录是可以被外部直 ...

  8. Linux嵌入式 -- Bootloader , Uboot

    1. Bootloader作用 PC机中的引导加载程序由BIOS(其本质是一段固件程序)和GRUB或LILO一起组成.BIOS在完成硬件检测和资源分配后,将硬盘中的引导程序读到系统内存中然后将控制权交 ...

  9. SharedPreferences概述

    SharedPreferences概述 一.简介 SharedPreferences简介 上图紫色标注的部分为使用方法. SharedPreferences成员(属性和方法) 二.核心函数及使用实例 ...

  10. 用find命令查找最近修改过的文件

    Linux的终端上,没有windows的搜索那样好用的图形界面工具,但find命令确是很强大的. 比如按名字查找一个文件,可以用 find / -name targetfilename . 唉,如果只 ...