<%@ 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. libSVM的数据格式

    首先介绍一下 libSVM的数据格式 Label 1:value 2:value -. Label:是类别的标识,比如上节train.model中提到的1 -1,你可以自己随意定,比如-10,0,15 ...

  2. 6. 星际争霸之php设计模式--建造器模式

    题记==============================================================================本php设计模式专辑来源于博客(jymo ...

  3. 使用php模拟post的几种方法

    <?phpfunction file_get_contents_post($url,$post){$options=array('http'=>array('method'=>'PO ...

  4. 《zw版·Halcon-delphi系列原创教程》cgal与opencv,Halcon

    <zw版·Halcon-delphi系列原创教程>cgal与opencv,Halcon opencv作为少有的专业开源图像软件,虽然功能,特别是几何计算方面,不如Halcon,不过因为开源 ...

  5. Eratosthenes筛选法求解质数

    问题说明: 除了自身之外,无法被其它整数整除的数称之为质数,要求质数很简单,但如何快速的求出质数则一直是程式设计人员与数学家努力的课题, 在这边介绍一个着名的 Eratosthenes求质数方法. 解 ...

  6. linux-exp 工具+小技巧

    # 工具篇 # pwntools ,gdb-peda ROPgadget-tool . EDB ## pwntools获取.安装和文档帮助 ## - pwntools: github可以搜索到 htt ...

  7. powershell: 生成随机字符串

    ASCII范围内的 获取6个随机字符(字母和数字) 48到57是数字0-9,powershell的范围操作符是..,和Perl 5的一样, 所以 48..57就是指(48 49 50 51 52 53 ...

  8. 常用Git命令

    Git教程:http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000 一般来说,日常使用只要 ...

  9. xib托线出来的为什么是weak而不是strong

    因为控件他爹( view.superview )已经揪着它的小辫了( strong reference ),你( viewController )眼瞅着( weak reference )就好了. 当 ...

  10. PHP 页面编码声明与用header或meta实现PHP页面编码的区别

    php的header来定义一个php页面为utf编码或GBK编码 php页面为utf编码 header("Content-type: text/html; charset=utf-8&quo ...