asp.net 13 缓存,Session存储
1.缓存
将数据从数据库/文件取出来放在服务器的内存中,这样后面的用来获取数据,不用查询数据库,直接从内存(缓冲)中获取数据,提高了访问的速度,节省了时间,也减轻了数据库的压力。
缓冲空间换时间的技术。
适合放在缓冲中的数据:经常被查询,但是不是经常改动的数据。
(分布式缓冲......Memcache Redis)
2.Cache对象
Cache高速缓冲,其实就是服务器端的状态保持~
(Session与Cache区别:每个用户都有自己单独的Sesson对象;但是放在Cache中的数据是共享的)
Cache对象基本使用方法:
using CZBK.ItcastProject.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Caching;
using System.Web.UI;
using System.Web.UI.WebControls; namespace CZBK.ItcastProject.WebApp._2015_6_6
{
public partial class CacheDemo : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//判断缓存中是否有数据.
if (Cache["userInfoList"] == null)
{
BLL.UserInfoService UserInfoService = new BLL.UserInfoService();
List<UserInfo> list = UserInfoService.GetList();
//将数据放到缓存中。
Cache["userInfoList"] = list;
}
else
{
List<UserInfo> list = (List<UserInfo>)Cache["userInfoList"];
Response.Write("数据来自缓存");
}
} }
}
Cache Insert方法:
using CZBK.ItcastProject.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Caching;
using System.Web.UI;
using System.Web.UI.WebControls; namespace CZBK.ItcastProject.WebApp._2015_6_6
{
public partial class CacheDemo : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//判断缓存中是否有数据.
if (Cache["userInfoList"] == null)
{
BLL.UserInfoService UserInfoService = new BLL.UserInfoService();
List<UserInfo> list = UserInfoService.GetList();
//将数据放到缓存中。
Cache.Insert("userInfoList", list, null, DateTime.Now.AddSeconds(), TimeSpan.Zero, System.Web.Caching.CacheItemPriority.Normal, RemoveCache);
//string key
//object value
//CacheDependency dependencies 缓存依赖,检测数据库的数据源,如果数据库发生改变,通知缓存失效
//DateTime absoluteExpiration 绝对过期时间
//TimeSpan slidingExpiration 滑动过期时间
//CacheItemPriority priority 缓存优先级
//CacheItemRemovedCallback onRemoveCallback 委托,缓存删除的回调函数 Response.Write("数据来自数据库");
//Cache.Remove("userInfoList");//移除缓存
}
else
{
List<UserInfo> list = (List<UserInfo>)Cache["userInfoList"];
Response.Write("数据来自缓存");
}
}
protected void RemoveCache(string key, object value, CacheItemRemovedReason reason)
{
if (reason == CacheItemRemovedReason.Expired)
{
//缓存移除的原因写到日志中。
}
}
}
}
3.页面缓冲
Duration:缓冲过期时间
VaryByParam:与该页关联的缓存设置的名称。这是可选特性,默认值为空字符串 ("")。
注:*https://www.cnblogs.com/woxpp/p/3973182.html (更详细)
<%@ OutputCache Duration="5" VaryByParam="*"%>
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="PageCacheDemo.aspx.cs"
Inherits="CZBK.ItcastProject.WebApp._2015_6_6.PageCacheDemo" %>
<%@ OutputCache Duration="5" VaryByParam="*" %>
<!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<a href="ShOWDetail.aspx?id=196">用户详细信息</a> <a href="ShOWDetail.aspx?id=197">用户详细信息</a>
</div> </form>
</body>
</html>
4.缓冲依赖
1)文件缓冲依赖 CacheDependency
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.Caching;
using System.Web.UI;
using System.Web.UI.WebControls; namespace CZBK.ItcastProject.WebApp._2015_6_6
{
public partial class FileCacheDep : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string filePath = Request.MapPath("File.txt");
if (Cache["fileContent"] == null)
{
//文件缓存依赖.
CacheDependency cDep = new CacheDependency(filePath);
string fileContent = File.ReadAllText(filePath);
Cache.Insert("fileContent", fileContent, cDep);
Response.Write("数据来自文件");
}
else
{
Response.Write("数据来自缓存:"+Cache["fileContent"].ToString());
}
}
}
}
2)数据库缓冲依赖 SqlCacheDependency
***https://www.cnblogs.com/wbzhao/archive/2012/05/11/2495459.html
web.config 设置缓冲依赖项配置
<!--缓存依赖项配置-->
<caching>
<sqlCacheDependency enabled="true">
<databases>
<add name="GSSMS" connectionStringName="connStr" pollTime="15000"/>
</databases>
</sqlCacheDependency>
</caching>
using CZBK.ItcastProject.DAL;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Caching;
using System.Web.UI;
using System.Web.UI.WebControls; namespace CZBK.ItcastProject.WebApp._2015_6_6
{
public partial class SqlCacheDep : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Cache["customerList"] == null)
{
SqlCacheDependency cDep = new SqlCacheDependency("GSSMS", "Customer"); string sql = "select * from Customer";
DataTable da = SqlHelper.GetDataTable(sql, CommandType.Text);
Cache.Insert("customerList", da, cDep);
Response.Write("数据来自数据库");
}
else
{
Response.Write("数据来自缓存");
} }
}
}
5.Session问题
1)进程外Session存储
Session存储服务器
a.开启asp.net状态服务开启,进程W3Wp.exe
b.应用程序,配置web.config文件,端口号42424
<sessionState mode="StateServer" stateConnectionString="tcpip=localhost:42424"/
c.修改注册表 (设置允许远程访问)
位置:C:\Windows\Microsoft.NET\Framework\v4.0.30319
HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\aspnet_state\Parameters 0改成1
**对象标记为可序列化的~
2)数据库Session存储
a.新建ASPSTATE数据库
b.在位置C:\Windows\Microsoft.NET\Framework\v4.0.30319 下运行aspnet_regsql.exe 新建相关表~
c..在位置C:\Windows\Microsoft.NET\Framework\v4.0.30319 下执行sql脚本文件:永久存储-InstallPersistSqlState.sql; 临时存储-InstallSqlState.sql
d.Webconfig配置文件
<sessionState mode="SQLServer"/>
*Session信息存储在表ASPStateTempSessions中
3)Memcache/Redis 分布式存储
6.错误页面配置
<customErrors mode="On" defaultRedirect="MyErrorPage.html">
<error statusCode="403" redirect="NoAccess.htm" />
<error statusCode="404" redirect="FileNotFound.html" />
</customErrors>
*mode节点,三种情况:on 总是显示定制错误页面;off 直接调用堆栈等异常信息;RemoteOnly 本机的访问显示调用堆栈等异常信息,对于外部用户的显示定制错误页面
*statusCode 响应状态码
asp.net 13 缓存,Session存储的更多相关文章
- [2014-02-23]Asp.net Mvc分布式Session存储方案
要玩集群的时候,怎么处理会话状态Session? InProc模式的sessionState是不能用了,因为这是在web服务器本机进程里的,会造成各节点数据不一致.除非在分流的时候用ip hash策略 ...
- ASP.net 中关于Session的存储信息及其它方式存储信息的讨论与总结
通过学习和实践笔者总结一下Session 的存储方式.虽然里面的理论众所周知,但是我还是想记录并整理一下.作为备忘录吧.除了ASP.net通过Web.config配置的方式,还有通过其它方式来存储的方 ...
- ASP.NET Core 缓存技术 及 Nginx 缓存配置
前言 在Asp.Net Core Nginx部署一文中,主要是讲述的如何利用Nginx来实现应用程序的部署,使用Nginx来部署主要有两大好处,第一是利用Nginx的负载均衡功能,第二是使用Nginx ...
- asp.net中缓存的使用介绍一
asp.net中缓存的使用介绍一 介绍: 在我解释cache管理机制时,首先让我阐明下一个观念:IE下面的数据管理.每个人都会用不同的方法去解决如何在IE在管理数据.有的会提到用状态管理,有的提到的c ...
- 转:ASP.NET中的SESSION实现与操作方法
在ASP.NET中,状态的保持方法大致有:ApplicationState,SessionState,Cookie,配置文件,缓存. ApplicationState 的典型应用如存储全局数据. Se ...
- Asp.Net 之 缓存机制
asp.net缓存有三种:页面缓存,数据源缓存,数据缓存. 一.页面缓存 原理:页面缓存是最常用的缓存方式,原理是用户第一次访问的时候asp.net服务器把动态生成的页面存到内存里,之后一段时间再有用 ...
- Asp.net Mvc 自定义Session (二)
在 Asp.net Mvc 自定义Session (一)中我们把数据缓存工具类写好了,今天在我们在这篇把 剩下的自定义Session写完 首先还请大家跟着我的思路一步步的来实现,既然我们要自定义Ses ...
- asp.net 服务器端缓存与客户端缓存 [转]
介绍: 在我解释cache管理机制时,首先让我阐明下一个观念:IE下面的数据管理.每个人都会用不同的方法去解决如何在IE在管理数据.有的会提到用状态管 理,有的提到的cache管理,这里我比较喜欢ca ...
- ASP.NET状缓存Cache的应用-提高数据库读取速度
原文:ASP.NET状缓存Cache的应用-提高数据库读取速度 一. Cache概述 既然缓存中的数据其实是来自数据库的,那么缓存中的数据如何和数据库进行同步呢?一般来说,缓存中应该存放改 ...
随机推荐
- CDN之简介
1. 什么是 CDN? 来自 <什么是 CDN?> CDN(内容交付网络)是一种高度分布式服务器平台,为交付 Web 应用程序.流媒体等内容专门优化.服务器网络分布于众多物理和网络位置,对 ...
- ElasticSearch2:集群安装
0.Linux系统参数设置 Linux进程数系统限制查看 [root@ip101 config]# sysctl kernel.pid_max kernel.pid_max = 131072 [roo ...
- [Mybatis]查询Sql得到一个字符串
// find min date HashMap<String, String> minDateMap = new HashMap<String, String>(); min ...
- vue 动态组件,传递参数
<template> <div class="top"> <div class='nav'> <ul class='navHader'&g ...
- 京东商城跨域设置Cookie实现SSO单点登陆过程
可以先看下这边文章:http://blog.chinaunix.net/uid-25508399-id-3431705.html 1.点击首页的登陆按钮跳转到京东的登陆中心https://pass ...
- golang 中国代理
vim /etc/profile export GO11MODULE=onexport GO111MODULE=onexport GOPROXY=https://goproxy.io source / ...
- sqliteDOC创建数据库
上次刚接触SqlLite,不知道怎么创建数据库,现在做下总结: 界面和MYSQL一样,都是CMD界面,但不是在SQLite.exe中创建数据库: 首先还是说一下cmd下sqlite的使用网上已经很多了 ...
- redis基础学习---1
5.1.xshell传输文件命令快捷键:alt+p 2.当运行一个程序时,想退出按ctrl+c退出 3.给用户权限:chmod 777 redis.conf 另一种方式:chmod –x 4. 5.查 ...
- 九十三:CMS系统之cms后台登录功能
config form from wtforms import Form, StringField, IntegerFieldfrom wtforms.validators import Email, ...
- [GPU] Machine Learning on C++
一.MPI为何物? 初步了解:MPI集群环境搭建 二.重新认识Spark 链接:https://www.zhihu.com/question/48743915/answer/115738668 马铁大 ...