diff --git a/Makefile b/Makefile index 2a1ba38..c96d15c 100644 --- a/Makefile +++ b/Makefile @@ -1,5 +1,7 @@ .PHONY: release +all: test lint + release: goreleaser release --clean --snapshot diff --git a/cachebatchkv/cache.go b/cachekv/batchkv.go similarity index 89% rename from cachebatchkv/cache.go rename to cachekv/batchkv.go index 9c44ba4..55040b9 100644 --- a/cachebatchkv/cache.go +++ b/cachekv/batchkv.go @@ -1,4 +1,4 @@ -package cachebatchkv +package cachekv import ( "context" @@ -6,7 +6,6 @@ import ( "fmt" "github.com/chenyanchen/db" - "github.com/chenyanchen/db/cachekv" ) // cacheBatchKV is a struct that contains a cache and a source BatchKV. @@ -19,13 +18,13 @@ type cacheBatchKV[K comparable, V any] struct { source db.BatchKV[K, V] } -// New creates a new cacheBatchKV instance with the given source and options. -func New[K comparable, V any]( +// NewBatch creates a new cacheBatchKV instance with the given source and options. +func NewBatch[K comparable, V any]( source db.BatchKV[K, V], - options ...cachekv.CacheOption[K, V], + options ...Option[K, V], ) *cacheBatchKV[K, V] { return &cacheBatchKV[K, V]{ - cache: cachekv.New(options...), + cache: New(options...), source: source, } } diff --git a/cachekv/cache.go b/cachekv/kv.go similarity index 68% rename from cachekv/cache.go rename to cachekv/kv.go index b714fe6..32a5d37 100644 --- a/cachekv/cache.go +++ b/cachekv/kv.go @@ -15,59 +15,59 @@ import ( "github.com/chenyanchen/db" ) -type CacheOption[K comparable, V any] func(kv *cacheKV[K, V]) +type Option[K comparable, V any] func(kv *cacheKV[K, V]) -// WithTTL returns a CacheOption to set ttl function. -func WithTTL[K comparable, V any](ttlFn func(K) time.Duration) CacheOption[K, V] { +// WithTTL returns a Option to set ttl function. +func WithTTL[K comparable, V any](ttlFn func(K) time.Duration) Option[K, V] { return func(kv *cacheKV[K, V]) { kv.ttlFn = ttlFn } } -// WithExpires returns a CacheOption to set solid ttl duration. -func WithExpires[K comparable, V any](ttl time.Duration) CacheOption[K, V] { +// WithExpires returns a Option to set solid ttl duration. +func WithExpires[K comparable, V any](ttl time.Duration) Option[K, V] { return WithTTL[K, V](func(K) time.Duration { return ttl }) } -// WithSmoothExpires returns a CacheOption to set smooth ttl. +// WithSmoothExpires returns a Option to set smooth ttl. // The real TTL is a random value between [0.5*ttl, 1.5*ttl). -func WithSmoothExpires[K comparable, V any](ttl time.Duration) CacheOption[K, V] { +func WithSmoothExpires[K comparable, V any](ttl time.Duration) Option[K, V] { return WithTTL[K, V](func(k K) time.Duration { return time.Duration((0.5 + rand.Float64()) * float64(ttl)) }) } -// WithSource returns a CacheOption to set source KV-storage. -func WithSource[K comparable, V any](source db.KV[K, V]) CacheOption[K, V] { +// WithSource returns a Option to set source KV-storage. +func WithSource[K comparable, V any](source db.KV[K, V]) Option[K, V] { return func(kv *cacheKV[K, V]) { kv.source = source } } -// WithTelemetryFunc returns a CacheOption to set telemetry prefix. -func WithTelemetryFunc[K comparable, V any](keyFn func(K, string)) CacheOption[K, V] { +// WithTelemetryFunc returns a Option to set telemetry prefix. +func WithTelemetryFunc[K comparable, V any](keyFn func(K, string)) Option[K, V] { return func(kv *cacheKV[K, V]) { kv.telFn = keyFn } } -// AsLRU returns a CacheOption to set LRU cache. -func AsLRU[K comparable, V any](capacity int) CacheOption[K, V] { +// AsLRU returns a Option to set LRU cache. +func AsLRU[K comparable, V any](capacity int) Option[K, V] { return func(kv *cacheKV[K, V]) { kv.cache = cache.New[K, V](cache.AsLRU[K, V](lru.WithCapacity(capacity))) } } -// AsLFU returns a CacheOption to set LFU cache. -func AsLFU[K comparable, V any](capacity int) CacheOption[K, V] { +// AsLFU returns a Option to set LFU cache. +func AsLFU[K comparable, V any](capacity int) Option[K, V] { return func(kv *cacheKV[K, V]) { kv.cache = cache.New[K, V](cache.AsLFU[K, V](lfu.WithCapacity(capacity))) } } -// AsFILO returns a CacheOption to set FILO cache. -func AsFILO[K comparable, V any](capacity int) CacheOption[K, V] { +// AsFILO returns a Option to set FILO cache. +func AsFILO[K comparable, V any](capacity int) Option[K, V] { return func(kv *cacheKV[K, V]) { kv.cache = cache.New[K, V](cache.AsFIFO[K, V](fifo.WithCapacity(capacity))) } } -// AsMRU returns a CacheOption to set MRU cache. -func AsMRU[K comparable, V any](capacity int) CacheOption[K, V] { +// AsMRU returns a Option to set MRU cache. +func AsMRU[K comparable, V any](capacity int) Option[K, V] { return func(kv *cacheKV[K, V]) { kv.cache = cache.New[K, V](cache.AsMRU[K, V](mru.WithCapacity(capacity))) } @@ -88,7 +88,7 @@ type cacheKV[K comparable, V any] struct { telFn func(k K, scene string) } -func New[K comparable, V any](options ...CacheOption[K, V]) *cacheKV[K, V] { +func New[K comparable, V any](options ...Option[K, V]) *cacheKV[K, V] { kv := &cacheKV[K, V]{cache: cache.New[K, V]()} for _, opt := range options { opt(kv) diff --git a/cachekv/cache_test.go b/cachekv/kv_test.go similarity index 100% rename from cachekv/cache_test.go rename to cachekv/kv_test.go