feat: optimize caching strategy and implement concurrency limits

This commit is contained in:
ExplodingDragon
2026-01-31 23:14:15 +08:00
parent 1aec59bfad
commit 662370e018
10 changed files with 148 additions and 41 deletions

View File

@@ -41,14 +41,15 @@ type Server struct {
}
type serverConfig struct {
client *http.Client
event subscribe.Subscriber
cacheMeta kv.KV
cacheMetaTTL time.Duration
cacheBlob cache.Cache
cacheBlobTTL time.Duration
errorHandler func(w http.ResponseWriter, r *http.Request, err error)
filterConfig map[string]map[string]any
client *http.Client
event subscribe.Subscriber
cacheMeta kv.KV
cacheMetaTTL time.Duration
cacheMetaRefresh time.Duration
cacheBlob cache.Cache
cacheBlobTTL time.Duration
errorHandler func(w http.ResponseWriter, r *http.Request, err error)
filterConfig map[string]map[string]any
}
type ServerOption func(*serverConfig)
@@ -65,10 +66,11 @@ func WithEvent(event subscribe.Subscriber) ServerOption {
}
}
func WithMetaCache(cache kv.KV, ttl time.Duration) ServerOption {
func WithMetaCache(cache kv.KV, ttl time.Duration, refresh time.Duration) ServerOption {
return func(c *serverConfig) {
c.cacheMeta = cache
c.cacheMetaTTL = ttl
c.cacheMetaRefresh = refresh
}
}
@@ -117,6 +119,10 @@ func NewPageServer(
}
}
if cfg.cacheMetaRefresh == 0 {
cfg.cacheMetaRefresh = cfg.cacheMetaTTL / 2
}
if cfg.cacheBlob == nil {
var err error
cfg.cacheBlob, err = cache.NewMemoryCache(cache.MemoryCacheConfig{
@@ -135,7 +141,7 @@ func NewPageServer(
}
alias := core.NewDomainAlias(db.Child("config", "alias"))
svcMeta := core.NewServerMeta(cfg.client, backend, domain, alias, cfg.cacheMeta, cfg.cacheMetaTTL)
svcMeta := core.NewServerMeta(cfg.client, backend, domain, alias, cfg.cacheMeta, cfg.cacheMetaTTL, cfg.cacheMetaRefresh)
pageMeta := core.NewPageDomain(svcMeta, domain)
globCache, err := lru.New[string, glob.Glob](512)
if err != nil {