zoukankan      html  css  js  c++  java
  • 使用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(10), TimeSpan.Zero);
            Label1.Text = "我可以进入这个页面";
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            string type = Request.QueryString["type"];
            Cache.Remove("'" + type + "'");
            Response.Redirect("Default.aspx");
        }
  • 相关阅读:
    docker安装部署命令
    kubernetes原理
    kubernetes安装部署
    Ansible安装
    模拟红绿灯(递归与队列)
    数据结构——顺序表
    数据结构——基本概念
    C语言高级编程
    Shell编程
    Shell命令
  • 原文地址:https://www.cnblogs.com/liuswi/p/3947604.html
Copyright © 2011-2022 走看看