|
| 1 | +namespace DotNetToolkit.Repository.Caching.InMemory |
| 2 | +{ |
| 3 | + using Configuration.Options; |
| 4 | + using JetBrains.Annotations; |
| 5 | + using System; |
| 6 | + using Utility; |
| 7 | + |
| 8 | + /// <summary> |
| 9 | + /// Contains various extension methods for <see cref="RepositoryOptionsBuilder" /> |
| 10 | + /// </summary> |
| 11 | + public static class RepositoryOptionsBuilderExtensions |
| 12 | + { |
| 13 | + /// <summary> |
| 14 | + /// Configures the caching provider to use microsoft's in-memory cache. |
| 15 | + /// </summary> |
| 16 | + /// <param name="source">The repository options builder.</param> |
| 17 | + /// <returns>The same builder instance.</returns> |
| 18 | + public static RepositoryOptionsBuilder UseInMemoryCache([NotNull] this RepositoryOptionsBuilder source) |
| 19 | + { |
| 20 | + Guard.NotNull(source, nameof(source)); |
| 21 | + |
| 22 | + source.UseCachingProvider(new InMemoryCacheProvider()); |
| 23 | + |
| 24 | + return source; |
| 25 | + } |
| 26 | + |
| 27 | + /// <summary> |
| 28 | + /// Configures the caching provider to use the in-memory cache. |
| 29 | + /// </summary> |
| 30 | + /// <param name="source">The repository options builder.</param> |
| 31 | + /// <param name="optionsAction">The options action.</param> |
| 32 | + /// <returns>The same builder instance.</returns> |
| 33 | + public static RepositoryOptionsBuilder UseInMemoryCache([NotNull] this RepositoryOptionsBuilder source, [NotNull] Action<InMemoryCacheOptions> optionsAction) |
| 34 | + { |
| 35 | + Guard.NotNull(source, nameof(source)); |
| 36 | + Guard.NotNull(optionsAction, nameof(optionsAction)); |
| 37 | + |
| 38 | + var options = new InMemoryCacheOptions(); |
| 39 | + |
| 40 | + optionsAction(options); |
| 41 | + |
| 42 | + var provider = new InMemoryCacheProvider( |
| 43 | + options.Clock, |
| 44 | + options.ExpirationScanFrequency, |
| 45 | + options.Expiry); |
| 46 | + |
| 47 | + source.UseCachingProvider(provider); |
| 48 | + |
| 49 | + return source; |
| 50 | + } |
| 51 | + } |
| 52 | +} |
0 commit comments