<commit_before>﻿namespace Showcase.Server.Infrastructure.Caching.Base
{
    using System;
    using System.Threading.Tasks;
    using System.Web;
    using System.Web.Caching;

    public abstract class BaseCacheService
    {
        protected async Task<T> Get<T>(
            string cacheId,
            Func<Task<T>> getItemCallback,
            DateTime? absoluteExpiration = null,
            TimeSpan? slidingExpiration = null) 
            where T : class
        {
            var item = HttpRuntime.Cache.Get(cacheId) as T;
            if (item == null)
            {
                item = await getItemCallback();
                HttpContext.Current.Cache.Insert(
                    cacheId,
                    item,
                    null,
                    absoluteExpiration ?? Cache.NoAbsoluteExpiration,
                    slidingExpiration ?? Cache.NoSlidingExpiration); // TODO: lock
                return item;
            }

            return item;
        }
    }
}<commit_msg>Added locking to BaseCacheService.cs (#45)<commit_after>﻿namespace Showcase.Server.Infrastructure.Caching.Base
{
    using System;
    using System.Threading.Tasks;
    using System.Web;
    using System.Web.Caching;

    public abstract class BaseCacheService
    {
        private static object CacheLock = new object();

        protected async Task<T> Get<T>(
            string cacheId,
            Func<Task<T>> getItemCallback,
            DateTime? absoluteExpiration = null,
            TimeSpan? slidingExpiration = null) 
            where T : class
        {
            var item = this.GetFromCache<T>(cacheId);
            if (item == null)
            {
                lock(CacheLock)
                {
                    item = this.GetFromCache<T>(cacheId);
                    if (item == null)
                    {
                        item = getItemCallback().Result;
                        HttpContext.Current.Cache.Insert(
                            cacheId,
                            item,
                            null,
                            absoluteExpiration ?? Cache.NoAbsoluteExpiration,
                            slidingExpiration ?? Cache.NoSlidingExpiration);
                    }
                }
            }

            return item;
        }

        private T GetFromCache<T>(string cacheId) where T : class
        {
            return HttpRuntime.Cache.Get(cacheId) as T;
        }
    }
}
{'fraction_non_alphanumeric': 0.07767385115900773, 'fraction_numerical': 0.0008133387555917039, 'mean_word_length': 1.294776119402985, 'pattern_counts': {'":': 0, '<': 14, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3143254', 'n_tokens_mistral': 609, 'n_tokens_neox': 580, 'n_words': 132}