Beispiel #1
0
        public static MongoDatabase Pool(string keys)
        {
            if (keys.IsNullEmpty())
            {
                keys = "";
            }
            string[] _keys = keys.ToLower().Split(';');
            if (pool.Count == 0)
            {
                Pool();
            }
            string _key = _keys[0];

            if (_keys.Length == 1)
            {
                if (pool.ContainsKey(_key))
                {
                    return(pool[_key]);
                }
            }
            else
            {
                _key = _keys[Rand.RndInt(0, _keys.Length)];
                if (pool.ContainsKey(_key))
                {
                    return(pool[_key]);
                }
            }
            return(Pool());
        }
Beispiel #2
0
        public static MongoDatabase UsePool(params string[] keys)
        {
            int count = pool.Count;

            if (count != poolkey.Count || count == 0)
            {
                pool.Clear(); servers.Clear(); poolkey.Clear(); count = 0;
                ConnectionStringSettingsCollection conns = WebConfig.GetConn();
                foreach (ConnectionStringSettings info in conns)
                {
                    string key = info.Name;
                    if (info.ProviderName != "MongoDB" || keys.IndexOf(key) == -1)
                    {
                        continue;
                    }

                    MongoClient client = new MongoClient(info.ConnectionString);
                    clients[key.ToLower()] = client;
                    MongoServer server = client.GetServer();
                    servers[key.ToLower()] = server;
                    pool[key.ToLower()]    = server.GetDatabase(key.IndexOf(".") == -1 ? key : key.Split('.')[1]);
                    poolkey.Add(key.ToLower());
                    count++;
                }
            }
            return(pool[poolkey[count == 1 ? 0 : Rand.RndInt(0, count)]]);
        }
Beispiel #3
0
        public static MongodbDatabase UsePool(params string[] keys)
        {
            int count = pool.Count;

            if (count != poolkey.Count || count == 0)
            {
                pool.Clear(); poolkey.Clear(); count = 0;
                ConnectionStringSettingsCollection conns = WebConfig.GetConn();
                foreach (ConnectionStringSettings info in conns)
                {
                    string key = info.Name;
                    if (key != "MongoDB" || keys.IndexOf(key) == -1)
                    {
                        continue;
                    }

                    MongodbDatabase db = new MongodbDatabase(key);
                    db.Pool             = key;
                    pool[key.ToLower()] = db;
                    poolkey.Add(key.ToLower());
                    count++;
                }
            }
            return(pool[poolkey[count == 1 ? 0 : Rand.RndInt(0, count)]]);
        }
Beispiel #4
0
        public static IMongoDatabase Pool()
        {
            int count = pool.Count;

            if (count != poolkey.Count || count == 0)
            {
                pool.Clear(); factorys.Clear(); poolkey.Clear(); count = 0;
                ConnectionStringSettingsCollection conns = WebConfig.GetConn();
                foreach (ConnectionStringSettings info in conns)
                {
                    string key = info.Name;
                    if (info.ProviderName != "MongoDB")
                    {
                        continue;
                    }

                    Mongo factory = new Mongo(info.ConnectionString);
                    factory.Connect();
                    factorys[key.ToLower()] = factory;
                    pool[key.ToLower()]     = factory.GetDatabase(key.IndexOf(".") == -1 ? key : key.Split('.')[1]);
                    poolkey.Add(key.ToLower());
                    count++;
                }
            }
            return(pool[poolkey[count == 1 ? 0 : Rand.RndInt(0, count)]]);
        }
Beispiel #5
0
        /// <summary>
        /// »ñÈ¡»º´æ¶ÔÏó
        /// </summary>
        /// <param name="key">»º´æ¼üÃû</param>
        /// <returns>·µ»Ø»º´æ¶ÔÏó</returns>
        public T Get <T>(string key)
        {
            var c = SlaveHosts[0].IsNullEmpty() ? GetClient() : Rand.RndInt(1, 3) == 1 ? GetClient() : GetReadOnlyClient();

            return(c.ContainsKey(key) ? c.Get <T>(key) : default(T));
            //using (c.AcquireLock("get")) {  }
        }
Beispiel #6
0
        /// <summary>
        /// »ñÈ¡»º´æ¶ÔÏó
        /// </summary>
        /// <param name="key">»º´æ¼üÃû</param>
        /// <returns>·µ»Ø»º´æ¶ÔÏó</returns>
        public object Get(string key)
        {
            var c = SlaveHosts[0].IsNullEmpty() ? GetClient() : Rand.RndInt(1, 3) == 1 ? GetClient() : GetReadOnlyClient();

            return(c.ContainsKey(key) ? c.GetValue(key) : null);
            //using (c.AcquireLock("get")) {  }
        }
Beispiel #7
0
        public static MongodbDatabase Pool(params string[] key)
        {
            string _key = key[Rand.RndInt(0, key.Length)].ToLower();

            if (pool.ContainsKey(_key))
            {
                return(pool[_key]);
            }
            return(Pool());
        }
Beispiel #8
0
        /// <summary>
        /// 生成干扰码
        /// </summary>
        /// <param name="len">int扩展</param>
        /// <returns>生成干扰码</returns>
        public static string Jammer(this int len)
        {
            string randStr = string.Empty;

            for (int i = 0; i < len; i++)
            {
                randStr += ((char)(Rand.RndInt(0, 33))).ToString() + ((char)(Rand.RndInt(63, 126))).ToString();
            }

            return(randStr);
        }
Beispiel #9
0
        public IList <CachedItem> GetList()
        {
            IList <CachedItem> list = new List <CachedItem>();
            var c     = SlaveHosts[0].IsNullEmpty() ? GetClient() : Rand.RndInt(1, 3) == 1 ? GetClient() : GetReadOnlyClient();
            var list2 = c.GetAllKeys();

            foreach (string key in list2)
            {
                if (!ContainsKey(key))
                {
                    continue;
                }
                list.Add(new CachedItem(key, Get(key).GetType().ToString()));
            }
            return(list);
        }
Beispiel #10
0
        /// <summary>
        /// ɾ³ý»º´æ
        /// </summary>
        /// <param name="pattern">»º´æ¼üÕýÔòÆ¥Åäģʽ</param>
        public void RemoveByPattern(string pattern)
        {
            var c    = SlaveHosts[0].IsNullEmpty() ? GetClient() : Rand.RndInt(1, 3) == 1 ? GetClient() : GetReadOnlyClient();
            var list = c.GetAllKeys();

            foreach (string key in list)
            {
                if (!ContainsKey(key))
                {
                    continue;
                }
                Regex regex = new Regex(pattern, RegexOptions.IgnoreCase | RegexOptions.Singleline | RegexOptions.Compiled);
                if (regex.IsMatch(key))
                {
                    Remove(key);
                }
            }
        }
Beispiel #11
0
 //#endregion
 //#region RndDateStr
 /// <summary>
 /// 日期随机函数
 /// </summary>
 /// <returns>返回日期随机串</returns>
 public static string RndDateStr()
 {
     return(DateTime.Now.ToString("yyyyMMddHHmmssfff") + Rand.RndInt(1000, 9999).ToString());
 }
Beispiel #12
0
        /// <summary>
        /// ¼üÊÇ·ñ´æÔÚ
        /// </summary>
        /// <param name="key">¼ü</param>
        /// <returns>true/false</returns>
        public bool ContainsKey(string key)
        {
            var c = SlaveHosts[0].IsNullEmpty() ? GetClient() : Rand.RndInt(1, 3) == 1 ? GetClient() : GetReadOnlyClient();

            return(c.ContainsKey(key));
        }