Exemple #1
0
        private CacheEntry PopulateFromEndpoint(AutoPopulateEndpoint endpoint = null)
        {
            var myMRE = GetManualResetEvent();

            try
            {
                myMRE.WaitOne();
                myMRE.Reset();

                endpoint = endpoint ?? GlobalSettings.AutoPopulateEndpoints.FirstOrDefault(x => x.authorization == Authorization && x.cacheKey == CacheKey);

                if (endpoint != null)
                {
                    var request = new APIRequest(endpoint.baseURL);
                    if (!string.IsNullOrWhiteSpace(endpoint.endpointAuthorizationKey))
                    {
                        request.AddHeader(endpoint.endpointAuthorizationKey, endpoint.endpointAuthorizationValue);
                    }

                    var result = request.Get <AutoPopulateResult>(endpoint.endpointMethod);

                    if (result.CacheSecondsOverride.HasValue && result.CacheSecondsOverride.Value >= 0)
                    {
                        CacheSeconds = result.CacheSecondsOverride.Value;
                    }
                    else if (endpoint.cacheLifespanSeconds.HasValue && endpoint.cacheLifespanSeconds.Value >= 0)
                    {
                        CacheSeconds = endpoint.cacheLifespanSeconds.Value;
                    }

                    object dataObj = GetDataObject(result.Data);

                    var entry = new CacheEntry(Authorization, CacheKey, dataObj, expirationSeconds: CacheSeconds);

                    lock (GenericMemLock)
                    {
                        var allKeys = GetAllKeys();
                        AddKeyToAllKeys(allKeys, entry);
                    }

                    MemoryCache.Set(GetAuthKey(CacheKey), entry, new MemoryCacheEntryOptions
                    {
                        AbsoluteExpirationRelativeToNow = CacheSeconds > 0 ? TimeSpan.FromSeconds(CacheSeconds) : (TimeSpan?)null
                    });

                    return(entry);
                }

                throw new Exception("cacheKey does not have a value");
            }
            finally
            {
                myMRE.Set();
            }
        }
Exemple #2
0
        //[Obsolete("Not intended for general use")]
        //public List<CacheEntry> ListFromDictionary()
        //{
        //    lock (GenericMemLock)
        //    {
        //        return GetAllKeys(authorization: Authorization);
        //    }
        //}

        public async Task <CacheEntry> GetFromDictionary(AutoPopulateEndpoint endpoint = null)
        {
            var myMRE = GetManualResetEvent();

            myMRE.WaitOne();

            var mySemaphore = GetSemaphoreSlim();

            try
            {
                await mySemaphore.WaitAsync();

                if (!MemoryCache.TryGetValue(GetAuthKey(CacheKey), out CacheEntry value))
                {
                    return(PopulateFromEndpoint(endpoint));
                }
                return(value);
            }
            finally
            {
                mySemaphore.Release();
            }
        }