HashTable简单作为缓存使用

    /// <summary>
    /// 从数据库中一次性获取用户信息放入哈希缓存中,避免重复的访问数据库
    /// </summary>
    /// <param name="cellht">手机号哈希</param>
    /// <param name="mailht">邮箱哈希</param>
    public static void GetUserInfoHash(ref Hashtable cellht, ref Hashtable mailht)
    {
        try
        {
            string strSqlCondition = "Select * from UserInfo";
            DataTable nDt = Data.GetDataTable(strSqlCondition);
            if (nDt != null && nDt.Rows.Count > 0)
            {
                for (int i = 0; i < nDt.Rows.Count; i++)
                {
                    string loginName = nDt.Rows[i]["LoginName"] as string;
                    string cellphone = nDt.Rows[i]["Cellphone"] as string;//手机号
                    string Email = nDt.Rows[i]["Email"] as string;//邮箱
                    if (!String.IsNullOrEmpty(loginName))
                    {
                        cellht.Add(loginName, cellphone);
                        mailht.Add(loginName, Email);                        
                    }
                }
            }
        }
        catch (Exception ex)
        {
            Log.AddLog("SendMsgAndMailToCurrentTask", "GetUserInfoHash Exception:" + ex.Message);
        }
    }
                Hashtable htCellphone = new Hashtable();
                Hashtable htEmail = new Hashtable();
                GetUserInfoHash(g2, ref htCellphone, ref htEmail);
                string AssignTostr = key;//hashkey
                 if (htCellphone.Contains(AssignTostr))
                    {
                         string Cellphone = htCellphone[AssignTostr] as string;
                    }
若需要重复的通过账户访问数据库获取数据,以实现某些功能场景,为了避免频繁的连接加大服务器的开销,可以将所需要的数据,一次取出在缓存中,然后再进行操作缓存,HashTable只是个简单的例子

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值