Beispiel #1
0
        /// <summary>
        /// Gets all the keys in the cache.
        /// WARNING: THIS IS A VERY EXPENSIVE OPERATION FOR LARGE CACHES. USE WITH CAUTION.
        /// </summary>
        /// <param name="pattern">The regular expression search pattern. If no pattern is provided, default "*" (all) is used.</param>
        public List <string> Keys(string pattern)
        {
            Regex regex = null;

            // Check if we have a pattern
            if (!string.IsNullOrWhiteSpace(pattern) && pattern != "*")
            {
                try
                {
                    regex = new Regex(pattern, RegexOptions.IgnoreCase);
                }
                catch (ArgumentException)
                {
                    return(null);
                }
            }

            _memoryCacheLock.EnterWriteLock();
            try
            {
                // Lock ensures single thread, so parallelize to improve response time
                return(_memoryCache.AsParallel().Where(kvp => regex == null ? true : regex.IsMatch(kvp.Key)).Select(kvp => kvp.Key).ToList());
            }
            finally
            {
                _memoryCacheLock.ExitWriteLock();
            }
        }
Beispiel #2
0
        /// <summary>
        /// 从缓存中清除所有缓存项
        /// </summary>
        public void Clear()
        {
            Dictionary <string, KeyValuePair <string, object> > allCacheItems = _cache.AsParallel().ToDictionary(a => { return(a.Key); });

            foreach (var key in allCacheItems.Keys)
            {
                _cache.Remove(key);
            }
        }
Beispiel #3
0
        public void Clear()
        {
            List <KeyValuePair <String, Object> > cacheItems = (from n in _cache.AsParallel() select n).ToList();

            foreach (KeyValuePair <String, Object> a in cacheItems)
            {
                _cache.Remove(a.Key);
            }

            _cacheInfo.Clear();
        }
Beispiel #4
0
        private void RemoveMatchedKeys(Func <string, bool> matchFunc)
        {
            Contract.Assert(matchFunc != null);

            SafeCacheAccess(() =>
            {
                List <string> keysToRemove = _cache.AsParallel().Where(item => matchFunc(item.Key)).Select(item => item.Key).ToList();
                keysToRemove.AsParallel().ForAll(key => _cache.Remove(key));
            },
                            true);
        }
Beispiel #5
0
        public string[] Keys(string pattern)
        {
            //Guard.NotEmpty(pattern, nameof(pattern));

            var keys = _cache.AsParallel().Select(x => x.Key);

            if (pattern.IsEmpty() || pattern == "*")
            {
                return(keys.ToArray());
            }

            return(keys.Where(x => x.StartsWith(pattern, StringComparison.OrdinalIgnoreCase)).ToArray());
        }
        public IEnumerable <string> Keys(string pattern)
        {
            Guard.NotEmpty(pattern, nameof(pattern));

            var keys = _cache.AsParallel().Select(x => x.Key);

            if (pattern.IsEmpty() || pattern == "*")
            {
                return(keys.ToArray());
            }

            var wildcard = new Wildcard(pattern, RegexOptions.IgnoreCase);

            return(keys.Where(x => wildcard.IsMatch(x)).ToArray());
        }
Beispiel #7
0
        /// <summary>
        /// Removes all data from the cache.
        /// </summary>
        public override void Purge()
        {
            lock (_cacheMutex)
            {
                // Note:
                //      Even though removing every single item is a lot slower than just creating a new MemoryCache object
                //      it is thread-safe and we won't run into some weird race conditions.
                //      Also the NativeCache class should only be used for local testing and not for live production, so
                //      the speed shouldn't be that much of an issue.

                var cacheItems = (from n in _cacheInstance.AsParallel() select n);
                foreach (KeyValuePair <String, Object> a in cacheItems)
                {
                    _cacheInstance.Remove(a.Key);
                }
            }
        }