From c9a35096eb70760ac0dec5a2de1eae082c21c1e3 Mon Sep 17 00:00:00 2001 From: MatteoSp Date: Wed, 10 Aug 2016 00:14:43 +0200 Subject: [PATCH] Supports cache entries monitoring --- ReadMe.md | 7 + src/.nuget/NuGet.Config | 10 +- .../App.config | 12 ++ .../CacheItemPolicyConfiguration.Tests.csproj | 152 +++++++++--------- .../ConfigFileIntegrationTests.cs | 43 ++++- .../ProgrammaticConfigIntegrationTests.cs | 49 +++++- .../packages.config | 11 +- src/CacheItemPolicyConfiguration.sln | 75 +++++---- .../CacheItemPolicyConfiguration.csproj | 126 ++++++++------- .../CacheItemPolicyConfigurationItem.cs | 27 +++- .../CacheItemPolicyHelpers.cs | 7 + .../CacheEntryConfigurationElement.cs | 25 +++ ...acheEntryConfigurationElementCollection.cs | 54 +++++++ .../CacheItemPolicyConfigurationElement.cs | 50 +++++- .../ConfigFile/SampleConfigFiles/App.config | 8 +- .../ICacheItemPolicyConfigurationItem.cs | 11 +- 16 files changed, 467 insertions(+), 200 deletions(-) create mode 100644 src/CacheItemPolicyConfiguration/ConfigFile/CacheEntryConfigurationElement.cs create mode 100644 src/CacheItemPolicyConfiguration/ConfigFile/CacheEntryConfigurationElementCollection.cs diff --git a/ReadMe.md b/ReadMe.md index e059726..a2bc68a 100644 --- a/ReadMe.md +++ b/ReadMe.md @@ -34,6 +34,13 @@ In the C# example above, the `cache` variable is an instance of the `System.Runt + + + + + + + diff --git a/src/.nuget/NuGet.Config b/src/.nuget/NuGet.Config index 67f8ea0..6a318ad 100644 --- a/src/.nuget/NuGet.Config +++ b/src/.nuget/NuGet.Config @@ -1,6 +1,6 @@ - - - - - + + + + + \ No newline at end of file diff --git a/src/CacheItemPolicyConfiguration.Tests/App.config b/src/CacheItemPolicyConfiguration.Tests/App.config index 8268d78..eeb97dc 100644 --- a/src/CacheItemPolicyConfiguration.Tests/App.config +++ b/src/CacheItemPolicyConfiguration.Tests/App.config @@ -28,5 +28,17 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/src/CacheItemPolicyConfiguration.Tests/CacheItemPolicyConfiguration.Tests.csproj b/src/CacheItemPolicyConfiguration.Tests/CacheItemPolicyConfiguration.Tests.csproj index fc19cd9..957886a 100644 --- a/src/CacheItemPolicyConfiguration.Tests/CacheItemPolicyConfiguration.Tests.csproj +++ b/src/CacheItemPolicyConfiguration.Tests/CacheItemPolicyConfiguration.Tests.csproj @@ -1,80 +1,86 @@ - - - - - Debug - AnyCPU - {EC6ED966-52A6-4AEA-828D-75C2DFDAAB41} - Library - Properties - CacheItemPolicyConfiguration - CacheItemPolicyConfiguration.Tests - v4.5 - 512 - 082f7e14 - ..\ - true - - - true - full - false - bin\Debug\ - DEBUG;TRACE - prompt - 4 - - - pdbonly - true - bin\Release\ - TRACE - prompt - 4 - - - - ..\packages\Should.1.1.20\lib\Should.dll - - - - - - ..\packages\xunit.1.9.2\lib\net20\xunit.dll - - - ..\packages\xunit.extensions.1.9.2\lib\net20\xunit.extensions.dll - - - - - - - - - - - {3e8fa116-5469-4247-9787-3b08b6736c02} - CacheItemPolicyConfiguration - - - - - - - - - - - This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}. - - - + + + + + + Debug + AnyCPU + {EC6ED966-52A6-4AEA-828D-75C2DFDAAB41} + Library + Properties + CacheItemPolicyConfiguration + CacheItemPolicyConfiguration.Tests + v4.5 + 512 + + + ..\ + true + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + ..\packages\Should.1.1.20\lib\Should.dll + + + + + + ..\packages\xunit.1.9.2\lib\net20\xunit.dll + + + ..\packages\xunit.extensions.1.9.2\lib\net20\xunit.extensions.dll + + + + + + + + + + + {3e8fa116-5469-4247-9787-3b08b6736c02} + CacheItemPolicyConfiguration + + + + + + + + + + + + + + This project references NuGet package(s) that are missing on this computer. Enable NuGet Package Restore to download them. For more information, see http://go.microsoft.com/fwlink/?LinkID=322105. The missing file is {0}. + + + + + --> \ No newline at end of file diff --git a/src/CacheItemPolicyConfiguration.Tests/ConfigFileIntegrationTests.cs b/src/CacheItemPolicyConfiguration.Tests/ConfigFileIntegrationTests.cs index d0536c2..6e19117 100644 --- a/src/CacheItemPolicyConfiguration.Tests/ConfigFileIntegrationTests.cs +++ b/src/CacheItemPolicyConfiguration.Tests/ConfigFileIntegrationTests.cs @@ -1,11 +1,13 @@ using System; +using System.Linq; using System.Collections.Generic; using System.Runtime.Caching; using CacheItemPolicyConfiguration.ConfigFile; using CacheItemPolicyConfiguration.TestHelpers; using Should; using Xunit.Extensions; - +using System.Collections.ObjectModel; + namespace CacheItemPolicyConfiguration { /// @@ -158,6 +160,41 @@ public static IEnumerable CanCreateCacheItemPolicyWithSlidingExpiratio yield return new object[] { "TestPolicySlidingSetToSecondsWithEnabledAttributeTrue", TimeSpan.FromSeconds(30), false }; yield return new object[] { "TestPolicySlidingWithoutEnabledAttribute", TimeSpan.FromMinutes(5), false }; } - } - } + } + + + [Theory, PropertyData("CanCreateCacheItemPolicyWithCacheEntriesFromDotNetConfigFileTestData")] + public void CanCreateCacheItemPolicyWithCacheEntriesFromDotNetConfigFile(string cacheItemPolicyName, ReadOnlyCollection cacheEntries, bool monitorShouldBeNull) + { + // Arrange + var expected = cacheEntries; + var config = new ConfigFileBasedCacheItemPolicyConfiguration(); + var factory = new CacheItemPolicyFactory(config); + + // Act + var cacheItemPolicy = factory.Create(cacheItemPolicyName); + var entriesMonitor = cacheItemPolicy.ChangeMonitors.FirstOrDefault() as CacheEntryChangeMonitor; + + // Assert + if (monitorShouldBeNull) + { + entriesMonitor.ShouldBeNull(); + return; + } + + entriesMonitor.ShouldNotBeNull(); + entriesMonitor.ShouldImplement(); + + entriesMonitor.CacheKeys.ShouldEqual(expected); + } + + public static IEnumerable CanCreateCacheItemPolicyWithCacheEntriesFromDotNetConfigFileTestData + { + get + { + yield return new object[] { "TestPolicyCacheEntries", new ReadOnlyCollection(new string[] { "aKeyToBeMonitored", "anotherOne", "andSoOn" }), false }; + yield return new object[] { "TestPolicyCacheEntriesEmpty", null, true }; + } + } + } } \ No newline at end of file diff --git a/src/CacheItemPolicyConfiguration.Tests/ProgrammaticConfigIntegrationTests.cs b/src/CacheItemPolicyConfiguration.Tests/ProgrammaticConfigIntegrationTests.cs index a785166..74dfbfa 100644 --- a/src/CacheItemPolicyConfiguration.Tests/ProgrammaticConfigIntegrationTests.cs +++ b/src/CacheItemPolicyConfiguration.Tests/ProgrammaticConfigIntegrationTests.cs @@ -1,10 +1,12 @@ using System; using System.Collections.Generic; +using System.Linq; using System.Runtime.Caching; using CacheItemPolicyConfiguration.TestHelpers; using Should; using Xunit.Extensions; - +using System.Collections.ObjectModel; + namespace CacheItemPolicyConfiguration { /// @@ -47,8 +49,11 @@ static ProgrammaticConfigIntegrationTests() new CacheItemPolicyConfigurationItem("TestPolicySlidingWithEnabledAttributeFalse", TimeSpan.MaxValue, false), new CacheItemPolicyConfigurationItem("TestPolicySlidingSetToZeroWithEnabledAttributeTrue", ObjectCache.InfiniteAbsoluteExpiration), new CacheItemPolicyConfigurationItem("TestPolicySlidingSetToSecondsWithEnabledAttributeTrue", TimeSpan.FromSeconds(30)), - new CacheItemPolicyConfigurationItem("TestPolicySlidingWithoutEnabledAttribute", TimeSpan.FromMinutes(5)) - }); + new CacheItemPolicyConfigurationItem("TestPolicySlidingWithoutEnabledAttribute", TimeSpan.FromMinutes(5)), + + new CacheItemPolicyConfigurationItem("TestPolicyCacheEntries", new string[] { "aKeyToBeMonitored", "anotherOne", "andSoOn" }), + new CacheItemPolicyConfigurationItem("TestPolicyCacheEntriesEmpty", new string[0]) + }); } finally { @@ -197,6 +202,40 @@ public static IEnumerable CanCreateCacheItemPolicyWithSlidingExpiratio yield return new object[] { "TestPolicySlidingSetToSecondsWithEnabledAttributeTrue", TimeSpan.FromSeconds(30), false }; yield return new object[] { "TestPolicySlidingWithoutEnabledAttribute", TimeSpan.FromMinutes(5), false }; } - } - } + } + + + [Theory, PropertyData("CanCreateCacheItemPolicyWithCacheEntriesProgrammaticallyTestData")] + public void CanCreateCacheItemPolicyWithCacheEntriesProgrammatically(string cacheItemPolicyName, ReadOnlyCollection cacheEntries, bool monitorShouldBeNull) + { + // Arrange + var expected = cacheEntries; + var factory = new CacheItemPolicyFactory(_configuration); + + // Act + var cacheItemPolicy = factory.Create(cacheItemPolicyName); + var entriesMonitor = cacheItemPolicy.ChangeMonitors.FirstOrDefault() as CacheEntryChangeMonitor; + + // Assert + if (monitorShouldBeNull) + { + entriesMonitor.ShouldBeNull(); + return; + } + + entriesMonitor.ShouldNotBeNull(); + entriesMonitor.ShouldImplement(); + + entriesMonitor.CacheKeys.ShouldEqual(expected); + } + + public static IEnumerable CanCreateCacheItemPolicyWithCacheEntriesProgrammaticallyTestData + { + get + { + yield return new object[] { "TestPolicyCacheEntries", new ReadOnlyCollection(new string[] { "aKeyToBeMonitored", "anotherOne", "andSoOn" }), false }; + yield return new object[] { "TestPolicyCacheEntriesEmpty", null, true }; + } + } + } } \ No newline at end of file diff --git a/src/CacheItemPolicyConfiguration.Tests/packages.config b/src/CacheItemPolicyConfiguration.Tests/packages.config index 7f96db5..37815e9 100644 --- a/src/CacheItemPolicyConfiguration.Tests/packages.config +++ b/src/CacheItemPolicyConfiguration.Tests/packages.config @@ -1,6 +1,7 @@ - - - - - + + + + + + \ No newline at end of file diff --git a/src/CacheItemPolicyConfiguration.sln b/src/CacheItemPolicyConfiguration.sln index 16636ba..b358797 100644 --- a/src/CacheItemPolicyConfiguration.sln +++ b/src/CacheItemPolicyConfiguration.sln @@ -1,35 +1,40 @@ - -Microsoft Visual Studio Solution File, Format Version 12.00 -# Visual Studio 2013 -VisualStudioVersion = 12.0.31101.0 -MinimumVisualStudioVersion = 10.0.40219.1 -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CacheItemPolicyConfiguration", "CacheItemPolicyConfiguration\CacheItemPolicyConfiguration.csproj", "{3E8FA116-5469-4247-9787-3B08B6736C02}" -EndProject -Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CacheItemPolicyConfiguration.Tests", "CacheItemPolicyConfiguration.Tests\CacheItemPolicyConfiguration.Tests.csproj", "{EC6ED966-52A6-4AEA-828D-75C2DFDAAB41}" -EndProject -Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".nuget", ".nuget", "{B771AAA4-C6B0-4A1E-8AD8-147BDF134FBE}" - ProjectSection(SolutionItems) = preProject - .nuget\NuGet.Config = .nuget\NuGet.Config - .nuget\NuGet.exe = .nuget\NuGet.exe - .nuget\NuGet.targets = .nuget\NuGet.targets - EndProjectSection -EndProject -Global - GlobalSection(SolutionConfigurationPlatforms) = preSolution - Debug|Any CPU = Debug|Any CPU - Release|Any CPU = Release|Any CPU - EndGlobalSection - GlobalSection(ProjectConfigurationPlatforms) = postSolution - {3E8FA116-5469-4247-9787-3B08B6736C02}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {3E8FA116-5469-4247-9787-3B08B6736C02}.Debug|Any CPU.Build.0 = Debug|Any CPU - {3E8FA116-5469-4247-9787-3B08B6736C02}.Release|Any CPU.ActiveCfg = Release|Any CPU - {3E8FA116-5469-4247-9787-3B08B6736C02}.Release|Any CPU.Build.0 = Release|Any CPU - {EC6ED966-52A6-4AEA-828D-75C2DFDAAB41}.Debug|Any CPU.ActiveCfg = Debug|Any CPU - {EC6ED966-52A6-4AEA-828D-75C2DFDAAB41}.Debug|Any CPU.Build.0 = Debug|Any CPU - {EC6ED966-52A6-4AEA-828D-75C2DFDAAB41}.Release|Any CPU.ActiveCfg = Release|Any CPU - {EC6ED966-52A6-4AEA-828D-75C2DFDAAB41}.Release|Any CPU.Build.0 = Release|Any CPU - EndGlobalSection - GlobalSection(SolutionProperties) = preSolution - HideSolutionNode = FALSE - EndGlobalSection -EndGlobal + +Microsoft Visual Studio Solution File, Format Version 12.00 +# Visual Studio 14 +VisualStudioVersion = 14.0.25420.1 +MinimumVisualStudioVersion = 10.0.40219.1 +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CacheItemPolicyConfiguration", "CacheItemPolicyConfiguration\CacheItemPolicyConfiguration.csproj", "{3E8FA116-5469-4247-9787-3B08B6736C02}" +EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CacheItemPolicyConfiguration.Tests", "CacheItemPolicyConfiguration.Tests\CacheItemPolicyConfiguration.Tests.csproj", "{EC6ED966-52A6-4AEA-828D-75C2DFDAAB41}" +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = ".nuget", ".nuget", "{B771AAA4-C6B0-4A1E-8AD8-147BDF134FBE}" + ProjectSection(SolutionItems) = preProject + .nuget\NuGet.Config = .nuget\NuGet.Config + .nuget\NuGet.exe = .nuget\NuGet.exe + .nuget\NuGet.targets = .nuget\NuGet.targets + EndProjectSection +EndProject +Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{09548298-B47B-49A7-994B-9A56AFADD1E2}" + ProjectSection(SolutionItems) = preProject + ..\ReadMe.md = ..\ReadMe.md + EndProjectSection +EndProject +Global + GlobalSection(SolutionConfigurationPlatforms) = preSolution + Debug|Any CPU = Debug|Any CPU + Release|Any CPU = Release|Any CPU + EndGlobalSection + GlobalSection(ProjectConfigurationPlatforms) = postSolution + {3E8FA116-5469-4247-9787-3B08B6736C02}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {3E8FA116-5469-4247-9787-3B08B6736C02}.Debug|Any CPU.Build.0 = Debug|Any CPU + {3E8FA116-5469-4247-9787-3B08B6736C02}.Release|Any CPU.ActiveCfg = Release|Any CPU + {3E8FA116-5469-4247-9787-3B08B6736C02}.Release|Any CPU.Build.0 = Release|Any CPU + {EC6ED966-52A6-4AEA-828D-75C2DFDAAB41}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {EC6ED966-52A6-4AEA-828D-75C2DFDAAB41}.Debug|Any CPU.Build.0 = Debug|Any CPU + {EC6ED966-52A6-4AEA-828D-75C2DFDAAB41}.Release|Any CPU.ActiveCfg = Release|Any CPU + {EC6ED966-52A6-4AEA-828D-75C2DFDAAB41}.Release|Any CPU.Build.0 = Release|Any CPU + EndGlobalSection + GlobalSection(SolutionProperties) = preSolution + HideSolutionNode = FALSE + EndGlobalSection +EndGlobal diff --git a/src/CacheItemPolicyConfiguration/CacheItemPolicyConfiguration.csproj b/src/CacheItemPolicyConfiguration/CacheItemPolicyConfiguration.csproj index 11525c7..6534a74 100644 --- a/src/CacheItemPolicyConfiguration/CacheItemPolicyConfiguration.csproj +++ b/src/CacheItemPolicyConfiguration/CacheItemPolicyConfiguration.csproj @@ -1,69 +1,71 @@ - - - - - Debug - AnyCPU - {3E8FA116-5469-4247-9787-3B08B6736C02} - Library - Properties - CacheItemPolicyConfiguration - CacheItemPolicyConfiguration - v4.5 - 512 - - - true - full - false - bin\Debug\ - DEBUG;TRACE - prompt - 4 - bin\Debug\CacheItemPolicyConfiguration.xml - - - pdbonly - true - bin\Release\ - TRACE - prompt - 4 - bin\Release\CacheItemPolicyConfiguration.xml - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + Debug + AnyCPU + {3E8FA116-5469-4247-9787-3B08B6736C02} + Library + Properties + CacheItemPolicyConfiguration + CacheItemPolicyConfiguration + v4.5 + 512 + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + bin\Debug\CacheItemPolicyConfiguration.xml + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + bin\Release\CacheItemPolicyConfiguration.xml + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + --> \ No newline at end of file diff --git a/src/CacheItemPolicyConfiguration/CacheItemPolicyConfigurationItem.cs b/src/CacheItemPolicyConfiguration/CacheItemPolicyConfigurationItem.cs index 872ded4..f159e94 100644 --- a/src/CacheItemPolicyConfiguration/CacheItemPolicyConfigurationItem.cs +++ b/src/CacheItemPolicyConfiguration/CacheItemPolicyConfigurationItem.cs @@ -1,5 +1,6 @@ using System; - +using System.Collections.Generic; + namespace CacheItemPolicyConfiguration { /// @@ -15,6 +16,20 @@ public CacheItemPolicyConfigurationItem() Enabled = true; } + /// + /// Initializes a new instance of the class. + /// + /// The name. + /// The cache keys to be monitored + /// if set to true [enabled]. + public CacheItemPolicyConfigurationItem(string name, IEnumerable cacheEntries, bool enabled = true) + { + Name = name; + CacheEntries = cacheEntries; + Enabled = enabled; + } + + /// /// Initializes a new instance of the class. /// @@ -83,5 +98,13 @@ public CacheItemPolicyConfigurationItem(string name, TimeSpan slidingExpiration, /// The sliding expiration. /// public TimeSpan SlidingExpiration { get; set; } - } + + /// + /// Gets a collection of cache keys that are monitored for changes. See . + /// + /// + /// The cache keys to be monitored. + /// + public IEnumerable CacheEntries { get; set; } + } } \ No newline at end of file diff --git a/src/CacheItemPolicyConfiguration/CacheItemPolicyHelpers.cs b/src/CacheItemPolicyConfiguration/CacheItemPolicyHelpers.cs index 5c50a17..6cfd7b3 100644 --- a/src/CacheItemPolicyConfiguration/CacheItemPolicyHelpers.cs +++ b/src/CacheItemPolicyConfiguration/CacheItemPolicyHelpers.cs @@ -1,4 +1,5 @@ using System; +using System.Linq; using System.Runtime.Caching; namespace CacheItemPolicyConfiguration @@ -38,6 +39,12 @@ public static CacheItemPolicy CreateCacheItemPolicy(ICacheItemPolicyConfiguratio policy.SlidingExpiration = configurationItem.SlidingExpiration; } + if (configurationItem.CacheEntries.Any()) + { + var monitor = MemoryCache.Default.CreateCacheEntryChangeMonitor(configurationItem.CacheEntries); + policy.ChangeMonitors.Add(monitor); + } + return policy; } diff --git a/src/CacheItemPolicyConfiguration/ConfigFile/CacheEntryConfigurationElement.cs b/src/CacheItemPolicyConfiguration/ConfigFile/CacheEntryConfigurationElement.cs new file mode 100644 index 0000000..94c89ec --- /dev/null +++ b/src/CacheItemPolicyConfiguration/ConfigFile/CacheEntryConfigurationElement.cs @@ -0,0 +1,25 @@ +using System; +using System.Collections.Generic; +using System.Configuration; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CacheItemPolicyConfiguration.ConfigFile +{ + /// + /// The configuration element contained in + /// + public class CacheEntryConfigurationElement : ConfigurationElement + { + /// + /// The key of a cache entry to be monitored + /// + [ConfigurationProperty("key")] + public string Key + { + get { return (string)base["key"]; } + set { base["key"] = value; } + } + } +} diff --git a/src/CacheItemPolicyConfiguration/ConfigFile/CacheEntryConfigurationElementCollection.cs b/src/CacheItemPolicyConfiguration/ConfigFile/CacheEntryConfigurationElementCollection.cs new file mode 100644 index 0000000..332792f --- /dev/null +++ b/src/CacheItemPolicyConfiguration/ConfigFile/CacheEntryConfigurationElementCollection.cs @@ -0,0 +1,54 @@ +using System; +using System.Collections.Generic; +using System.Configuration; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace CacheItemPolicyConfiguration.ConfigFile +{ + public class CacheEntryConfigurationElementCollection : ConfigurationElementCollection + { + private static readonly string _ElementName = "cacheEntry"; + + + public override ConfigurationElementCollectionType CollectionType + { + get { return ConfigurationElementCollectionType.BasicMapAlternate; } + } + + /// + /// Gets the name used to identify this collection of elements in the configuration file when overridden in a derived class. + /// + /// + /// The name of the collection; otherwise, an empty string. The default is an empty + /// + protected override string ElementName + { + get { return _ElementName; } + } + + /// + /// When overridden in a derived class, creates a new . + /// + /// + /// A new . + /// + protected override ConfigurationElement CreateNewElement() + { + return new CacheEntryConfigurationElement(); + } + + /// + /// Gets the element key for a specified configuration element when overridden in a derived class. + /// + /// + /// An that acts as the key for the specified . + /// + /// The to return the key for. + protected override object GetElementKey(ConfigurationElement element) + { + return ((CacheEntryConfigurationElement)element).Key; + } + } +} diff --git a/src/CacheItemPolicyConfiguration/ConfigFile/CacheItemPolicyConfigurationElement.cs b/src/CacheItemPolicyConfiguration/ConfigFile/CacheItemPolicyConfigurationElement.cs index 15302a1..2009900 100644 --- a/src/CacheItemPolicyConfiguration/ConfigFile/CacheItemPolicyConfigurationElement.cs +++ b/src/CacheItemPolicyConfiguration/ConfigFile/CacheItemPolicyConfigurationElement.cs @@ -1,4 +1,6 @@ using System; +using System.Linq; +using System.Collections.Generic; using System.Configuration; namespace CacheItemPolicyConfiguration.ConfigFile @@ -12,12 +14,13 @@ public class CacheItemPolicyConfigurationElement : ConfigurationElement, ICacheI private const string NameAttributeName = "name"; private const string AbsoluteExpirationAttributeName = "absoluteExpiration"; private const string SlidingExpirationAttributeName = "slidingExpiration"; - - /// - /// Gets a value indicating whether the item is enabled. - /// - /// true if enabled; otherwise, false. - [ConfigurationProperty(EnabledAttributeName, DefaultValue = true, IsRequired = false)] + private const string CacheEntriesAttributeNAme = "cacheEntries"; + + /// + /// Gets a value indicating whether the item is enabled. + /// + /// true if enabled; otherwise, false. + [ConfigurationProperty(EnabledAttributeName, DefaultValue = true, IsRequired = false)] public bool Enabled { get { return (bool)this[EnabledAttributeName]; } @@ -77,6 +80,37 @@ public string SlidingExpiration TimeSpan ICacheItemPolicyConfigurationItem.SlidingExpiration { get { return CacheItemPolicyHelpers.ParseSlidingExpiration(SlidingExpiration); } - } - } + } + + + /// + /// Gets a collection of cache keys that are monitored for changes. See . + /// + /// + /// The cache key to be monitored. + /// + [ConfigurationProperty(CacheEntriesAttributeNAme, IsRequired = false)] + public CacheEntryConfigurationElementCollection CacheEntries + { + get { return this[CacheEntriesAttributeNAme] as CacheEntryConfigurationElementCollection; } + } + + /// + /// Gets a collection of cache keys that are monitored for changes. See . + /// + /// + /// The cache keys to be monitored. + /// + IEnumerable ICacheItemPolicyConfigurationItem.CacheEntries + { + get + { + var entries = CacheEntries; + if (entries == null) + return new string[0]; + + return entries.Cast().Select(x => x.Key); + } + } + } } \ No newline at end of file diff --git a/src/CacheItemPolicyConfiguration/ConfigFile/SampleConfigFiles/App.config b/src/CacheItemPolicyConfiguration/ConfigFile/SampleConfigFiles/App.config index 58d3ad7..7a5f105 100644 --- a/src/CacheItemPolicyConfiguration/ConfigFile/SampleConfigFiles/App.config +++ b/src/CacheItemPolicyConfiguration/ConfigFile/SampleConfigFiles/App.config @@ -4,6 +4,12 @@
- + + + + + + + \ No newline at end of file diff --git a/src/CacheItemPolicyConfiguration/ICacheItemPolicyConfigurationItem.cs b/src/CacheItemPolicyConfiguration/ICacheItemPolicyConfigurationItem.cs index 1ada8a0..f8cd437 100644 --- a/src/CacheItemPolicyConfiguration/ICacheItemPolicyConfigurationItem.cs +++ b/src/CacheItemPolicyConfiguration/ICacheItemPolicyConfigurationItem.cs @@ -1,5 +1,6 @@ using System; - +using System.Collections.Generic; + namespace CacheItemPolicyConfiguration { /// @@ -38,5 +39,13 @@ public interface ICacheItemPolicyConfigurationItem /// The sliding expiration. /// TimeSpan SlidingExpiration { get; } + + /// + /// Gets a collection of cache keys that are monitored for changes. See . + /// + /// + /// The cache keys to be monitored. + /// + IEnumerable CacheEntries { get; } } } \ No newline at end of file