feat(model): 增加缓存机制

This commit is contained in:
yokowu
2025-07-13 00:05:39 +08:00
parent d6574ad44c
commit 963ad4eabc
7 changed files with 40 additions and 14 deletions

View File

@@ -17,14 +17,14 @@ import (
v1_4 "github.com/chaitin/MonkeyCode/backend/internal/dashboard/handler/v1"
repo6 "github.com/chaitin/MonkeyCode/backend/internal/dashboard/repo"
usecase5 "github.com/chaitin/MonkeyCode/backend/internal/dashboard/usecase"
repo3 "github.com/chaitin/MonkeyCode/backend/internal/extension/repo"
repo4 "github.com/chaitin/MonkeyCode/backend/internal/extension/repo"
usecase2 "github.com/chaitin/MonkeyCode/backend/internal/extension/usecase"
"github.com/chaitin/MonkeyCode/backend/internal/middleware"
v1_2 "github.com/chaitin/MonkeyCode/backend/internal/model/handler/http/v1"
repo4 "github.com/chaitin/MonkeyCode/backend/internal/model/repo"
repo2 "github.com/chaitin/MonkeyCode/backend/internal/model/repo"
usecase3 "github.com/chaitin/MonkeyCode/backend/internal/model/usecase"
"github.com/chaitin/MonkeyCode/backend/internal/openai/handler/v1"
repo2 "github.com/chaitin/MonkeyCode/backend/internal/openai/repo"
repo3 "github.com/chaitin/MonkeyCode/backend/internal/openai/repo"
"github.com/chaitin/MonkeyCode/backend/internal/openai/usecase"
"github.com/chaitin/MonkeyCode/backend/internal/proxy"
"github.com/chaitin/MonkeyCode/backend/internal/proxy/repo"
@@ -54,17 +54,17 @@ func newServer() (*Server, error) {
return nil, err
}
proxyRepo := repo.NewProxyRepo(client)
proxyUsecase := usecase.NewProxyUsecase(proxyRepo)
modelRepo := repo2.NewModelRepo(client)
proxyUsecase := usecase.NewProxyUsecase(proxyRepo, modelRepo)
domainProxy := proxy.NewLLMProxy(proxyUsecase, configConfig, slogLogger)
openAIRepo := repo2.NewOpenAIRepo(client)
openAIRepo := repo3.NewOpenAIRepo(client)
openAIUsecase := openai.NewOpenAIUsecase(configConfig, openAIRepo, slogLogger)
extensionRepo := repo3.NewExtensionRepo(client)
extensionRepo := repo4.NewExtensionRepo(client)
extensionUsecase := usecase2.NewExtensionUsecase(extensionRepo, configConfig, slogLogger)
proxyMiddleware := middleware.NewProxyMiddleware(proxyUsecase)
redisClient := store.NewRedisCli(configConfig)
activeMiddleware := middleware.NewActiveMiddleware(redisClient, slogLogger)
v1Handler := v1.NewV1Handler(slogLogger, web, domainProxy, openAIUsecase, extensionUsecase, proxyMiddleware, activeMiddleware, configConfig)
modelRepo := repo4.NewModelRepo(client)
modelUsecase := usecase3.NewModelUsecase(slogLogger, modelRepo, configConfig)
sessionSession := session.NewSession(configConfig)
authMiddleware := middleware.NewAuthMiddleware(sessionSession, slogLogger)

View File

@@ -20,6 +20,7 @@ type ModelUsecase interface {
}
type ModelRepo interface {
GetWithCache(ctx context.Context, modelType consts.ModelType) (*db.Model, error)
List(ctx context.Context) (*AllModelResp, error)
Create(ctx context.Context, m *CreateModelReq) (*db.Model, error)
Update(ctx context.Context, id string, fn func(tx *db.Tx, old *db.Model, up *db.ModelUpdateOne) error) (*db.Model, error)

View File

@@ -10,7 +10,6 @@ import (
"github.com/chaitin/MonkeyCode/backend/db"
)
// Proxy LLM API代理接口
type Proxy interface {
AcceptCompletion(ctx context.Context, req *AcceptCompletionReq) error
HandleCompletion(ctx context.Context, w http.ResponseWriter, req CompletionRequest)

View File

@@ -10,6 +10,7 @@ require (
github.com/google/wire v0.6.0
github.com/labstack/echo/v4 v4.13.4
github.com/lib/pq v1.10.9
github.com/patrickmn/go-cache v2.1.0+incompatible
github.com/redis/go-redis/v9 v9.7.3
github.com/rokku-c/go-openai v1.35.7-fix2
github.com/spf13/viper v1.20.1

View File

@@ -115,6 +115,8 @@ github.com/opencontainers/go-digest v1.0.0 h1:apOUWs51W5PlhuyGyz9FCeeBIOUDA/6nW8
github.com/opencontainers/go-digest v1.0.0/go.mod h1:0JzlMkj0TRzQZfJkVvzbP0HBR3IKzErnv2BNG4W4MAM=
github.com/opencontainers/image-spec v1.1.0 h1:8SG7/vwALn54lVB/0yZ/MMwhFrPYtpEHQb2IpWsCzug=
github.com/opencontainers/image-spec v1.1.0/go.mod h1:W4s4sFTMaBeK1BQLXbG4AdM2szdn85PY75RI83NrTrM=
github.com/patrickmn/go-cache v2.1.0+incompatible h1:HRMgzkcYKYpi3C8ajMPV8OFXaaRUnok+kx1WdO15EQc=
github.com/patrickmn/go-cache v2.1.0+incompatible/go.mod h1:3Qf8kWWT7OJRJbdiICTKqZju1ZixQ/KpMGzzAfe6+WQ=
github.com/pelletier/go-toml/v2 v2.2.4 h1:mye9XuhQ6gvn5h28+VilKrrPoQVanw5PMw/TB0t5Ec4=
github.com/pelletier/go-toml/v2 v2.2.4/go.mod h1:2gIqNv+qfxSVS7cM2xJQKtLSTLUE9V8t9Stt+h56mCY=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=

View File

@@ -6,6 +6,7 @@ import (
"entgo.io/ent/dialect/sql"
"github.com/google/uuid"
"github.com/patrickmn/go-cache"
"github.com/chaitin/MonkeyCode/backend/consts"
"github.com/chaitin/MonkeyCode/backend/db"
@@ -18,11 +19,30 @@ import (
)
type ModelRepo struct {
db *db.Client
db *db.Client
cache *cache.Cache
}
func NewModelRepo(db *db.Client) domain.ModelRepo {
return &ModelRepo{db: db}
cache := cache.New(24*time.Hour, 10*time.Minute)
return &ModelRepo{db: db, cache: cache}
}
func (r *ModelRepo) GetWithCache(ctx context.Context, modelType consts.ModelType) (*db.Model, error) {
if v, ok := r.cache.Get(string(modelType)); ok {
return v.(*db.Model), nil
}
m, err := r.db.Model.Query().
Where(model.ModelType(modelType)).
Where(model.Status(consts.ModelStatusActive)).
Only(ctx)
if err != nil {
return nil, err
}
r.cache.Set(string(modelType), m, 24*time.Hour)
return m, nil
}
func (r *ModelRepo) Create(ctx context.Context, m *domain.CreateModelReq) (*db.Model, error) {
@@ -40,6 +60,7 @@ func (r *ModelRepo) Create(ctx context.Context, m *domain.CreateModelReq) (*db.M
status = consts.ModelStatusActive
}
r.cache.Delete(string(m.ModelType))
return r.db.Model.Create().
SetUserID(uid).
SetModelName(m.ModelName).
@@ -62,6 +83,7 @@ func (r *ModelRepo) Update(ctx context.Context, id string, fn func(tx *db.Tx, ol
if err != nil {
return err
}
r.cache.Delete(string(old.ModelType))
up := tx.Model.UpdateOneID(old.ID)
if err := fn(tx, old, up); err != nil {

View File

@@ -10,11 +10,12 @@ import (
)
type ProxyUsecase struct {
repo domain.ProxyRepo
repo domain.ProxyRepo
modelRepo domain.ModelRepo
}
func NewProxyUsecase(repo domain.ProxyRepo) domain.ProxyUsecase {
return &ProxyUsecase{repo: repo}
func NewProxyUsecase(repo domain.ProxyRepo, modelRepo domain.ModelRepo) domain.ProxyUsecase {
return &ProxyUsecase{repo: repo, modelRepo: modelRepo}
}
func (p *ProxyUsecase) Record(ctx context.Context, record *domain.RecordParam) error {
@@ -23,7 +24,7 @@ func (p *ProxyUsecase) Record(ctx context.Context, record *domain.RecordParam) e
// SelectModelWithLoadBalancing implements domain.ProxyUsecase.
func (p *ProxyUsecase) SelectModelWithLoadBalancing(modelName string, modelType consts.ModelType) (*domain.Model, error) {
model, err := p.repo.SelectModelWithLoadBalancing(modelName, modelType)
model, err := p.modelRepo.GetWithCache(context.Background(), modelType)
if err != nil {
return nil, err
}