使用Cache防止多人同时修改同一条信息

时间:2023-03-09 15:49:20
使用Cache防止多人同时修改同一条信息

Default.aspx:

<a href="Default2.aspx?id=123&type=11ad">打开第二个页面id=123</a><br>
<a href="Default2.aspx?id=12&type=11ad">打开第二个页面id=12</a>

Default2.aspx:

<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label><asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
protected void Page_Load(object sender, EventArgs e)
{
string type = Request.QueryString["type"].ToString();
if (!IsPostBack)
{
if (Cache["'" + type + "'"] == null)
{
msg();
}
else
{
if (Cache["'" + type + "'"].ToString() == Request.QueryString["id"].ToString())
{
Label1.Text = "我可以进入这个页面";
}
else
{
Label1.Text = "有人已经进入该页面,请稍后。。。";
Button1.Visible = false;
}
}
} } private void msg()
{
string type = Request.QueryString["type"];
//Cache["'" + type + "'"] = Request.QueryString["id"].ToString();
Cache.Insert("'"+type+"'", Request.QueryString["id"].ToString(), null, DateTime.Now.AddSeconds(), TimeSpan.Zero);
Label1.Text = "我可以进入这个页面";
}
protected void Button1_Click(object sender, EventArgs e)
{
string type = Request.QueryString["type"];
Cache.Remove("'" + type + "'");
Response.Redirect("Default.aspx");
}