软件缓存
方式1:页面输出缓存,把一张网页缓存
方式2:把网页的一部分缓存
方式3:以程序控制的方式是存储对象
当客户端第一次访问服务器的时候,服务器与信息传送给客户端,第二次客户端将需要的信息返回给服务器后,服务器将客户端需要的页面放入在客户端设置的缓存中,然后在一段刷新时间后将页面显示给用户
在页面上设置缓存
<%@ OutputCache Duration="30" VaryByParam="None"%>
Duration表示缓存的时间,在30秒内刷新则页面内容不变,30秒后刷新页面则页面改变
VaryByParam缓存特定的数据
页面的片段缓存
将需要局部不变的内容放入用户控件中
然后在用户控件的页面中加入
<%@ OutputCache Duration="30" VaryByParam="None"%>
例如,从页面1跳转到页面2,需要将页面2缓存。那么将页面1中向页面2传递的参数设置为VarByParam,表示需要将这个字段对应的页面缓存。然后根据传递过来的字段得到访问的页显示在页面2上
以程序控制的方式缓存
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Web.Caching;
public partial class Demo4_Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if(!IsPostBack)
{
Bind();
}
}
DataSet ds = null;
//将后台获取的数据放置在了数据集中
public DataSet ReturnData()
{
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=testtwo;Integrated Security=True");
con.Open();
DataSet ds = new DataSet();
SqlDataAdapter sda = new SqlDataAdapter("select * from users",con);
sda.Fill(ds);
return ds;
}
/// <summary>
/// 控制缓存,即要控制数据集缓存
/// </summary>
public void Bind()
{
ds = (DataSet)Cache["MyData"];
if (ds == null)
{
ds = ReturnData();
//参数1表示缓存的名字
//参数2表示缓存的对象
//参数3表示缓存的依赖
//参数4表示缓存时间
//参数5表示缓存过期时间的间隔,值NoAbsoluteExpiration表示在设置的缓存时间内一直缓存,不改变;值NoSlidingExpiration表示在缓存时间内如果在进行缓存操作,那么从这一刻开始缓存时间又为0然后缓存设置的时间
//参数6表示缓存的优先级
//参数7表示缓存的委托
Cache.Add("MyData", ds, null, DateTime.Now.AddMinutes(2), Cache.NoSlidingExpiration, CacheItemPriority.Default, null);
Label1.Text = "数据来至数据库";
}
else
{
Label1.Text = "数据来至于缓存";
}
GridView1.DataSource=ds;
GridView1.DataBind();
}
protected void Button1_Click(object sender, EventArgs e)
{
Cache.Remove("MyData");
Bind();
}
}
(2)反射
源代码编译成中间语言,然后在到clr编译成平台特有语言
元数据:
程序集:编译好的面向.net framework的程序逻辑单元(dll)
反射:分析程序集中的属性和方法
应用程序域:多个程序集组成
程序集集中有多个托管模块
托管模块:中间语言和元数据组成
托管模块的组成部分:
PE表头:包含文件的类型,文件创建时间,cpu代码信息
CLR表头:
应用程序域就是一个进程
System.Reflection命名空间包含的一些类
Type的使用
Type type = Type.GetType("System.Int32");
StringBuilder buil = new StringBuilder();
buil.Append(type.IsAbstract);
buil.Append("/n");
buil.Append(type.IsArray);
buil.Append("/n");
buil.Append(type.IsClass);
buil.Append("/n");
buil.Append(type.Namespace);
MessageBox.Show(buil.ToString());
获取dll中的方法:
Type type=typeof(Int32);
MethodInfo[] info=type.GetMethods();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < info.Length;i++ )
{
sb.Append(info[i]+"/n");
}
MessageBox.Show(sb.ToString());
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Type type = Type.GetType("System.Int32");
StringBuilder buil = new StringBuilder();
buil.Append(type.IsAbstract);
buil.Append("/n");
buil.Append(type.IsArray);
buil.Append("/n");
buil.Append(type.IsClass);
buil.Append("/n");
buil.Append(type.Namespace);
MessageBox.Show(buil.ToString());
}
private void button2_Click(object sender, EventArgs e)
{
Type type=typeof(Int32);
MethodInfo[] info=type.GetMethods();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < info.Length;i++ )
{
sb.Append(info[i]+"/n");
}
MessageBox.Show(sb.ToString());
}
public void GetInfo(Assembly a)
{
StringBuilder sb = new StringBuilder();
//分析程序集中的模块
foreach(Module m in a.GetModules())
{
sb.Append(m+"/n");
//无法分析中间语句,所以分析元数据
foreach(Type t in m.GetTypes())
{
sb.Append(t+"/t/n");
//分析成员信息
foreach(MemberInfo info in t.GetMembers())
{
sb.Append(info+"/n");
}
}
}
MessageBox.Show(sb.ToString());
}
private void button3_Click(object sender, EventArgs e)
{
Assembly a =Assembly.GetExecutingAssembly();
GetInfo(a);
}
private void button4_Click(object sender, EventArgs e)
{
//循环整个程序域
foreach(Assembly a in AppDomain.CurrentDomain.GetAssemblies())
{
GetInfo(a);
}
}
}
}