From 460bd3c9d07c665b1e67d9dac4ba28cac8803c6e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20M=2E=20Alarc=C3=B3n?= Date: Sat, 25 Apr 2020 21:08:13 +0200 Subject: [PATCH] Added new CachingTimeOut property to force cache invalidation after the specified number of seconds --- src/MIISHandler/MarkdownFile.cs | 23 +++++++++++++++------- src/MIISHandler/Properties/AssemblyInfo.cs | 4 ++-- src/Tests/posts/index.md | 9 +++++---- 3 files changed, 23 insertions(+), 13 deletions(-) diff --git a/src/MIISHandler/MarkdownFile.cs b/src/MIISHandler/MarkdownFile.cs index 5822376..8e07451 100644 --- a/src/MIISHandler/MarkdownFile.cs +++ b/src/MIISHandler/MarkdownFile.cs @@ -38,7 +38,7 @@ public class MarkdownFile private string _layout; private SimpleYAMLParser _FrontMatter; private bool? _CachingEnabled = null; //Should be default to allow for the expression shortcircuit in the CachingEnabled property - private double _NumSecondsCacheIsValid = 0; + private double _CachingTimeOut = 0; private bool _isPersistedToCache = false; #endregion @@ -524,18 +524,26 @@ internal bool CachingEnabled } } + //The timeout in seconds to force the content cache to invalidate //Set by custom tags or params. 0 means no time based expiration. + //This is only read once, when persisting the file contents to the Cache internal double CachingTimeOut { get { - return _NumSecondsCacheIsValid; + //Read from Front-Matter + double fmVal = 0; + _ = double.TryParse(FieldValuesHelper.GetFieldValue("CachingTimeOut", this, "0"), out fmVal); + return Math.Min(_CachingTimeOut, fmVal); } set { - _NumSecondsCacheIsValid = value; - if (value <= 0) _NumSecondsCacheIsValid = 0; - //if (value > 86400) _NumSecondsCacheIsValid = 86400; //24 hours in seconds + if (value <= 0) return; + //If the value has been set before (external value and FM value, for example), the minimum one wins + if (_CachingTimeOut > 0) + _CachingTimeOut = Math.Min(_CachingTimeOut, value); + else + _CachingTimeOut = value; } } @@ -693,12 +701,13 @@ private void PersistContentToCache() { CacheDependency deps = GetCacheDependencies(); + double cachingTimeout = this.CachingTimeOut; //Add to Cache - if (this.CachingTimeOut > 0) + if (cachingTimeout > 0) { //Cache invalidation when files change or after a certain time HttpRuntime.Cache.Insert(this.CachingId, this.CachedContentItem, deps, - DateTime.UtcNow.AddSeconds(this.CachingTimeOut), Cache.NoSlidingExpiration); + DateTime.UtcNow.AddSeconds(cachingTimeout), Cache.NoSlidingExpiration); } else { diff --git a/src/MIISHandler/Properties/AssemblyInfo.cs b/src/MIISHandler/Properties/AssemblyInfo.cs index d54ec18..542fa1c 100644 --- a/src/MIISHandler/Properties/AssemblyInfo.cs +++ b/src/MIISHandler/Properties/AssemblyInfo.cs @@ -31,5 +31,5 @@ // // You can specify all the values or you can default the Revision and Build Numbers // by using the '*' as shown below: -[assembly: AssemblyVersion("3.3.0")] -[assembly: AssemblyFileVersion("3.3.0")] +[assembly: AssemblyVersion("3.4.0")] +[assembly: AssemblyFileVersion("3.4.0")] diff --git a/src/Tests/posts/index.md b/src/Tests/posts/index.md index 5caa676..1d3f042 100644 --- a/src/Tests/posts/index.md +++ b/src/Tests/posts/index.md @@ -1,10 +1,11 @@ --- title: Sample folder for Files custom Front-Matter field author: jmalarcon -posts: !!FilesFromFolder ./ false -tags: !!TagsFromFolder ./ false -categs: !!CategsFromFolder ./ false -caching: true +#posts: !!FilesFromFolder ./ false +#tags: !!TagsFromFolder ./ false +#categs: !!CategsFromFolder ./ false +#caching: true +#CachingTimeout: 60 arr: [cli-ref-config, cli-ref-delete, cli-ref-init] ---