Example #1
0
        public void Get_Multiple_Positive_Test()
        {
            Debug.WriteLine("-------------Get_Multiple_Positive_Test()------------");

            string[] keys = new string[10];

            using (CalcTimeSpan calc = new CalcTimeSpan("Time for Setting"))
            {
                for (int i = 0; i < 10; i++)
                {
                    DistCache.Add(i.ToString(), i * 2);
                    keys[i] = i.ToString();
                }
            }

            using (CalcTimeSpan calc = new CalcTimeSpan("Time for Getting"))
            {
                IDictionary <string, object> _retVal = DistCache.Get(keys);

                for (int i = 0; i < 10; i++)
                {
                    Assert.IsTrue(((int)_retVal[keys[i]]) == (i * 2));
                }
            }
        }
Example #2
0
        /// <summary>
        /// 获取缓存数据。
        /// </summary>
        /// <param name="key">键。</param>
        /// <returns>缓存数据。</returns>
        private static object GetCacheValue(string key)
        {
            object value = null;
            bool   useMemcached, useWebCache, useBothCache;
            int    cacheTimeout;
            string versionKey;

            AnalyzeCacheKey(key, out useMemcached, out useWebCache, out useBothCache, out cacheTimeout, out versionKey);

            if (useBothCache) //使用双缓存
            {
                object dotNetCacheVersion = _cache.Get(versionKey);
                object memcachedVersion   = DistCache.Get(versionKey);

                //如果Web Cache和Memcached中的缓存版本不相等,则表示缓存的数据已发生变化,需重新从数据库读取并更新缓存
                if (dotNetCacheVersion == null || memcachedVersion == null || dotNetCacheVersion.ToString() != memcachedVersion.ToString())
                {
                    value = null;
                }
                else
                {
                    value = _cache.Get(key);
                }
            }
            else if (useWebCache)
            {
                value = _cache.Get(key);
            }
            else if (useMemcached)
            {
                value = DistCache.Get(key);
            }

            return(value);
        }
        //public static IDictionary<string, object> Get(params string[] keys)
        //{
        //    return DistCache.Get(keys);
        //}

        public static T Get <T>(string strKey, int cacheType = 1)
        {
            if (cacheType == 0)
            {
                return(DistCache.Get <T>(strKey));
            }
            else
            {
                return(RedisManager.Get <T>(strKey));
            }
        }
Example #4
0
        public void Get_Template_Positive_Test()
        {
            Debug.WriteLine("-------------Get_Template_Positive_Test()------------");
            string strKey      = "firstname";
            string strValue    = "Fahad";
            string strReturned = string.Empty;

            using (CalcTimeSpan calc = new CalcTimeSpan("Time for Setting"))
            {
                Assert.IsTrue(DistCache.Add(strKey, strValue), "Error adding value");
            }

            using (CalcTimeSpan calc = new CalcTimeSpan("Time for Getting"))
            {
                strReturned = DistCache.Get <string>(strKey);
            }

            Assert.IsTrue(strValue.Equals(strReturned), string.Format("Not the same: {0}", strReturned));
        }
Example #5
0
        /// <summary>
        /// 获取一个key数据
        /// </summary>
        /// <typeparam name="T">泛型数据</typeparam>
        /// <param name="strKey">key</param>
        /// <returns>泛型数据</returns>
        public static T GetVal <T>(string strKey)
        {
            T t = DistCache.Get <T>(strKey);

            if (t == null)
            {
                object obj = DistCache.Get(strKey);
                if (obj == null)
                {
                    return(t);
                }
                T rst = JsonHelper.Deserialize <T>(obj.ToString());
                if (rst == null)
                {
                    return(t);
                }
                t = rst;
            }
            return(t);
            //return DistCache.Get<T>(strKey);
        }
Example #6
0
        public static long lNumofMilliSeconds = (1000 * 60 * 60 * 2);/*缓存时间2个小时*/
        //public static long lNumofMilliSeconds = 0;/*缓存时间3个小时*/

        /// <summary>
        /// 获取一个key数据
        /// </summary>
        /// <param name="strKey">key值</param>
        /// <returns></returns>
        public static object GetVal(string strKey)
        {
            return(DistCache.Get(strKey));
        }