<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="DemoCache.aspx.cs" Inherits="WebApplication1.DemoCache" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Button ID="Button1" runat="server" Text="Cache存数据"
onclick="Button1_Click" />
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<br />
<asp:Button ID="Button2" runat="server" Text="取值" onclick="Button2_Click" />
<br />
<asp:Button ID="Button3" runat="server" Text="查看活动商品" onclick="Button3_Click" />
<br />
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
</div>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient; namespace WebApplication1
{
public partial class DemoCache : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
} protected void Button1_Click(object sender, EventArgs e)
{
//Cache缓存对象
//Cache数据是公共的,存放在服务器内容中,可以设置过期或依赖项过期(如:改变文件时过期)
string n = TextBox1.Text;
//Cache.Insert("appName", n, null, DateTime.Now.AddDays(1), System.Web.Caching.Cache.NoSlidingExpiration);//绝对到期
Cache.Insert("appName", n, null,System.Web.Caching.Cache.NoAbsoluteExpiration, new TimeSpan(, , ));//多少时间后过期
Response.Write("OK");
} protected void Button2_Click(object sender, EventArgs e)
{
//Cache 在使用的时候,有自动锁,所有没有Lock与unLock方法
object obj = Cache["appName"];
if (obj!=null)
{
Response.Write("名称为"+obj);
} }
//查看商品
protected void Button3_Click(object sender, EventArgs e)
{
//将缓存数据呈现不通过访问数据库的方式
if (Cache["data"] != null)
{
DataTable d = Cache["data"] as DataTable;
GridView1.DataSource = d;
GridView1.DataBind();
}
else
{
BindList();
}
} private void BindList()
{
string sql = "select * from student";
DataTable dt = SQLHelper.GetTable(sql);
//将数据放到缓存中,减少服务器压力
Cache.Insert("data", dt, null, DateTime.Now.AddDays(), System.Web.Caching.Cache.NoSlidingExpiration);
GridView1.DataSource = dt;
GridView1.DataBind();
}
}
}

Cache缓存对象缓存对象的更多相关文章

  1. 在Spring中使用cache(EhCache的对象缓存和页面缓存)

    Spring框架从version3.1开始支持cache,并在version4.1版本中对cache功能进行了增强. spring cache 的关键原理就是 spring AOP,通过 spring ...

  2. spring ehcache 页面、对象缓存

    一.Ehcache基本用法 CacheManager cacheManager = CacheManager.create(); // 或者 cacheManager = CacheManager.g ...

  3. Ehcache 整合Spring 使用页面、对象缓存

    Ehcache 整合Spring 使用页面.对象缓存 Ehcache在很多项目中都出现过,用法也比较简单.一 般的加些配置就可以了,而且Ehcache可以对页面.对象.数据进行缓存,同时支持集群/分布 ...

  4. (转)Ehcache 整合Spring 使用页面、对象缓存

    Ehcache在很多项目中都出现过,用法也比较简单.一般的加些配置就可以了,而且Ehcache可以对页面.对象.数据进行缓存,同时支持集群/分布式缓存.如果整合Spring.Hibernate也非常的 ...

  5. 缓存插件 EHCache 对象缓存(Spring)

    对象缓存就是将查询的数据,添加到缓存中,下次再次查询的时候直接从缓存中获取,而不去数据库中查询. 对象缓存一般是针对方法.类而来的,结合Spring的Aop对象.方法缓存就很简单.这里需要用到切面编程 ...

  6. 高性能的分布式内存对象缓存系统Memcached

    Memcached概述   什么是Memcached? 先看看下面几个概念: Memory:内存存储,不言而喻,速度快,对于内存的要求高,不指出的话所缓存的内容非持久化.对于CPU要求很低,所以常常采 ...

  7. [转]高并发访问下避免对象缓存失效引发Dogpile效应

    避免Redis/Memcached缓存失效引发Dogpile效应 Redis/Memcached高并发访问下的缓存失效时可能产生Dogpile效应(Cache Stampede效应). 推荐阅读:高并 ...

  8. 分布式内存对象缓存系统Memcached-概述

    全面掌握Memcached 1.       概述 Memcached是danga.com(运营LiveJournal的技术团队)开发的一套分布式内存对象缓存系统,是为了加快网站http://www. ...

  9. Ehcache 整合Spring 使用页面、对象缓存(转载)

    Ehcache在很多项目中都出现过,用法也比较简单.一般的加些配置就可以了,而且Ehcache可以对页面.对象.数据进行缓存,同时支持集群/分布式缓存.如果整合Spring.Hibernate也非常的 ...

  10. Ehcache 整合Spring 使用页面、对象缓存(转)

    Ehcache在很多项目中都出现过,用法也比较简单.一般的加些配置就可以了,而且Ehcache可以对页面.对象.数据进行缓存,同时支持集群/分布式缓存.如果整合Spring.Hibernate也非常的 ...

随机推荐

  1. 本周PSP+历年作品评论

    本周PSP 类别 内容 开始时间 结束时间 间断时间 净时间(min) 9月11号 看书 构建之法 19:00 21:00 14 106 9月12号 写程序 词频统计多需求版 8:00 14:23 3 ...

  2. 一个fork()系统调用的问题

    转载:http://coolshell.cn/articles/7965.html 题目:请问下面的程序一共输出多少个“-”? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ...

  3. js操作记录

    让checkbox全选 $("#checkall").click(function(){ $("input[name='checklist']").prop(& ...

  4. linux设备驱动归纳总结(四):3.抢占和上下文切换【转】

    本文转载自:http://blog.chinaunix.net/uid-25014876-id-65711.html linux设备驱动归纳总结(四):3.抢占和上下文切换 xxxxxxxxxxxxx ...

  5. TI CC2541的GPIO引脚设置.

    P1SEL寄存器, 0xF4, 功能选择用的, 0表示GPIO口, 1表示外设.

  6. DateTimeUtil 工具类,android 和 java 通用

    import java.sql.Date;import java.text.SimpleDateFormat; public class DateTimeUtil { public final cla ...

  7. PHP使用PHPExcel删除Excel单元格指定列的方法是怎样

    有一个系统仅公司内部和外部经销商使用,在一个导出功能中公司内部员工跟外部经销商导出的列是不一样的(某些数据是不能提供给经销商的)因为导出的数据都是一样的(某些列外数据外部没有)因此并没有单独处理,而是 ...

  8. Git的搭建和使用技巧完整精华版

    [Git使用技巧] 1.把一个已经存在于版本库中的文件加入忽略提交文件(.gitignore)中,需要如下代码: git rm --cached [文件路径] 例如: git rm --cached  ...

  9. SQLServer学习笔记<>日期和时间数据的处理(cast转化格式、日期截取、日期的加减)和 case表达式

    日期和时间数据的处理. (1)字符串日期 ‘20080301’,这一串为字符串日期,但必须保证为四位的年份,两位的月份,两位的日期.例如,查询订单表日期大于‘20080301’.可以这样写: 1 se ...

  10. CSS 学习质料

    1.学习CSS布局           http://zh.learnlayout.com/display.html