Exemplo n.º 1
0
        /// <summary>
        /// Removes one item from cache when the cache hits the maximum size and a new item is to be put inside.
        /// </summary>
        private static void CleanUp()
        {
            RemoveDirtyItems(); // remove dirty and re-check if still cache is full
            if (cache.Count >= Globals.ThisAddIn.Application.Worksheets.Count + CACHE_BUFFER_SIZE)
            {
                // If cache size exceeded the limit, remove one item keeping the recent items.
                CachedSequenceData itemToRemove = null;
                string             keyToRemove  = string.Empty;
                foreach (var item in cache)
                {
                    if (itemToRemove == null)
                    {
                        itemToRemove = item.Value;
                        keyToRemove  = item.Key;
                    }
                    if (itemToRemove.LastAccessTime > item.Value.LastAccessTime)
                    {
                        itemToRemove = item.Value;
                        keyToRemove  = item.Key;
                    }
                }

                cache.Remove(keyToRemove);
            }
        }
Exemplo n.º 2
0
        /// <summary>
        /// Tries to get a sequence from the cache for the given range list
        /// </summary>
        /// <param name="selectedRanges">Range list for which the request is made</param>
        /// <param name="inputParamsAsKey"></param>
        /// <returns>Returns the sequence object or null if not found</returns>
        public static object TryGetSequence(IEnumerable <Range> selectedRanges, string inputParamsAsKey)
        {
            RemoveDirtyItems();

            string key = GetRangeAddress(selectedRanges, inputParamsAsKey);

            if (cache.ContainsKey(key))
            {
                CachedSequenceData seq = cache[key];
                seq.LastAccessTime = DateTime.Now;
                return(seq.CachedData);
            }

            return(null);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Add a new item to the cache
        /// </summary>
        /// <param name="range">Range to use as the key for caching</param>
        /// <param name="dataToCache">Object to be cached</param>
        /// <param name="inputParamsAsKey">Input param names as key</param>
        /// <returns>True if caching was successful</returns>
        public static bool Add(IEnumerable <Range> selectedRanges, object dataToCache, string inputParamsAsKey)
        {
            CleanUp(); // cleanup as cache may be full or this sheet may be marked dirty already

            string key = GetRangeAddress(selectedRanges, inputParamsAsKey);

            if (cache.ContainsKey(key)) // remove if exists
            {
                cache[key] = new CachedSequenceData(dataToCache);
            }
            else
            {
                cache.Add(key, new CachedSequenceData(dataToCache));
            }
            return(true);
        }
Exemplo n.º 4
0
        /// <summary>
        /// Trys to get a sequence from the cache for the given range
        /// </summary>
        /// <param name="selectedRange">Range for which the request is made</param>
        /// <returns>Returns the sequence object or null if not found</returns>
        public static object TryGetSequence(Range selectedRange, string inputParamsAsKey)
        {
            RemoveDirtyItems();

            string key = GetRangeAddress(selectedRange, inputParamsAsKey);

            lock (typeof(SequenceCache))
            {
                if (cache.ContainsKey(key))
                {
                    CachedSequenceData seq = cache[key];
                    seq.LastAccessTime = DateTime.Now;
                    return(seq.CachedData);
                }
                else
                {
                    return(null);
                }
            }
        }
Exemplo n.º 5
0
        /// <summary>
        /// Add a new item to the cache
        /// </summary>
        /// <param name="selectedRange">Range to use as the key for caching</param>
        /// <param name="dataToCache">Object to be cached</param>
        /// <param name="inputParamsAsKey">Input param names as key</param>
        /// <returns>True if caching was successful</returns>
        public static bool Add(Range selectedRange, object dataToCache, string inputParamsAsKey)
        {
            CleanUp(); // cleanup as cache may be full or this sheet may be marked dirty already

            string key = GetRangeAddress(selectedRange, inputParamsAsKey);

            if (cache.ContainsKey(key)) // remove if exists
            {
                cache[key] = new CachedSequenceData(dataToCache);
            }
            else
            {
                cache.Add(key, new CachedSequenceData(dataToCache));
            }

            return true;
        }