private string Get(string key)
 {
     try
     {
         if (_cache.TryGetValue(key, out Option option))
         {
             return(option.Value);
         }
         else
         {
             option = _db.Options.AsNoTracking().Where(o => o.Id == key).FirstOrDefault();
             if (option != null)
             {
                 _cache.Add(key, option);
                 return(option.Value);
             }
             else
             {
                 return(null);
             }
         }
     }
     catch
     {
         _cache.Remove(key);
         return(null);
     }
 }
Exemple #2
0
        public async Task <PropertyListModel> GetFeaturedAsync()
        {
            string cacheKey = typeof(PropertyListModel) + ".Featured";

            if (_cache.TryGetValue(cacheKey, out PropertyListModel properties))
            {
                return(properties);
            }
            else
            {
                properties = await GetPropertiesAsync(new PropertyListModel()
                {
                    Featured = true, PageSize = int.MaxValue
                });

                _cache.Add(cacheKey, properties, TimeSpan.FromMinutes(60));
                return(properties);
            }
        }
Exemple #3
0
 public static T GetFromCache <T>(this IHoodCache cache, string key, Func <T> loadAction)
 {
     if (cache.TryGetValue(key, out T cachedObject))
     {
         return(cachedObject);
     }
     cachedObject = loadAction.Invoke();
     if (cachedObject != null)
     {
         cache.Add(key, cachedObject);
     }
     return(cachedObject);
 }
Exemple #4
0
        public async Task <Content> GetContentByIdAsync(int id, bool clearCache = false, bool track = true)
        {
            string cacheKey = typeof(Content).ToString() + ".Single." + id;

            if (!_cache.TryGetValue(cacheKey, out Content content) || clearCache)
            {
                content = _db.Content.Include(p => p.Categories).ThenInclude(c => c.Category)
                          .Include(p => p.Media)
                          .Include(p => p.Metadata)
                          .Include(p => p.Author)
                          .FirstOrDefault(c => c.Id == id);
                if (content == null)
                {
                    return(content);
                }

                await RefreshMetasAsync(content);

                _cache.Add(cacheKey, content, TimeSpan.FromMinutes(60));
            }
            return(content);
        }
Exemple #5
0
        public virtual string GenerateScripts(IUrlHelper urlHelper, ResourceLocation location, bool bundleScripts = true)
        {
            if (!_scripts.ContainsKey(location) || _scripts[location] == null)
            {
                return("");
            }

            if (!_scripts.Any())
            {
                return("");
            }

            if (bundleScripts && _hostingEnvironment.EnvironmentName != "Development")
            {
                var partsToBundle = _scripts[location]
                                    .Where(x => x.Bundle)
                                    .Distinct()
                                    .ToArray();
                var partsToDontBundle = _scripts[location]
                                        .Where(x => !x.Bundle)
                                        .Distinct()
                                        .ToArray();

                var result = new StringBuilder();

                if (partsToBundle.Any())
                {
                    var bundlesDirectory = _hostingEnvironment.WebRootPath + "\\bundles";
                    if (!Directory.Exists(bundlesDirectory))
                    {
                        Directory.CreateDirectory(bundlesDirectory);
                    }

                    var bundle = new Bundle();
                    foreach (var item in partsToBundle)
                    {
                        if (item.Src.IsAbsoluteUrl())
                        {
                            partsToDontBundle.Append(item);
                            continue;
                        }

                        new PathString(urlHelper.Content(item.Src)).StartsWithSegments(urlHelper.ActionContext.HttpContext.Request.PathBase, out PathString path);

                        var src = path.Value.TrimStart('/');
                        if (!File.Exists(_hostingEnvironment.WebRootPath + src))
                        {
                            src = $"wwwroot/{src}";
                        }

                        bundle.InputFiles.Add(src);
                    }

                    var bundleSha256 = GetBundleSha256(partsToBundle.Select(x => x.Src).ToArray());
                    bundle.OutputFileName = $"wwwroot/bundles/" + bundleSha256 + ".js";

                    bundle.FileName = _hostingEnvironment.ContentRootPath + "\\" + bundleSha256 + ".json";
                    bool minified = true;
                    lock (accessLock)
                    {
                        var  cacheKey      = $"Hood.Bundling.ShouldRebuild.{bundleSha256}";
                        bool shouldRebuild = !_cache.TryGetValue(cacheKey, out shouldRebuild);
                        if (shouldRebuild)
                        {
                            BundleMinifier.ErrorMinifyingFile += BundleMinifier_ErrorMinifyingFile;

                            minified = _bundleFileProcessor.Process(bundle.FileName, new List <Bundle> {
                                bundle
                            });
                            _cache.Add(cacheKey, false, new TimeSpan(2, 0, 0));
                        }
                    }

                    result.AppendFormat("<script id=\"hood-{1}\" src=\"{0}\"></script>", urlHelper.Content("~/bundles/" + bundleSha256 + (minified ? ".min.js" : ".js")), location.ToString().ToSentenceCase().ToSeoUrl());
                    result.Append(Environment.NewLine);
                }
                foreach (var item in partsToDontBundle)
                {
                    result.AppendFormat("<script {1}{2}src=\"{0}\"></script>", urlHelper.Content(item.Src), item.IsAsync ? "async " : "", item.IsDefer ? "defer " : "");
                    result.Append(Environment.NewLine);
                }
                return(result.ToString());
            }
            else
            {
                //bundling is disabled
                var result = new StringBuilder();
                foreach (var item in _scripts[location].Distinct())
                {
                    result.AppendFormat("<script {1}{2}src=\"{0}\"></script>", urlHelper.Content(item.Src), item.IsAsync ? "async " : "", item.IsDefer ? "defer " : "");
                    result.Append(Environment.NewLine);
                }
                return(result.ToString());
            }
        }