public Error404 GetError404(int nodeId) { var siteNode = GetSiteRoot(nodeId); var cacheKey = CacheKey.Build <CmsServiceCachedProxy, Error404>(siteNode.Id.ToString()); return(_cache.Get(cacheKey, () => _cmsService.GetError404(siteNode.Id))); }
public Home GetHome(int nodeId) { var siteNode = GetSiteRoot(nodeId); var cacheKey = CacheKey.Build <CmsServiceCachedProxy, Home>(siteNode.Id.ToString()); return(_cache.Get(cacheKey, () => _cmsService.GetHome(siteNode.Id))); }
public IEnumerable <IPublishedContent> Fetch(Func <int, IEnumerable <IPublishedContent> > hydrationFunction, string functionAlias, int rootNodeId) { var cacheKey = CacheKey.Build <NodeGetterCachedProxy, IPublishedContent>($"GetByhydrationFunction_{functionAlias}_{rootNodeId}"); var node = Cache.Instance.Get(cacheKey, () => _nodeGetter.Fetch(hydrationFunction, null, rootNodeId)); return(node); }
public void Value_Is_Returned() { var cacheKey = CacheKey.Build <When_Getting_With_Function, string>("Value_Is_Returned"); var result = _cache.Get <string>(cacheKey, MyValueMethod); Assert.Equal("Hello", result); }
// Get public IPublishedContent Get(string xpath, int rootNodeId) { var cacheKey = CacheKey.Build <NodeGetterCachedProxy, IPublishedContent>($"Get_{xpath}_{rootNodeId}"); var node = Cache.Instance.Get(cacheKey, () => _nodeGetter.Get(xpath, rootNodeId)); return(node); }
public void NonExisting_Item_Is_Handled() { var cacheKey = CacheKey.Build <When_Getting_With_Function, string>("NonExisting_Item_Is_Handled"); var result = _cache.Get <string>(cacheKey, MyNullValueMethod); Assert.Null(result); }
public void NonExisting_Custom_Class_Is_Handled() { var cacheKey = CacheKey.Build <When_Getting_With_Function, string>("NonExisting_Custom_Class_Is_Handled"); var result = _cache.Get <MyClass>(cacheKey); Assert.Null(result); }
// Fetch public IEnumerable <IPublishedContent> Fetch(string xpath, int rootNodeId) { var cacheKey = CacheKey.Build <NodeGetterCachedProxy, IPublishedContent>($"Fetch_{xpath}_{rootNodeId}"); var node = Cache.Instance.Get(cacheKey, () => _nodeGetter.Fetch(xpath, rootNodeId)); return(node); }
public void NonExisting_Of_Interface_Is_Handled() { var cacheKey = CacheKey.Build <When_Getting, string>("NonExisting_Of_Interface_Is_Handled"); var result = _cache.Get <IMyInterface>(cacheKey); Assert.Null(result); }
public void NonExisting_Date_Is_Handled() { var cacheKey = CacheKey.Build <When_Getting, string>("NonExisting_Date_Is_Handled"); var result = _cache.Get <DateTime?>(cacheKey); Assert.Null(result); }
public void Existing_Item_Causes_Error() { var cacheKey = CacheKey.Build <When_Adding, string>("Existing_Item_Causes_Error"); _cache.Add(cacheKey, "hello"); var result = _cache.Add(cacheKey, "goodbye"); Assert.False(result); }
public void Value_Is_Returned() { var cacheKey = CacheKey.Build <When_Getting, string>("Value_Is_Returned"); _cache.Add(cacheKey, "hello", new TimeSpan(0, 0, 1)); var result = _cache.Get <string>(cacheKey); Assert.Equal("hello", result); }
public void Items_Are_Removed_By_Prefix() { var cacheKey = CacheKey.Build <When_Removing, string>("Items_Are_Removed_By_Prefix"); _cache.Add(cacheKey, "hello"); _cache.RemoveByPrefix(typeof(When_Removing).ToString()); var result = _cache.Get <string>(cacheKey); Assert.Null(result); }
public void Item_Is_Removed() { var cacheKey = CacheKey.Build <When_Removing, string>("Item_Is_Removed"); _cache.Add(cacheKey, "hello"); _cache.Remove(cacheKey); var result = _cache.Get <string>(cacheKey); Assert.Null(result); }
public void Item_Is_Added_For_Expected_Time() { var cacheKey = CacheKey.Build <When_Adding, string>("Value_Times_Out"); _cache.Add(cacheKey, "hello", new TimeSpan(0, 0, 1)); Thread.Sleep(1100); // this is a bit hacky but i can't think of a more elegant way to test var result = _cache.Get <string>(cacheKey); Assert.Null(result); }
//-- // Where the magic happens! //-- internal IPublishedContent GetRootNode(int nodeId) { var cacheKey = CacheKey.Build <NodeGetterCachedProxy, Dictionary <int, IPublishedContent> >("RootNodes"); var sites = Cache.Instance.Get <Dictionary <int, IPublishedContent> >(cacheKey); IPublishedContent rootNode; var umbracoHelper = new UmbracoHelper(UmbracoContext.Current); if (sites != null) { var node = umbracoHelper.TypedContent(NodeHelper.EnsureNodeId(nodeId)); // Use the IDs in the Path property to get the Root node from the cached Dictionary. var pathIds = node.Path.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries).Select(x => int.Parse(x)); foreach (var id in pathIds) { if (sites.ContainsKey(id)) { rootNode = sites[id]; return(rootNode); } } } // get here if there were no cached Site nodes, OR the Site node was not found in the dictionary sites = new Dictionary <int, IPublishedContent>(); // this needs to be configurable by node type alias because the "site node" migght be at level 2 if (!string.IsNullOrWhiteSpace(_siteNodeContentTypeAlias)) { rootNode = umbracoHelper.TypedContent(NodeHelper.EnsureNodeId(nodeId)).AncestorOrSelf(_siteNodeContentTypeAlias); // top level node } else { rootNode = umbracoHelper.TypedContent(NodeHelper.EnsureNodeId(nodeId)).AncestorOrSelf(1); } if (rootNode != null) { // GetSiteNode might return null sites.Add(rootNode.Id, rootNode); Cache.Instance.Add(cacheKey, sites); } return(rootNode); }
/// <summary> /// Uses a Dictionary to store ALL the root nodes in cache. /// /// Uses the IDs in the Path property of the given node to get the Root node from the cached Dictionary. /// </summary> /// <param name="nodeId"></param> /// <returns></returns> public SiteRoot GetSiteRoot(int nodeId) { var cacheKey = CacheKey.Build <CmsServiceCachedProxy, Dictionary <int, SiteRoot> >("-1"); var sites = _cache.Get <Dictionary <int, SiteRoot> >(cacheKey); SiteRoot siteNode; if (sites != null) { // Use the IDs in the Path property to get the Root node from the cached Dictionary. IPublishedContent node; using (UmbracoContextReference umbContextRef = _umbracoContextFactory.EnsureUmbracoContext()) { var contentCache = umbContextRef.UmbracoContext.Content; node = umbContextRef.UmbracoContext.Content.GetById(nodeId); } var pathIds = node.Path.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries).Select(x => int.Parse(x)); foreach (var id in pathIds) { if (sites.ContainsKey(id)) { siteNode = sites[id]; return(siteNode); } } } // get here if there were no cached Site nodes, OR the Site node was not found in the dictionary sites = new Dictionary <int, SiteRoot>(); siteNode = _cmsService.GetSiteRoot(nodeId); if (siteNode != null) { // GetSiteNode might return null sites.Add(siteNode.Id, siteNode); } _cache.Add(cacheKey, sites); return(siteNode); }
internal static INodeGetter NodeGetter() { var cacheKey = CacheKey.Build <Resolver, NodeGetter>("NodeGetterInstance"); var nodeGetter = Cache.Instance.Get <INodeGetter>(cacheKey, () => { var umbracoHelper = new UmbracoHelper(UmbracoContext.Current); var umbAdaptor = new UmbracoHelperAdaptor(umbracoHelper); var getter = new NodeGetter(umbAdaptor); if (ConfigurationManager.AppSettings[AppSettingKey] == true.ToString().ToLower()) { return(new NodeGetterCachedProxy(getter)); } return(getter); }); return(nodeGetter); }
public List <SitemapXmlItem> GetSitemap(int nodeId, string baseUrl) { var cacheKey = CacheKey.Build <CmsServiceCachedProxy, List <SitemapXmlItem> >(nodeId.ToString()); return(_cache.Get(cacheKey, () => _sitemapXmlGenerator.GetSitemap(nodeId, baseUrl))); }