Add Redis caching to FetchPluginInstallation middleware for performance optimization #161

Closed
opened 2026-02-16 00:20:13 -05:00 by yindo · 0 comments
Owner

Originally created by @Blackoutta on GitHub (Jul 14, 2025).

Self Checks

To make sure we get to you in time, please check the following :)

  • I have searched for existing issues search for existing issues, including closed ones.
  • I confirm that I am using English to submit this report (我已阅读并同意 Language Policy).
  • [FOR CHINESE USERS] 请务必使用英文提交 Issue,否则会被关闭。谢谢!:)
  • "Please do not modify this template :) and fill in all the required fields."

Is your feature request related to a problem? Please describe.
The FetchPluginInstallation middleware is called frequently and performs a database query on each request, which causes performance issues due to repeated database access for the same plugin installation data.

Describe the solution you'd like
Add Redis caching to the FetchPluginInstallation middleware using the existing AutoGetWithGetter pattern to reduce database load and improve response times for frequent plugin installation lookups.

Describe alternatives you've considered
None

Additional context
I've gone ahead and implement this feature, the details are shown below.

Implementation Details

Files Modified:

  1. internal/utils/cache/helper/keys.go (new file)

    • Added PluginInstallationCacheKey() function for consistent cache key generation
    • Format: "plugin_id:{pluginId}:tenant_id:{tenantId}"
  2. internal/server/middleware.go

    • Modified FetchPluginInstallation() to use cache.AutoGetWithGetter()
    • Added cache and helper package imports
    • Maintains existing error handling logic
    • Fixed pointer dereferencing for context storage
  3. internal/server/endpoint.go

    • Updated EndpointHandler() to use shared cache key helper
    • Replaced inline cache key construction with centralized function
    • Improved import organization
  4. internal/types/models/curd/atomic.go

    • Updated UninstallPlugin() to use shared cache key helper
    • Replaced inline cache key construction with centralized function
    • Removed unused strings import

Benefits:

  • Performance: Reduces database queries for frequently accessed plugin installations
  • Consistency: Uses existing Redis infrastructure and follows established patterns
  • Maintainability: Centralized cache key construction prevents inconsistencies
  • Automatic TTL: 30-minute cache expiration via AutoGetWithGetter

Cache Strategy:

  • Cache hit: Returns cached plugin installation immediately
  • Cache miss: Fetches from database, caches result, then returns
  • Cache invalidation: Handled automatically on plugin uninstall
Originally created by @Blackoutta on GitHub (Jul 14, 2025). **Self Checks** To make sure we get to you in time, please check the following :) - [x] I have searched for existing issues [search for existing issues](https://github.com/langgenius/dify-plugin-daemon/issues), including closed ones. - [x] I confirm that I am using English to submit this report (我已阅读并同意 [Language Policy](https://github.com/langgenius/dify/issues/1542)). - [x] [FOR CHINESE USERS] 请务必使用英文提交 Issue,否则会被关闭。谢谢!:) - [x] "Please do not modify this template :) and fill in all the required fields." **Is your feature request related to a problem? Please describe.** The `FetchPluginInstallation` middleware is called frequently and performs a database query on each request, which causes performance issues due to repeated database access for the same plugin installation data. **Describe the solution you'd like** Add Redis caching to the `FetchPluginInstallation` middleware using the existing `AutoGetWithGetter` pattern to reduce database load and improve response times for frequent plugin installation lookups. **Describe alternatives you've considered** None **Additional context** I've gone ahead and implement this feature, the details are shown below. ## Implementation Details ### Files Modified: 1. **`internal/utils/cache/helper/keys.go`** (new file) - Added `PluginInstallationCacheKey()` function for consistent cache key generation - Format: `"plugin_id:{pluginId}:tenant_id:{tenantId}"` 2. **`internal/server/middleware.go`** - Modified `FetchPluginInstallation()` to use `cache.AutoGetWithGetter()` - Added cache and helper package imports - Maintains existing error handling logic - Fixed pointer dereferencing for context storage 3. **`internal/server/endpoint.go`** - Updated `EndpointHandler()` to use shared cache key helper - Replaced inline cache key construction with centralized function - Improved import organization 4. **`internal/types/models/curd/atomic.go`** - Updated `UninstallPlugin()` to use shared cache key helper - Replaced inline cache key construction with centralized function - Removed unused `strings` import ### Benefits: - **Performance**: Reduces database queries for frequently accessed plugin installations - **Consistency**: Uses existing Redis infrastructure and follows established patterns - **Maintainability**: Centralized cache key construction prevents inconsistencies - **Automatic TTL**: 30-minute cache expiration via `AutoGetWithGetter` ### Cache Strategy: - Cache hit: Returns cached plugin installation immediately - Cache miss: Fetches from database, caches result, then returns - Cache invalidation: Handled automatically on plugin uninstall
yindo closed this issue 2026-02-16 00:20:13 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: langgenius/dify-plugin-daemon#161