コード例 #1
0
        public string Get(string key)
        {
            bool   cacheHit = true;
            string value    = cacheMap.GetOrAdd(key, (key) =>
            {
                cacheHit = false;
                return(GetFromDb(key));
            });

            if (cacheHit)
            {
                Interlocked.Increment(ref CacheHit);
            }
            else
            {
                if (cacheMap.Count > maxSize)
                {
                    var keyToEvict = evictionPolicy.GetKeyToEvict();
                    cacheMap.TryRemove(keyToEvict, out _);
                }
            }
            evictionPolicy.RegisterKeyHit(key);
            Interlocked.Increment(ref TotalRequest);
            return(value);
        }