Skip to content

Conversation

cnlangzi
Copy link
Member

@cnlangzi cnlangzi commented May 4, 2025

Changed

Fixed

Added

  • feat(ext/cache): added ext/cache

Tests

Tasks to complete before merging PR:

  • Ensure unit tests are passing. If not run make unit-tests to check for any regressions 📋
  • Ensure lint tests are passing. if not run make lint to check for any issues
  • Ensure codecov/patch is passing for changes.

Summary by Sourcery

Add a new cache middleware extension to the xun framework that allows configurable caching rules for HTTP routes

New Features:

  • Implement a cache middleware that can set Cache-Control headers based on configurable matching rules

Enhancements:

  • Create a flexible rule-based matching system for cache configuration
  • Support case-insensitive prefix and suffix matching for route paths

Copy link

sourcery-ai bot commented May 4, 2025

Reviewer's Guide

This pull request introduces a new xun.Middleware in ext/cache that sets the Cache-Control header based on configurable rules matching URL path prefixes or suffixes. Rules and their durations are defined using functional options.

Class Diagram for New Cache Components

classDiagram
    class Rule {
        +StartsWith: string
        +EndsWith: string
        +Duration: time.Duration
        +Match(path string) bool
    }
    note for Rule "Defines a cache rule based on path matching."

    class Options {
        +Rules: []Rule
    }
    note for Options "Holds configured cache rules."

    class Option {
        <<func>>
        +apply(*Options)
    }
    note for Option "Functional option type (func(*Options)) for configuration."

    class cache {
        <<package>>
        +New(...Option) xun.Middleware
        +Match(startsWith string, endsWith string, duration time.Duration) Option
        -hasPrefix(s string, prefix string) bool
        -hasSuffix(s string, suffix string) bool
    }
    note for cache "Entry point and helpers for the cache middleware."

    Options "1" *-- "0..*" Rule : contains
    cache ..> Option : creates/uses
    cache ..> Options : configures via Option
    cache ..> Rule : uses for matching
Loading

File-Level Changes

Change Details Files
Added rule-based caching middleware.
  • Created a New function that returns xun.Middleware to integrate with the framework.
  • Implemented logic within the middleware to iterate through defined rules and match against the request URL path.
  • Set the Cache-Control response header with the appropriate max-age directive when a rule matches.
  • Defined Rule struct and functional options (Option, Match) for configuring path matching (prefix/suffix) and cache duration.
ext/cache/cache.go
ext/cache/option.go

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@cnlangzi cnlangzi requested a review from Copilot May 4, 2025 07:42
Copy link

@Copilot 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 adds a new cache middleware extension for the xun framework to support configurable caching rules for HTTP routes based on case-insensitive prefix and suffix matching.

  • Introduces a rule-based matching system for cache configuration.
  • Implements middleware to set Cache-Control headers based on matching rules.

Reviewed Changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
ext/cache/option.go Adds rule definitions and configuration options for cache middleware.
ext/cache/cache.go Implements the middleware that applies cache rules to HTTP requests.
Comments suppressed due to low confidence (1)

ext/cache/option.go:32

  • [nitpick] Consider renaming the option constructor 'Match' to something more descriptive (e.g., 'WithCacheRule') to clearly differentiate it from the Rule.Match method.
func Match(startsWith, endsWith string, duration time.Duration) Option {

return func(c *xun.Context) error {
for _, rule := range options.Rules {
if rule.Match(c.Request.URL.Path) {
c.WriteHeader("Cache-Control", "max-age="+strconv.Itoa(int(rule.Duration.Seconds())))
Copy link
Preview

Copilot AI May 4, 2025

Choose a reason for hiding this comment

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

[nitpick] Add a comment clarifying that only the first matching rule is applied to help future maintainers understand the middleware behavior when multiple rules might match.

Copilot uses AI. Check for mistakes.

Copy link

@sourcery-ai sourcery-ai bot left a comment

Choose a reason for hiding this comment

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

Hey @cnlangzi - I've reviewed your changes - here's some feedback:

  • Consider if case-insensitive path matching (EqualFold) is the desired behavior for cache rules.
Here's what I looked at during the review
  • 🟢 General issues: all looks good
  • 🟢 Security: all looks good
  • 🟢 Testing: all looks good
  • 🟢 Documentation: all looks good

Sourcery is free for open source - if you like our reviews please consider sharing them ✨
Help me be more useful! Please click 👍 or 👎 on each comment and I'll use the feedback to improve your reviews.

Copy link

codecov bot commented May 4, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 91.71%. Comparing base (7b70679) to head (6ad65a9).
Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main      #72      +/-   ##
==========================================
+ Coverage   91.57%   91.71%   +0.13%     
==========================================
  Files          63       65       +2     
  Lines        2541     2582      +41     
==========================================
+ Hits         2327     2368      +41     
  Misses        186      186              
  Partials       28       28              
Flag Coverage Δ
Unit-Tests 91.71% <100.00%> (+0.13%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Copy link

deepsource-io bot commented May 4, 2025

Here's the code health analysis summary for commits 7b70679..6ad65a9. View details on DeepSource ↗.

Analysis Summary

AnalyzerStatusSummaryLink
DeepSource Go LogoGo✅ SuccessView Check ↗

💡 If you’re a repository administrator, you can configure the quality gates from the settings.

@cnlangzi cnlangzi requested a review from Copilot May 5, 2025 06:28
Copy link

@Copilot 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 introduces a new cache middleware extension to the xun framework, which applies configurable Cache-Control headers based on flexible matching rules for HTTP routes.

  • Adds a rule-based caching system in ext/cache/option.go.
  • Implements the middleware in ext/cache/cache.go.
  • Provides unit tests in ext/cache/cache_test.go to validate the new caching behavior.

Reviewed Changes

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

File Description
ext/cache/option.go Implements caching rules with case-insensitive prefix/suffix matches
ext/cache/cache_test.go Tests various caching rules and header behaviors
ext/cache/cache.go Defines the middleware that applies Cache-Control headers based on the rules

@cnlangzi cnlangzi merged commit 752c73d into main May 5, 2025
7 checks passed
@cnlangzi cnlangzi deleted the feat/cache branch May 5, 2025 06:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant