Skip to content

Commit 72868a6

Browse files
feat(ICacheManager): add Keys parameter (#5158)
* feat(ICacheManager): add Keys parameter * test: 更新单元测试 Co-Authored-By: Alex chow <[email protected]>
1 parent c794a1e commit 72868a6

File tree

3 files changed

+49
-0
lines changed

3 files changed

+49
-0
lines changed

src/BootstrapBlazor/Services/CacheManager.cs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,40 @@ public DateTimeOffset GetStartTime()
139139
return ret;
140140
}
141141

142+
/// <summary>
143+
/// 获得 缓存数量
144+
/// </summary>
145+
public long Count
146+
{
147+
get
148+
{
149+
var count = 0;
150+
if (Cache is MemoryCache c)
151+
{
152+
count = c.Count;
153+
}
154+
return count;
155+
}
156+
}
157+
158+
#if NET9_0_OR_GREATER
159+
/// <summary>
160+
/// 获得 缓存键集合
161+
/// </summary>
162+
public IEnumerable<object> Keys
163+
{
164+
get
165+
{
166+
var keys = Enumerable.Empty<object>();
167+
if (Cache is MemoryCache c)
168+
{
169+
keys = c.Keys;
170+
}
171+
return keys;
172+
}
173+
}
174+
#endif
175+
142176
#region Count
143177
public static int ElementCount(object? value)
144178
{

src/BootstrapBlazor/Services/ICacheManager.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,4 +55,16 @@ public interface ICacheManager
5555
/// </summary>
5656
/// <param name="key"></param>
5757
void Clear(object? key = null);
58+
59+
/// <summary>
60+
/// 获得 缓存数量
61+
/// </summary>
62+
long Count { get; }
63+
64+
#if NET9_0_OR_GREATER
65+
/// <summary>
66+
/// 获得 缓存键集合
67+
/// </summary>
68+
IEnumerable<object> Keys { get; }
69+
#endif
5870
}

test/UnitTest/Services/CacheManagerTest.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ public void GetStartTime_Ok()
2424
Cache.Clear("BootstrapBlazor_StartTime");
2525
Cache.SetStartTime();
2626
Assert.True(DateTime.Now > Cache.GetStartTime());
27+
28+
Assert.Equal(1, Cache.Count);
29+
Assert.Single(Cache.Keys);
2730
}
2831

2932
[Fact]

0 commit comments

Comments
 (0)