Skip to content

Conversation

@definitelynotagoblin
Copy link
Contributor

@definitelynotagoblin definitelynotagoblin commented Jul 2, 2025

Description

Invalidate cache when version changed, or when RebuildCache command flag specified.

Motivation and Context

https://specterops.atlassian.net/browse/BED-6140

How Has This Been Tested?

On lab VM

Screenshots (if appropriate):

image image

Types of changes

  • Chore (a change that does not modify the application functionality)
  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to change)

Checklist:

  • Documentation updates are needed, and have been made accordingly.
  • I have added and/or updated tests to cover my changes.
  • All new and existing tests passed.
  • My changes include a database migration.

Summary by CodeRabbit

  • New Features

    • Improved cache management with automatic invalidation based on cache age (older than 30 days) or outdated version.
    • Enhanced logging of cache statistics during loading.
  • Bug Fixes

    • Improved consistency in exception handling and formatting throughout the application.

@definitelynotagoblin definitelynotagoblin self-assigned this Jul 2, 2025
@definitelynotagoblin definitelynotagoblin added bug Something isn't working enhancement New feature or request labels Jul 2, 2025
@coderabbitai
Copy link

coderabbitai bot commented Jul 2, 2025

Walkthrough

The update to src/SharpLinks.cs introduces assembly version retrieval, refines cache management by adding age and version-based invalidation, and improves code formatting. A new private method checks if the cache should be invalidated. Some logging and early return logic in cache saving have been commented out.

Changes

File(s) Change Summary
src/SharpLinks.cs Added assembly version retrieval, enhanced cache invalidation logic (age/version), improved formatting, introduced CacheNeedsInvalidation, and commented out select cache save logic.

Poem

In the warren of code, a cache now gets wise,
With version and age, it checks for disguise.
If thirty days pass, or old code’s in play,
A new cache will hop in, without much delay.
🐇✨
SharpLinks grows sharper, in a bunny-bright way!

✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@definitelynotagoblin definitelynotagoblin changed the title fix: Add cache invalidation [BED-6140] Add cache invalidation Jul 3, 2025
@definitelynotagoblin definitelynotagoblin marked this pull request as ready for review July 3, 2025 19:07
Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 0

🧹 Nitpick comments (2)
src/SharpLinks.cs (2)

176-187: Effective cache invalidation strategy implemented.

The CacheNeedsInvalidation method implements a sound invalidation strategy:

  • Age-based invalidation (30 days) prevents stale cache usage
  • Version-based invalidation ensures cache compatibility with application updates
  • Proper null checking prevents runtime exceptions

However, consider making the 30-day threshold configurable for different deployment scenarios.

Consider making the cache age threshold configurable:

-private bool CacheNeedsInvalidation(Cache cache, Version version) {
-    var threshold = DateTime.Now.Subtract(TimeSpan.FromDays(30));
+private bool CacheNeedsInvalidation(Cache cache, Version version, int maxCacheAgeDays = 30) {
+    var threshold = DateTime.Now.Subtract(TimeSpan.FromDays(maxCacheAgeDays));

349-353: Remove commented code instead of leaving it in place.

The commented code in SaveCacheFile removes the early return check for context.Flags.MemCache and disables cache stats logging. If this change is intentional, the commented code should be removed entirely rather than left as comments.

Apply this diff to remove the commented code:

public IContext SaveCacheFile(IContext context) {
-    // if (context.Flags.MemCache)
-    //     return context;
-    // // 15. Program exit started. Save the cache file
    var cache = Cache.GetCacheInstance();
-    // context.Logger.LogInformation("Saving cache with stats: {stats}", cache.GetCacheStats());
    var serialized = JsonConvert.SerializeObject(cache, CacheContractResolver.Settings);
    using var stream =
        new StreamWriter(context.GetCachePath());
    stream.Write(serialized);
    return context;
}
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between cda9597 and 3f147fe.

📒 Files selected for processing (1)
  • src/SharpLinks.cs (8 hunks)
🔇 Additional comments (4)
src/SharpLinks.cs (4)

22-22: LGTM! Assembly version retrieval support added correctly.

The System.Reflection namespace import is necessary for the Assembly.GetExecutingAssembly().GetName().Version call used in the cache invalidation logic.


33-33: Consistent namespace formatting applied.

The namespace declaration has been reformatted to a single-line style, which is consistent with modern C# conventions.


65-66: Consistent code formatting improvements applied.

The formatting of else and catch statements has been standardized throughout the file for better readability and consistency.

Also applies to: 94-95, 210-211, 257-258


143-169: Well-implemented cache invalidation logic with proper fallback handling.

The cache invalidation logic correctly:

  • Retrieves the current assembly version for comparison
  • Creates new cache when file doesn't exist or when explicitly requested
  • Handles cache loading failures gracefully
  • Applies age and version-based invalidation rules

The implementation provides robust error handling with appropriate logging at each step.

}

private bool CacheNeedsInvalidation(Cache cache, Version version) {
var threshold = DateTime.Now.Subtract(TimeSpan.FromDays(30));
Copy link
Contributor

Choose a reason for hiding this comment

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

It might be valuable to make this value at least a constant.

if (context.Flags.MemCache)
return context;
// 15. Program exit started. Save the cache file
// if (context.Flags.MemCache)
Copy link
Contributor

@MikeX777 MikeX777 Jul 11, 2025

Choose a reason for hiding this comment

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

Do we support memcache? (not sure how it would work) If no, should we remove this line from readme? Also should we removed this commented out code if we are removing support?

Copy link
Contributor

@MikeX777 MikeX777 left a comment

Choose a reason for hiding this comment

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

Few questions, looks great otherwise!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants