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概述 既然缓存中的数据其实是来自数据库的,那么缓存中的数据如何和数据库进行同步呢?一般来说,缓存中应该存放改 ...
随机推荐
- Linux设备驱动程序 之 完成量
内核编程中常见的一种模式是,在当前线程之外初始化某个活动,然后等待该活动的结束:这个活动可能是,创建一个新的内核线程或者新的用户空间进程.对一个已有进程的某个请求,或者某种类型的硬件动作等: 内核提供 ...
- laravel 链式组合查询数据
laravel 链式组合查询数据 一.总结 一句话总结: - 就是链式操作的基本操作,因为返回的都是一直可以进行链式操作的对象,所以我们接收返回的对象即可 - $result = DB::table( ...
- 修改网卡缓存,解决Linux 网卡丢包严重问题
Linux 网卡丢包严重 生产中有一台linux设备并发比较大,droped包比较多,尤其是在跑游戏数据包的时候,存在严重的丢包现象,怀疑网卡性能不足,在更换设备前想能不有通过软件方法解决,通过网上一 ...
- 性能测试 | 记一次生产数据库sql由451s优化为0.4s的过程
概述 最近开发说某个接口跑的很慢,排查了下发现其中一条sql,数据量不大,但居然要跑451s,下面简单记录一下优化的过程. 问题sql SELECT l.location_gid ENUMVALUE, ...
- SpringCloud(一)之微服务核心组件Eureka(注册中心)的介绍和使用
一 Eureka服务治理体系1.1 服务治理服务治理是微服务架构中最为核心和基础的模块,它主要用来实现各个微服务实例的自动化注册和发现. Spring Cloud Eureka是Spring Clou ...
- mybaits及mybaits generator 插件使用指南(亲测原创)
一. eclips安装mybaits插件 参考文章:http://www.cnblogs.com/zengsong-restService/archive/2013/08/09/3248245.htm ...
- jar/war文件的解释
http://blog.csdn.net/tang_123_/article/details/6012202#comments
- web页面找不到资源文件,报404,但是资源文件存在且路径没错
如题 , 今天遇到这个问题,maven项目导入本地myeclipse,正常跑起来之后,在web端存在部分页面资源加载不进来. 但是项目资源确实存在,一开始以为是myeclipse开发环境搭建错误导致, ...
- k8s local volume 和host path volume的区别
k8s提供多种volume接口,其中local 和host path是容易混淆的两个接口.下面这篇文章解释了两者的区别: https://groups.google.com/forum/#!topic ...
- iis管理器的程序应用池中没有Asp.NET v4.0
然后 windows + r 输入 cmd 然后输入CD C:\Windows\Microsoft.NET\Framework64\v4.0.30319 然后 输入 aspnet_regiis.exe ...