Skip to content

Add caching template for gowrap#111

Merged
hexdigest merged 2 commits into
hexdigest:masterfrom
retr0-kernel:feature/add-caching-template
May 26, 2026
Merged

Add caching template for gowrap#111
hexdigest merged 2 commits into
hexdigest:masterfrom
retr0-kernel:feature/add-caching-template

Conversation

@retr0-kernel

Copy link
Copy Markdown
Contributor

Add Caching Template for GoWrap

This PR adds a new caching template that automatically instruments Go interfaces with in-memory caching.

Features

  • Smart Caching: Only caches methods with return values
  • Error Handling: Errors are never cached, failed calls always hit the underlying implementation
  • Thread-Safe: Uses sync.RWMutex for concurrent access
  • Configurable: Customizable cache expiration and cleanup intervals
  • Multi-Return Support: Handles both single and multiple return value methods
  • Key Generation: Cache keys based on method name + all parameters

Usage

# Basic usage
gowrap gen -p io -i Reader -t caching -o reader_with_caching.go

# With custom decorator name
gowrap gen -p io -i Writer -t caching -v DecoratorName=CachedWriter -o writer_with_caching.go

- Add templates/caching with in-memory caching using go-cache
- Cache successful method results based on method name + parameters
- Skip caching for void methods and error results
- Thread-safe implementation with configurable expiration
- Comprehensive test coverage for all caching scenarios
- Follows existing gowrap template patterns and conventions
@retr0-kernel

Copy link
Copy Markdown
Contributor Author

Hey there @hexdigest ,
Added a caching template that instruments interfaces with in-memory caching using go-cache. Let me know if any changes needed or anything, will do the needful. Thank you.

Comment thread templates/caching Outdated
{{- if and $method.HasResults (not $method.ReturnsError)}}
_key := "{{$method.Name}}"
{{- range $param := $method.Params}}
_key += fmt.Sprintf(":%v", {{$param.Name}})

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

%#v would be better here

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Applied

Comment thread templates/caching Outdated
{{- else if and $method.HasResults $method.ReturnsError}}
_key := "{{$method.Name}}"
{{- range $param := $method.Params}}
_key += fmt.Sprintf(":%v", {{$param.Name}})

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

%#v

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Applied

@retr0-kernel

Copy link
Copy Markdown
Contributor Author

Hi @hexdigest let me know if anything else is needed.

@hexdigest hexdigest merged commit 075cfae into hexdigest:master May 26, 2026
1 check passed
@hexdigest

Copy link
Copy Markdown
Owner

@retr0-kernel thanks for your contribution!

@retr0-kernel

Copy link
Copy Markdown
Contributor Author

No worries @hexdigest. Just wanted to know one thing, are you still actively maintaining this repo? Would love to help you maintain it and continue this. Thanks

@hexdigest

Copy link
Copy Markdown
Owner

Yeah I'm on it, things got busy in the past year but I'll try to keep up.

@retr0-kernel

Copy link
Copy Markdown
Contributor Author

No issues let me know if i can help would love to help in as a maintainer.

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.

2 participants