系统所有用户缓存数据(仅含帐号和密码信息)

using System.Data;
using CSFrameworkV4_5.Common;
using CSFrameworkV4_5.Models;
using CSFrameworkV4_5.Server.DataAccess.DAL_Permission;

namespace CSFrameworkV4_5.WCFContract
{
    /// <summary>
    /// 系统所有用户缓存数据(仅含帐号和密码信息),用于客户端调用WCF接口时检测用户名和密码,有效避免频繁访问数据库。
    /// </summary>
    public static class ActivityUserCache
    {
        private static DataTable _AllUser = null;

        /// <summary>
        /// 所有缓存的用户
        /// </summary>
        public static DataTable AllUser
        {
            get
            {
                if (_AllUser == null) ActivityUserCache.RefreshUserCache();
                return _AllUser;
            }
        }

        /// <summary>
        /// 刷新缓存数据,重新加载所有用户数据。
        /// </summary>
        public static void RefreshUserCache()
        {
            _AllUser = dalUser.GetUserList4Cache();
        }

        /// <summary>
        /// 同步用户缓存数据
        /// </summary>
        /// <param name="newUserData">最新修改的用户数据</param>
        public static void SyncUserCache(DataTable newUserData)
        {
            if (newUserData.Rows.Count <= 0) return;
            DataRow R = newUserData.Rows[0];
            if (R.RowState != DataRowState.Deleted)
            {
                string account = ConvertEx.ToString(R[tb_MyUser.Account]);
                string password = ConvertEx.ToString(R[tb_MyUser.Password]);
                if (R.RowState == DataRowState.Added) SyncAddUserCache(account, password);
                if (R.RowState == DataRowState.Modified) SyncUpdateUserCache(account, password);
            }
        }

        /// <summary>
        /// 同步用户密码
        /// </summary>
        /// <param name="account">帐号</param>
        /// <param name="pwd">新密码</param>
        public static void SyncUpdateUserCache(string account, string pwd)
        {
            if (_AllUser == null) return;
            DataRow[] rs = _AllUser.Select("Account='" + account + "'");
            if (rs.Length > 0)
            {
                lock (_AllUser)
                {
                    rs[0][tb_MyUser.Password] = pwd;
                    _AllUser.AcceptChanges();
                }
            }
        }

        /// <summary>
        /// 删除指定缓存用户
        /// </summary>
        /// <param name="account"></param>
        public static void SyncRemoveUserCache(string account)
        {
            if (_AllUser == null) return;

            lock (_AllUser)
            {
                DataRow[] rs = _AllUser.Select("Account='" + account + "'");
                if (rs.Length > 0) _AllUser.Rows.Remove(rs[0]);
            }
        }

        /// <summary>
        /// 系统增新用户,添加到缓存表
        /// </summary>
        /// <param name="account"></param>
        /// <param name="encodedPwd"></param>
        public static void SyncAddUserCache(string account, string encodedPwd)
        {
            if (_AllUser == null) return;

            lock (_AllUser)
            {
                DataRow R = _AllUser.Rows.Add();
                R[tb_MyUser.Account] = account;
                R[tb_MyUser.Password] = encodedPwd;
                _AllUser.AcceptChanges();
            }
        }

        //基于性能优化,定义成员变量
        private static DataRow[] _CurrentValidateUser = null;
        private static string filter = "Account='{0}' AND Password='{1}'";

        /// <summary>
        /// 检查用户名及密码是否正确
        /// </summary>
        /// <param name="account">登录帐号</param>
        /// <param name="encodedPwd">加密后的密码</param>
        /// <returns></returns>
        public static bool ValidateUser(string account, string encodedPwd)
        {
            _CurrentValidateUser = ActivityUserCache.AllUser.Select(string.Format(filter, account, encodedPwd));
            return _CurrentValidateUser.Length > 0;
        }

    }
}

 

posted @ 2019-08-14 10:22  DarJeely  阅读(490)  评论(0编辑  收藏  举报