예제 #1
0
        public IDictionary <string, object> GetAll()
        {
            IDictionary <string, object> data = new Dictionary <string, object>();

#if NET35 || NET40 || NET45
            IDictionaryEnumerator cacheEnum = System.Web.HttpRuntime.Cache.GetEnumerator();

            while (cacheEnum.MoveNext())
            {
                data[cacheEnum.Key as string] = cacheEnum.Value;
            }
#else
            //获取所有Key
            var keyStoreFinalKey = LocalObjectCacheHelper.GetKeyStoreKey(this);
            if (CheckExisted(keyStoreFinalKey, true))
            {
                var keys = _cache.Get <List <string> >(keyStoreFinalKey);
                foreach (var key in keys)
                {
                    data[key] = Get(key, true);
                }
            }
#endif
            return(data);
        }
예제 #2
0
        public long GetCount()
        {
#if NET35 || NET40 || NET45
            return(_cache.Count);
#else
            var keyStoreFinalKey = LocalObjectCacheHelper.GetKeyStoreKey(this);
            if (CheckExisted(keyStoreFinalKey, true))
            {
                var keys = _cache.Get <List <string> >(keyStoreFinalKey);
                return(keys.Count);
            }
            else
            {
                return(0);
            }
#endif
        }
예제 #3
0
        public void RemoveFromCache(string key, bool isFullKey = false)
        {
            var cacheKey = GetFinalKey(key, isFullKey);

            _cache.Remove(cacheKey);

#if !NET35 && !NET40 && !NET45
            //移除key
            var keyStoreFinalKey = LocalObjectCacheHelper.GetKeyStoreKey(this);
            if (CheckExisted(keyStoreFinalKey, true))
            {
                var keys = _cache.Get <List <string> >(keyStoreFinalKey);
                keys.Remove(cacheKey);
                _cache.Set(keyStoreFinalKey, keys);
            }
#endif
        }
예제 #4
0
        public void Set(string key, object value, TimeSpan?expiry = null, bool isFullKey = false)
        {
            if (key == null || value == null)
            {
                return;
            }

            var finalKey = base.GetFinalKey(key, isFullKey);

#if NET35 || NET40 || NET45
            _cache[finalKey] = value;
#else
            var newKey = !CheckExisted(finalKey, true);

            if (expiry.HasValue)
            {
                _cache.Set(finalKey, value, expiry.Value);
            }
            else
            {
                _cache.Set(finalKey, value);
            }

            //由于MemoryCache不支持遍历Keys,所以需要单独储存
            if (newKey)
            {
                var           keyStoreFinalKey = LocalObjectCacheHelper.GetKeyStoreKey(this);
                List <string> keys;
                if (!CheckExisted(keyStoreFinalKey, true))
                {
                    keys = new List <string>();
                }
                else
                {
                    keys = _cache.Get <List <string> >(keyStoreFinalKey);
                }
                keys.Add(finalKey);
                _cache.Set(keyStoreFinalKey, keys);
            }
#endif
        }