Files

41 lines
999 B
Go
Raw Permalink Normal View History

package cache
import goredis "github.com/redis/go-redis/v9"
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
switch driver {
case "redis":
backend = newRedisCache(rdb, options.DeleteScanCount)
default:
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)
}
if options.AsyncFillEnabled {
backend = newAsyncCache(backend, options.AsyncFillWorkers, options.AsyncFillBuffer, options.AsyncFillTimeout)
}
return newOptionsCache(backend, options)
}