Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor(cache): improve caching system and add expiring cache #408

Open
wants to merge 7 commits into
base: main
Choose a base branch
from

Conversation

nathabonfim59
Copy link
Contributor

Description

For my usage which primarily uses Copilot, this change made a huge difference in performance - reducing about ~2 seconds for each request by caching the access tokens.

Not sure if this is the direction you want to go, but I found it very helpful for my workflow.
Let me know if you'd like any changes - happy to update!

Changes

  1. Convert existing cache to generic Cache[T] type

    • Split into "conversations" and "temporary" cache types
    • Add better error handling and directory management
    • Improve type safety with generics
  2. Add ExpiringCache implementation

    • Support for time-based cache expiration
    • Automatic cleanup of expired entries
    • Used for caching Copilot access tokens
$HOME/user/.local/share/mods
.
├── cache
│   └── copilot.1734276998
└── conversations
    ├── 0dbcdaae5bf314e06a9b47bf513e4ab69483761e.gob
    ├── 14a5b9133e384ae4f42c3a49756f6be9703c6982.gob
    ├── 35588af9aeec76b98ee8e4776e6be751e9528417.gob
    └── mods.db

Note: This PR builds on #406 and should be merged after it.

cache.go Outdated

const (
ConversationCache CacheType = "conversations"
TemporaryCache CacheType = "cache"
Copy link
Member

Choose a reason for hiding this comment

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

maybe name it "temp" instead of "cache"? or something more specific?

@caarlos0
Copy link
Member

caarlos0 commented Jan 6, 2025

hey! thanks for the PR! can you merge with main?

thanks!

With this approach, I was able to shave off ~1.2s in each request
… cache

Using a standard error type (os.ErrNotExist) instead of a custom error
message ("cache expired") makes the error handling more consistent with
Go conventions and allows for better error checking using errors.Is().
- Add error handling for file removal operations
- Properly handle file.Close() errors using defer functions
- Add package-level documentation for cache types and constants
- Document public methods and types with descriptive comments
- Reorganize constant and variable declarations
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