2026-04-02 00:31:28 +08:00
|
|
|
package cache
|
|
|
|
|
|
|
|
|
|
import goredis "github.com/redis/go-redis/v9"
|
|
|
|
|
|
2026-05-03 02:02:39 +08:00
|
|
|
func New(driver string, rdb *goredis.Client, opts ...Option) Cache {
|
|
|
|
|
options := DefaultOptions()
|
|
|
|
|
for _, opt := range opts {
|
|
|
|
|
if opt != nil {
|
|
|
|
|
opt(&options)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return NewWithOptions(driver, rdb, options)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func NewWithOptions(driver string, rdb *goredis.Client, options Options) Cache {
|
|
|
|
|
options = normalizeOptions(options)
|
|
|
|
|
|
|
|
|
|
var backend Cache
|
2026-04-02 00:31:28 +08:00
|
|
|
switch driver {
|
|
|
|
|
case "redis":
|
2026-05-03 02:02:39 +08:00
|
|
|
backend = newRedisCache(rdb, options.DeleteScanCount)
|
2026-04-02 00:31:28 +08:00
|
|
|
default:
|
2026-05-03 02:02:39 +08:00
|
|
|
backend = newMemoryCache()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if options.Namespace != "" {
|
|
|
|
|
backend = newPrefixCache(backend, options.Namespace)
|
|
|
|
|
}
|
|
|
|
|
if options.L1Enabled && driver == "redis" {
|
|
|
|
|
backend = newL1Cache(backend, options.L1TTL)
|
|
|
|
|
}
|
|
|
|
|
if options.MetricsEnabled {
|
|
|
|
|
backend = newObservedCache(backend, driver)
|
2026-04-02 00:31:28 +08:00
|
|
|
}
|
2026-05-03 02:02:39 +08:00
|
|
|
if options.AsyncFillEnabled {
|
|
|
|
|
backend = newAsyncCache(backend, options.AsyncFillWorkers, options.AsyncFillBuffer, options.AsyncFillTimeout)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return newOptionsCache(backend, options)
|
2026-04-02 00:31:28 +08:00
|
|
|
}
|