Skip to content

Tests for RemoveAllAsync + some tweaks to cache maintenance. #377

Merged
niemyjski merged 2 commits intomainfrom
perf/cache-remove-all
Apr 24, 2025
Merged

Tests for RemoveAllAsync + some tweaks to cache maintenance. #377
niemyjski merged 2 commits intomainfrom
perf/cache-remove-all

Conversation

@niemyjski
Copy link
Copy Markdown
Member

No description provided.

@niemyjski niemyjski requested a review from ejsmith April 23, 2025 20:36
@niemyjski niemyjski self-assigned this Apr 23, 2025
await cache.SetAsync("flag", true);
Assert.Equal(13422, (await cache.GetAsync<int>("test")).Value);
Assert.Null(await cache.GetAsync<int>("test2"));
Assert.False((await cache.GetAsync<int>("test2")).HasValue);
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These tests were skipped so I think this is just something that was broken as GetAsync always returns a cache value and never null

await CompactAsync().AnyContext();

if (TimeSpan.FromMilliseconds(100) < utcNow - _lastMaintenance)
if (TimeSpan.FromMilliseconds(250) < utcNow - _lastMaintenance)
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bumped the time a bit, feels like we could even raise it higher, more of an issue with high operation count, this was really up in the performance profiler... There are several issues already with this method like not making sure it's already running, race conditions with setting _lastMaintenance.

Comment on lines +1024 to +1025
var expiresAt = kvp.Value.ExpiresAt;
if (expiresAt < DateTime.MaxValue && expiresAt <= utcNow)
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

image

Just trying to cut down a tiny bit on number of times expires is called.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR expands testing for cache removal functionality and refines cache maintenance behavior.

  • Enabled tests for RemoveAll and RemoveAllKeys methods across multiple test files.
  • Adjusted maintenance timing and compact logic in InMemoryCacheClient to conditionally compact the cache.
  • Enhanced error logging by appending exception messages.

Reviewed Changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated no comments.

Show a summary per file
File Description
tests/Foundatio.Tests/Caching/InMemoryHybridCacheClientTests.cs Added tests for CanRemoveAllAsync and CanRemoveAllKeysAsync.
tests/Foundatio.Tests/Caching/InMemoryCacheClientTests.cs Added similar removal tests and updated comments.
src/Foundatio/Caching/InMemoryCacheClient.cs Tweaked maintenance conditions and logging enhancements.
src/Foundatio.TestHarness/Caching/HybridCacheClientTests.cs Added removal tests for hybrid cache functionality.
src/Foundatio.TestHarness/Caching/CacheClientTestsBase.cs Added implementations for RemoveAll and RemoveAllKeys tests, and adjusted assertions.
Comments suppressed due to low confidence (2)

src/Foundatio/Caching/InMemoryCacheClient.cs:1021

  • Ensure that gating the expiration removal solely on infrequent access does not bypass removal for frequently accessed but expired items.
if (!lastAccessTimeIsInfrequent) continue;

src/Foundatio/Caching/InMemoryCacheClient.cs:960

  • Please confirm that increasing the maintenance threshold from 100ms to 250ms aligns with performance expectations, as this change may delay the removal of expired entries.
if (TimeSpan.FromMilliseconds(250) < utcNow - _lastMaintenance)

@ejsmith ejsmith changed the title Remove All Tests + some tweaks to cache maintenance. Tests for RemoveAllAsync + some tweaks to cache maintenance. Apr 23, 2025
@niemyjski niemyjski merged commit a3a4e47 into main Apr 24, 2025
3 checks passed
@niemyjski niemyjski deleted the perf/cache-remove-all branch April 24, 2025 04:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants