Skip to content

Commit 46f4508

Browse files
committed
#283 Adding initial implementation for removing entries from the memory cache.
TODO: - [] need to add more tests - [] need to add comments
1 parent 19780c5 commit 46f4508

File tree

13 files changed

+203
-46
lines changed

13 files changed

+203
-46
lines changed

src/MyTested.AspNetCore.Mvc.Caching/Builders/Contracts/Data/IAndMemoryCacheBuilder.cs renamed to src/MyTested.AspNetCore.Mvc.Caching/Builders/Contracts/Data/IAndWithMemoryCacheBuilder.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
/// <summary>
44
/// Used for adding AndAlso() method to the <see cref="Microsoft.Extensions.Caching.Memory.IMemoryCache"/> builder.
55
/// </summary>
6-
public interface IAndMemoryCacheBuilder : IMemoryCacheBuilder
6+
public interface IAndWithMemoryCacheBuilder : IWithMemoryCacheBuilder
77
{
88
/// <summary>
99
/// AndAlso method for better readability when building <see cref="Microsoft.Extensions.Caching.Memory.IMemoryCache"/>.
1010
/// </summary>
11-
/// <returns>The same <see cref="IMemoryCacheBuilder"/>.</returns>
12-
IMemoryCacheBuilder AndAlso();
11+
/// <returns>The same <see cref="IWithMemoryCacheBuilder"/>.</returns>
12+
IWithMemoryCacheBuilder AndAlso();
1313
}
1414
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace MyTested.AspNetCore.Mvc.Builders.Contracts.Data
2+
{
3+
public interface IAndWithoutMemoryCacheBuilder : IWithoutMemoryCacheBuilder
4+
{
5+
IWithoutMemoryCacheBuilder AndAlso();
6+
}
7+
}

src/MyTested.AspNetCore.Mvc.Caching/Builders/Contracts/Data/IMemoryCacheTestBuilder.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,38 +13,38 @@ public interface IMemoryCacheTestBuilder
1313
/// Tests whether the <see cref="IMemoryCache"/> contains entry with the provided key.
1414
/// </summary>
1515
/// <param name="key">Key of the cache entry.</param>
16-
/// <returns>The same <see cref="IAndMemoryCacheBuilder"/>.</returns>
16+
/// <returns>The same <see cref="IAndWithMemoryCacheBuilder"/>.</returns>
1717
IAndMemoryCacheTestBuilder ContainingEntryWithKey(object key);
1818

1919
/// <summary>
2020
/// Tests whether the <see cref="IMemoryCache"/> contains entry with the provided value.
2121
/// </summary>
2222
/// <typeparam name="TValue">Type of the cache entry value.</typeparam>
2323
/// <param name="value">Value of the cache entry.</param>
24-
/// <returns>The same <see cref="IAndMemoryCacheBuilder"/>.</returns>
24+
/// <returns>The same <see cref="IAndWithMemoryCacheBuilder"/>.</returns>
2525
IAndMemoryCacheTestBuilder ContainingEntryWithValue<TValue>(TValue value);
2626

2727
/// <summary>
2828
/// Tests whether the <see cref="IMemoryCache"/> contains entry with value of the provided type.
2929
/// </summary>
3030
/// <typeparam name="TValue">Type of the cache entry value.</typeparam>
31-
/// <returns>The same <see cref="IAndMemoryCacheBuilder"/>.</returns>
31+
/// <returns>The same <see cref="IAndWithMemoryCacheBuilder"/>.</returns>
3232
IAndMemoryCacheTestBuilder ContainingEntryOfType<TValue>();
3333

3434
/// <summary>
3535
/// Tests whether the <see cref="IMemoryCache"/> contains entry with the provided key and corresponding value.
3636
/// </summary>
3737
/// <param name="key">Key of the cache entry.</param>
3838
/// <param name="value">Value of the cache entry.</param>
39-
/// <returns>The same <see cref="IAndMemoryCacheBuilder"/>.</returns>
39+
/// <returns>The same <see cref="IAndWithMemoryCacheBuilder"/>.</returns>
4040
IAndMemoryCacheTestBuilder ContainingEntry(object key, object value);
4141

4242
/// <summary>
4343
/// Tests whether the <see cref="IMemoryCache"/> contains entry with value of the provided type and the given key.
4444
/// </summary>
4545
/// <typeparam name="TValue">Type of the cache entry value.</typeparam>
4646
/// <param name="key">Key of the cache entry.</param>
47-
/// <returns>The same <see cref="IAndMemoryCacheBuilder"/>.</returns>
47+
/// <returns>The same <see cref="IAndWithMemoryCacheBuilder"/>.</returns>
4848
IAndMemoryCacheTestBuilder ContainingEntryOfType<TValue>(object key);
4949

5050
/// <summary>
@@ -53,21 +53,21 @@ public interface IMemoryCacheTestBuilder
5353
/// <param name="key">Key of the cache entry.</param>
5454
/// <param name="value">Value of the cache entry.</param>
5555
/// <param name="options"><see cref="MemoryCacheEntryOptions"/> of the cache entry.</param>
56-
/// <returns>The same <see cref="IAndMemoryCacheBuilder"/>.</returns>
56+
/// <returns>The same <see cref="IAndWithMemoryCacheBuilder"/>.</returns>
5757
IAndMemoryCacheTestBuilder ContainingEntry(object key, object value, MemoryCacheEntryOptions options);
5858

5959
/// <summary>
6060
/// Tests whether the <see cref="IMemoryCache"/> contains specific entry by using a builder.
6161
/// </summary>
6262
/// <param name="memoryCacheEntryTestBuilder">Builder for setting specific cache entry tests.</param>
63-
/// <returns>The same <see cref="IAndMemoryCacheBuilder"/>.</returns>
63+
/// <returns>The same <see cref="IAndWithMemoryCacheBuilder"/>.</returns>
6464
IAndMemoryCacheTestBuilder ContainingEntry(Action<IMemoryCacheEntryKeyTestBuilder> memoryCacheEntryTestBuilder);
6565

6666
/// <summary>
6767
/// Tests whether the <see cref="IMemoryCache"/> contains the provided entries.
6868
/// </summary>
6969
/// <param name="entries">Dictionary of cache entries.</param>
70-
/// <returns>The same <see cref="IAndMemoryCacheBuilder"/>.</returns>
70+
/// <returns>The same <see cref="IAndWithMemoryCacheBuilder"/>.</returns>
7171
IAndMemoryCacheTestBuilder ContainingEntries(IDictionary<object, object> entries);
7272
}
7373
}

src/MyTested.AspNetCore.Mvc.Caching/Builders/Contracts/Data/IMemoryCacheBuilder.cs renamed to src/MyTested.AspNetCore.Mvc.Caching/Builders/Contracts/Data/IWithMemoryCacheBuilder.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,37 +7,37 @@
77
/// <summary>
88
/// Used for building mocked <see cref="IMemoryCache"/>.
99
/// </summary>
10-
public interface IMemoryCacheBuilder
10+
public interface IWithMemoryCacheBuilder
1111
{
1212
/// <summary>
1313
/// Adds cache entry to the mocked <see cref="IMemoryCache"/>.
1414
/// </summary>
1515
/// <param name="key">Key of the cache entry.</param>
1616
/// <param name="value">Value of the cache entry.</param>
17-
/// <returns>The same <see cref="IAndMemoryCacheBuilder"/>.</returns>
18-
IAndMemoryCacheBuilder WithEntry(object key, object value);
17+
/// <returns>The same <see cref="IAndWithMemoryCacheBuilder"/>.</returns>
18+
IAndWithMemoryCacheBuilder WithEntry(object key, object value);
1919

2020
/// <summary>
2121
/// Adds cache entry to the mocked <see cref="IMemoryCache"/>.
2222
/// </summary>
2323
/// <param name="key">Key of the cache entry.</param>
2424
/// <param name="value">Value of the cache entry.</param>
2525
/// <param name="options"><see cref="MemoryCacheEntryOptions"/> of the cache entry.</param>
26-
/// <returns>The same <see cref="IAndMemoryCacheBuilder"/>.</returns>
27-
IAndMemoryCacheBuilder WithEntry(object key, object value, MemoryCacheEntryOptions options);
26+
/// <returns>The same <see cref="IAndWithMemoryCacheBuilder"/>.</returns>
27+
IAndWithMemoryCacheBuilder WithEntry(object key, object value, MemoryCacheEntryOptions options);
2828

2929
/// <summary>
3030
/// Adds cache entry to the mocked <see cref="IMemoryCache"/>.
3131
/// </summary>
3232
/// <param name="memoryCacheEntryBuilder">Builder for creating cache entry.</param>
33-
/// <returns>The same <see cref="IAndMemoryCacheBuilder"/>.</returns>
34-
IAndMemoryCacheBuilder WithEntry(Action<IMemoryCacheEntryKeyBuilder> memoryCacheEntryBuilder);
33+
/// <returns>The same <see cref="IAndWithMemoryCacheBuilder"/>.</returns>
34+
IAndWithMemoryCacheBuilder WithEntry(Action<IMemoryCacheEntryKeyBuilder> memoryCacheEntryBuilder);
3535

3636
/// <summary>
3737
/// Adds cache entries to the mocked <see cref="IMemoryCache"/>.
3838
/// </summary>
3939
/// <param name="entries">Dictionary of cache entries.</param>
40-
/// <returns>The same <see cref="IAndMemoryCacheBuilder"/>.</returns>
41-
IAndMemoryCacheBuilder WithEntries(IDictionary<object, object> entries);
40+
/// <returns>The same <see cref="IAndWithMemoryCacheBuilder"/>.</returns>
41+
IAndWithMemoryCacheBuilder WithEntries(IDictionary<object, object> entries);
4242
}
4343
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
namespace MyTested.AspNetCore.Mvc.Builders.Contracts.Data
2+
{
3+
using System.Collections.Generic;
4+
5+
public interface IWithoutMemoryCacheBuilder
6+
{
7+
IAndWithoutMemoryCacheBuilder WithoutEntry(object key);
8+
9+
IAndWithoutMemoryCacheBuilder WithoutEntries(IEnumerable<object> keys);
10+
11+
IAndWithoutMemoryCacheBuilder ClearCache();
12+
}
13+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
namespace MyTested.AspNetCore.Mvc.Builders.Data
2+
{
3+
using Microsoft.Extensions.Caching.Memory;
4+
using Microsoft.Extensions.DependencyInjection;
5+
using System;
6+
7+
public abstract class MemoryCacheBaseBuilder
8+
{
9+
public MemoryCacheBaseBuilder(IServiceProvider services)
10+
{
11+
this.MemoryCache = services.GetRequiredService<IMemoryCache>();
12+
}
13+
14+
protected IMemoryCache MemoryCache { get; private set; }
15+
}
16+
}

src/MyTested.AspNetCore.Mvc.Caching/Builders/Data/MemoryCacheBuilder.cs renamed to src/MyTested.AspNetCore.Mvc.Caching/Builders/Data/MemoryCacheWithBuilder.cs

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,45 +4,35 @@
44
using System.Collections.Generic;
55
using Contracts.Data;
66
using Microsoft.Extensions.Caching.Memory;
7-
using Microsoft.Extensions.DependencyInjection;
87
using Utilities.Extensions;
98

109
/// <summary>
1110
/// Used for building mocked <see cref="IMemoryCache"/>.
1211
/// </summary>
13-
public class MemoryCacheBuilder : IAndMemoryCacheBuilder
12+
public class MemoryCacheWithBuilder : MemoryCacheBaseBuilder, IAndWithMemoryCacheBuilder
1413
{
1514
/// <summary>
16-
/// Initializes a new instance of the <see cref="MemoryCacheBuilder"/> class.
15+
/// Initializes a new instance of the <see cref="MemoryCacheWithBuilder"/> class.
1716
/// </summary>
1817
/// <param name="services"><see cref="IServiceProvider"/> providing the current <see cref="IMemoryCache"/>.</param>
19-
public MemoryCacheBuilder(IServiceProvider services)
20-
{
21-
this.MemoryCache = services.GetRequiredService<IMemoryCache>();
22-
}
23-
24-
/// <summary>
25-
/// Gets the mocked <see cref="IMemoryCache"/>.
26-
/// </summary>
27-
/// <value>Built <see cref="IMemoryCache"/>.</value>
28-
protected IMemoryCache MemoryCache { get; private set; }
18+
public MemoryCacheWithBuilder(IServiceProvider services) : base(services) { }
2919

3020
/// <inheritdoc />
31-
public IAndMemoryCacheBuilder WithEntry(object key, object value)
21+
public IAndWithMemoryCacheBuilder WithEntry(object key, object value)
3222
{
3323
this.MemoryCache.Set(key, value);
3424
return this;
3525
}
3626

3727
/// <inheritdoc />
38-
public IAndMemoryCacheBuilder WithEntry(object key, object value, MemoryCacheEntryOptions options)
28+
public IAndWithMemoryCacheBuilder WithEntry(object key, object value, MemoryCacheEntryOptions options)
3929
{
4030
this.MemoryCache.Set(key, value, options);
4131
return this;
4232
}
4333

4434
/// <inheritdoc />
45-
public IAndMemoryCacheBuilder WithEntry(Action<IMemoryCacheEntryKeyBuilder> memoryCacheEntryBuilder)
35+
public IAndWithMemoryCacheBuilder WithEntry(Action<IMemoryCacheEntryKeyBuilder> memoryCacheEntryBuilder)
4636
{
4737
var newMemoryCacheEntryBuilder = new MemoryCacheEntryBuilder();
4838
memoryCacheEntryBuilder(newMemoryCacheEntryBuilder);
@@ -58,13 +48,13 @@ public IAndMemoryCacheBuilder WithEntry(Action<IMemoryCacheEntryKeyBuilder> memo
5848
}
5949

6050
/// <inheritdoc />
61-
public IAndMemoryCacheBuilder WithEntries(IDictionary<object, object> entries)
51+
public IAndWithMemoryCacheBuilder WithEntries(IDictionary<object, object> entries)
6252
{
6353
entries.ForEach(e => this.WithEntry(e.Key, e.Value));
6454
return this;
6555
}
6656

6757
/// <inheritdoc />
68-
public IMemoryCacheBuilder AndAlso() => this;
58+
public IWithMemoryCacheBuilder AndAlso() => this;
6959
}
7060
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
namespace MyTested.AspNetCore.Mvc.Builders.Data
2+
{
3+
using System;
4+
using System.Collections.Generic;
5+
using Contracts.Data;
6+
using Utilities.Extensions;
7+
8+
public class MemoryCacheWithoutBuilder : MemoryCacheBaseBuilder, IAndWithoutMemoryCacheBuilder
9+
{
10+
public MemoryCacheWithoutBuilder(IServiceProvider services) : base(services) { }
11+
12+
public IAndWithoutMemoryCacheBuilder WithoutEntry(object key)
13+
{
14+
this.MemoryCache.Remove(key);
15+
return this;
16+
}
17+
18+
public IAndWithoutMemoryCacheBuilder WithoutEntries(IEnumerable<object> keys)
19+
{
20+
this.MemoryCache.AsMemoryCacheMock().RemoveKeys(keys);
21+
return this;
22+
}
23+
24+
public IAndWithoutMemoryCacheBuilder ClearCache()
25+
{
26+
this.MemoryCache.AsMemoryCacheMock().ClearCache();
27+
return this;
28+
}
29+
30+
public IWithoutMemoryCacheBuilder AndAlso() => this;
31+
}
32+
}

src/MyTested.AspNetCore.Mvc.Caching/ComponentBuilderCachingExtensions.cs

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using Builders.Contracts.Data;
66
using Builders.Data;
77
using Builders.Base;
8+
using System.Collections.Generic;
89

910
/// <summary>
1011
/// Contains <see cref="Microsoft.Extensions.Caching.Memory.IMemoryCache"/> extension methods for <see cref="IBaseTestBuilderWithComponentBuilder{TBuilder}"/>.
@@ -16,16 +17,45 @@ public static class ComponentBuilderCachingExtensions
1617
/// </summary>
1718
/// <typeparam name="TBuilder">Class representing ASP.NET Core MVC test builder.</typeparam>
1819
/// <param name="builder">Instance of <see cref="IBaseTestBuilderWithComponentBuilder{TBuilder}"/> type.</param>
19-
/// <param name="memoryCacheBuilder">Action setting the <see cref="Microsoft.Extensions.Caching.Memory.IMemoryCache"/> values by using <see cref="IMemoryCacheBuilder"/>.</param>
20+
/// <param name="memoryCacheBuilder">Action setting the <see cref="Microsoft.Extensions.Caching.Memory.IMemoryCache"/> values by using <see cref="IWithMemoryCacheBuilder"/>.</param>
2021
/// <returns>The same component builder.</returns>
2122
public static TBuilder WithMemoryCache<TBuilder>(
2223
this IBaseTestBuilderWithComponentBuilder<TBuilder> builder,
23-
Action<IMemoryCacheBuilder> memoryCacheBuilder)
24+
Action<IWithMemoryCacheBuilder> memoryCacheBuilder)
2425
where TBuilder : IBaseTestBuilder
2526
{
2627
var actualBuilder = (BaseTestBuilderWithComponentBuilder<TBuilder>)builder;
2728

28-
memoryCacheBuilder(new MemoryCacheBuilder(actualBuilder.TestContext.HttpContext.RequestServices));
29+
memoryCacheBuilder(new MemoryCacheWithBuilder(actualBuilder.TestContext.HttpContext.RequestServices));
30+
31+
return actualBuilder.Builder;
32+
}
33+
34+
public static TBuilder WithoutMemoryCache<TBuilder>(
35+
this IBaseTestBuilderWithComponentBuilder<TBuilder> builder)
36+
where TBuilder : IBaseTestBuilder
37+
=> builder.WithoutMemoryCache(cache => cache.ClearCache());
38+
39+
public static TBuilder WithoutMemoryCache<TBuilder>(
40+
this IBaseTestBuilderWithComponentBuilder<TBuilder> builder,
41+
object key)
42+
where TBuilder : IBaseTestBuilder
43+
=> builder.WithoutMemoryCache(cache => cache.WithoutEntry(key));
44+
45+
public static TBuilder WithoutMemoryCache<TBuilder>(
46+
this IBaseTestBuilderWithComponentBuilder<TBuilder> builder,
47+
IEnumerable<object> keys)
48+
where TBuilder : IBaseTestBuilder
49+
=> builder.WithoutMemoryCache(cache => cache.WithoutEntries(keys));
50+
51+
public static TBuilder WithoutMemoryCache<TBuilder>(
52+
this IBaseTestBuilderWithComponentBuilder<TBuilder> builder,
53+
Action<IWithoutMemoryCacheBuilder> memoryCacheBuilder)
54+
where TBuilder : IBaseTestBuilder
55+
{
56+
var actualBuilder = (BaseTestBuilderWithComponentBuilder<TBuilder>)builder;
57+
58+
memoryCacheBuilder(new MemoryCacheWithoutBuilder(actualBuilder.TestContext.HttpContext.RequestServices));
2959

3060
return actualBuilder.Builder;
3161
}

src/MyTested.AspNetCore.Mvc.Caching/Internal/Caching/MemoryCacheMock.cs

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,10 @@ private static readonly TestLocal<IDictionary<object, ICacheEntry>> Current
1212

1313
private readonly IDictionary<object, ICacheEntry> cache;
1414

15-
public MemoryCacheMock()
15+
public MemoryCacheMock()
1616
=> this.cache = this.GetCurrentCache();
1717

1818
public int Count => this.cache.Count;
19-
20-
public void Dispose()
21-
=> this.cache.Clear();
2219

2320
public void Remove(object key)
2421
{
@@ -59,9 +56,23 @@ public bool TryGetCacheEntry(object key, out ICacheEntry value)
5956
return false;
6057
}
6158

62-
public IDictionary<object, object> GetCacheAsDictionary()
59+
public IDictionary<object, object> GetCacheAsDictionary()
6360
=> this.cache.ToDictionary(c => c.Key, c => c.Value.Value);
6461

62+
public void RemoveKeys(IEnumerable<object> keys)
63+
{
64+
foreach (var key in keys)
65+
{
66+
this.Remove(key);
67+
}
68+
}
69+
70+
public void ClearCache()
71+
=> this.cache.Clear();
72+
73+
public void Dispose()
74+
=> this.ClearCache();
75+
6576
private IDictionary<object, ICacheEntry> GetCurrentCache()
6677
{
6778
var result = Current.Value;

0 commit comments

Comments
 (0)