Ejemplo n.º 1
0
        public ActionResult GetPolicy(string slug)
        {
            MicrosoftDynamicsCRMadoxioPolicydocument policyDocument = null;

            string cacheKey = CacheKeys.PolicyDocumentPrefix + slug;

            if (!_cache.TryGetValue(cacheKey, out policyDocument))
            {
                // Key not in cache, so get data.
                policyDocument = _dynamicsClient.GetPolicyDocumentBySlug(slug);

                // Set cache options.
                var cacheEntryOptions = new MemoryCacheEntryOptions()
                                        // Keep in cache for this time
                                        .SetAbsoluteExpiration(TimeSpan.FromMinutes(5));

                // Save data in cache.
                _cache.Set(cacheKey, policyDocument, cacheEntryOptions);
            }

            if (policyDocument == null)
            {
                return(new NotFoundResult());
            }
            else
            {
                return(Json(policyDocument.ToViewModel()));
            }
        }
        public ActionResult GetPolicy(string slug)
        {
            MicrosoftDynamicsCRMadoxioPolicydocument policyDocument = null;
            bool   fetchDocument = false;
            string cacheKey      = CacheKeys.PolicyDocumentPrefix + slug;
            string cacheAgeKey   = CacheKeys.PolicyDocumentCategoryPrefix + slug + "_dto";

            if (!_cache.TryGetValue(cacheKey, out policyDocument))
            // item is not in cache at all, fetch.
            {
                fetchDocument = true;
            }
            else
            {
                DateTimeOffset dto = DateTimeOffset.Now;
                // fetch the age of the cache item from the cache
                if (!_cache.TryGetValue(cacheAgeKey, out dto))
                // unable to get cache age, fetch.
                {
                    fetchDocument = true;
                }
                else
                {
                    TimeSpan age = DateTimeOffset.Now - dto;
                    if (age.TotalMinutes > 10)
                    // More than 10 minutes old, fetch.
                    {
                        fetchDocument = true;
                    }
                }
            }
            if (fetchDocument)
            {
                try
                {
                    policyDocument = _dynamicsClient.GetPolicyDocumentBySlug(slug);

                    if (policyDocument != null) // handle case where the document is missing.
                    {
                        // Set cache options.
                        var newCacheEntryOptions = new MemoryCacheEntryOptions()
                                                   // Set the cache to expire far in the future.
                                                   .SetAbsoluteExpiration(TimeSpan.FromDays(365 * 5));

                        // Save data in cache.
                        _cache.Set(cacheKey, policyDocument, newCacheEntryOptions);
                        _cache.Set(cacheAgeKey, DateTimeOffset.Now, newCacheEntryOptions);
                    }
                    else
                    {
                        _logger.LogError($"Unable to get Policy Document {slug} - does it exist?");
                    }
                }
                catch (HttpOperationException httpOperationException)
                {
                    // this will gracefully handle situations where Dynamics is not available however we have a cache version.
                    _logger.LogError(httpOperationException, "Error getting policy document");
                }
                catch (Exception e)
                {
                    // unexpected exception
                    _logger.LogError(e, "Unknown error occured");
                }
            }

            if (policyDocument == null)
            {
                return(new NotFoundResult());
            }

            return(new JsonResult(policyDocument.ToViewModel()));
        }