[PR #400] [MERGED] Add Redis caching to FetchPluginInstallation middleware for performance optimization #488

Closed
opened 2026-02-16 01:16:07 -05:00 by yindo · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/langgenius/dify-plugin-daemon/pull/400
Author: @Blackoutta
Created: 7/14/2025
Status: Merged
Merged: 8/27/2025
Merged by: @Yeuoly

Base: mainHead: feature/blackoutta/add-middleware-caching


📝 Commits (3)

  • e862746 add caching to fetch plugin installation middleware
  • 4cfe6fd invalidate plugin inst cache after successfully upgrading or uninstalling operations
  • 632081f optimize: refactored cache invalidation ops from curd layer to service layer

📊 Changes

8 files changed (+97 additions, -68 deletions)

View changed files

📝 internal/server/endpoint.go (+4 -10)
📝 internal/server/middleware.go (+17 -5)
📝 internal/service/endpoint.go (+8 -2)
📝 internal/service/install_plugin.go (+9 -0)
📝 internal/service/install_service/state.go (+22 -27)
📝 internal/service/setup_endpoint.go (+10 -9)
📝 internal/types/models/curd/atomic.go (+2 -15)
internal/utils/cache/helper/keys.go (+25 -0)

📄 Description

Description

This PR implements Redis caching for the FetchPluginInstallation middleware to improve performance by reducing database queries for frequently accessed plugin installation data.

Problem: The FetchPluginInstallation middleware was called frequently and performed a database query on each request, causing performance issues due to repeated database access for the same plugin installation data.

Solution: Added Redis caching using the existing AutoGetWithGetter pattern to reduce database load and improve response times for frequent plugin installation lookups.

Closes #399

Type of Change

  • Bug fix
  • New feature
  • Refactor
  • Performance improvement
  • Other

Essential Checklist

Testing

  • I have tested the changes locally and confirmed they work as expected
  • [] I have added unit tests where necessary and they pass successfully

Bug Fix (if applicable)

  • I have used GitHub syntax to close the related issue (e.g., Fixes #123 or Closes #123)

Additional Information

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

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/langgenius/dify-plugin-daemon/pull/400 **Author:** [@Blackoutta](https://github.com/Blackoutta) **Created:** 7/14/2025 **Status:** ✅ Merged **Merged:** 8/27/2025 **Merged by:** [@Yeuoly](https://github.com/Yeuoly) **Base:** `main` ← **Head:** `feature/blackoutta/add-middleware-caching` --- ### 📝 Commits (3) - [`e862746`](https://github.com/langgenius/dify-plugin-daemon/commit/e862746e6d9f6afd726979beddc31c0848cbbc47) add caching to fetch plugin installation middleware - [`4cfe6fd`](https://github.com/langgenius/dify-plugin-daemon/commit/4cfe6fd5b0b6389f73beb27cfc23e1b82618f981) invalidate plugin inst cache after successfully upgrading or uninstalling operations - [`632081f`](https://github.com/langgenius/dify-plugin-daemon/commit/632081f9ab16652c8240ce5deec9df87cb6bfb0a) optimize: refactored cache invalidation ops from curd layer to service layer ### 📊 Changes **8 files changed** (+97 additions, -68 deletions) <details> <summary>View changed files</summary> 📝 `internal/server/endpoint.go` (+4 -10) 📝 `internal/server/middleware.go` (+17 -5) 📝 `internal/service/endpoint.go` (+8 -2) 📝 `internal/service/install_plugin.go` (+9 -0) 📝 `internal/service/install_service/state.go` (+22 -27) 📝 `internal/service/setup_endpoint.go` (+10 -9) 📝 `internal/types/models/curd/atomic.go` (+2 -15) ➕ `internal/utils/cache/helper/keys.go` (+25 -0) </details> ### 📄 Description ## Description This PR implements Redis caching for the `FetchPluginInstallation` middleware to improve performance by reducing database queries for frequently accessed plugin installation data. **Problem**: The `FetchPluginInstallation` middleware was called frequently and performed a database query on each request, causing performance issues due to repeated database access for the same plugin installation data. **Solution**: Added Redis caching using the existing `AutoGetWithGetter` pattern to reduce database load and improve response times for frequent plugin installation lookups. Closes #399 ## Type of Change - [ ] Bug fix - [ ] New feature - [ ] Refactor - [x] Performance improvement - [ ] Other ## Essential Checklist ### Testing - [x] I have tested the changes locally and confirmed they work as expected - [] I have added unit tests where necessary and they pass successfully ### Bug Fix (if applicable) - [ ] I have used GitHub syntax to close the related issue (e.g., `Fixes #123` or `Closes #123`) ## Additional Information ### 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 --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
yindo added the pull-request label 2026-02-16 01:16:07 -05:00
yindo closed this issue 2026-02-16 01:16:07 -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#488