Exemple #1
0
        /// <summary>
        /// Get a cached value.
        /// </summary>
        /// <param name="key">The key to look up.</param>
        /// <returns>The cached value, or null if the value was not found (CACHE MISS).</returns>
        public TVal get(TKey key)
        {
            int tag = hashGenerator.getHashCode(key);

            int start, end;

            calculateBlockRange(tag, out start, out end);

            // NOTE: Did a crude test on "Parallel.For(start, end, i => {" to see if it was worth using. With its overhead,
            // on my machine, "N" needed to be >= 32 before it was actually faster. A set cardinality that high is almost
            // never the right balance.
            for (int i = start; i <= end; i++)
            {
                CacheEntry <TKey, TVal> entry = entries[i];

                if (entry.valid && entry.key.Equals(key))
                {
                    cacheHits++;
                    entry.timestamp = getCurrentTime();
                    return(entry.value);
                }
            }
            ;

            // Cache miss. We were told not to provide a backing store, so we just return default and let the caller handle it.
            // TODO: Talk to the team and see if we want to provide an (optional?) Interface to streamline this.
            cacheMisses++;

            return(default(TVal));
        }