{"id":3445,"date":"2011-10-29T04:58:02","date_gmt":"2011-10-29T04:58:02","guid":{"rendered":"https:\/\/test.simple-talk.com\/uncategorized\/sharepoint-2010-hierarchicalconfig-caching-problem\/"},"modified":"2016-07-28T10:50:38","modified_gmt":"2016-07-28T10:50:38","slug":"sharepoint-2010-hierarchicalconfig-caching-problem","status":"publish","type":"post","link":"https:\/\/www.red-gate.com\/simple-talk\/blogs\/sharepoint-2010-hierarchicalconfig-caching-problem\/","title":{"rendered":"SharePoint 2010 HierarchicalConfig Caching Problem"},"content":{"rendered":"<p>We&#8217;ve started using the <a href=\"http:\/\/msdn.microsoft.com\/en-us\/library\/ff798371.aspx\">Application Foundations for SharePoint 2010<\/a> in some of our projects at work, and I came across a nasty issue with the hierarchical configuration settings.&#160; I have some settings that I am storing at the Farm level, and as I was testing my code it seemed like the settings were not being saved &#8211; at least that is what it appeared was the case at first.&#160; <\/p>\n<p>However, I happened to reset IIS and the settings suddenly appeared.&#160; Immediately, I figured that it must be a caching issue and dug into the code base.&#160; I found that there was a 10 second caching mechanism in the SPFarmPropertyBag and the SPWebAppPropertyBag classes.&#160; So I ran another test where I waited 10 seconds to make sure that enough time had passed to force the caching mechanism to reset the data.&#160; After 10 minutes the cache had still not cleared.&#160; After digging a bit further, I found a double lock check that looked a bit off in the GetSettingsStore() method of the SPFarmPropertyBag class:<\/p>\n<pre>{\n  \/\/Need to exist so don't deadlock.\n  rrLock.EnterWriteLock();\n  try\n  {\n    \/\/make sure first another thread didn't already load...\n        {\n      _settingStore = WebAppSettingStore.Load(this.webApplication);\n      lastLoad = DateTime.Now;\n    }\n  }\n  finally\n  {\n    rrLock.ExitWriteLock();\n  }\n}<\/pre>\n<p>What ends up happening here is the outer check determines if the _settingStore is null or the cache has expired, but the inner check is just checking if the _settingStore is null (which is never the case after the first time it&#8217;s been loaded).&#160; Ergo, the cached settings are never reset.&#160; <\/p>\n<p>The fix is really easy, just add the cache checking back into the inner if statement.<\/p>\n<pre>\/\/make sure first another thread didn't already load...\n  {\n    _settingStore = WebAppSettingStore.Load(this.webApplication);\n    lastLoad = DateTime.Now;\n  }<\/pre>\n<p>And then it starts working just fine. as long as you wait at least 10 seconds for the cache to clear.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>We&#8217;ve started using the Application Foundations for SharePoint 2010 in some of our projects at work, and I came across a nasty issue with the hierarchical configuration settings.&#160; I have some settings that I am storing at the Farm level, and as I was testing my code it seemed like the settings were not being&#8230;&hellip;<\/p>\n","protected":false},"author":46738,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[2],"tags":[],"coauthors":[],"class_list":["post-3445","post","type-post","status-publish","format-standard","hentry","category-blogs"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/3445","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/users\/46738"}],"replies":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/comments?post=3445"}],"version-history":[{"count":2,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/3445\/revisions"}],"predecessor-version":[{"id":42102,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/3445\/revisions\/42102"}],"wp:attachment":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/media?parent=3445"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/categories?post=3445"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/tags?post=3445"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/coauthors?post=3445"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}