Skip to content

Commit

Permalink
Merge pull request #3 from chenyanchen/set_to_source
Browse files Browse the repository at this point in the history
Set value into source.
  • Loading branch information
chenyanchen committed May 11, 2023
2 parents db58731 + 915092d commit 8e88de5
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
12 changes: 9 additions & 3 deletions cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,16 @@ func (c *cacheKV[K, V]) Get(ctx context.Context, k K) (V, error) {
c.telemetry(k, "miss_src")
return got, fmt.Errorf("get from source: %w", err)
}
_ = c.Set(ctx, k, got)
c.cache.Set(k, got, c.cacheOptions(k)...)
c.telemetry(k, "hit_src")
return got, nil
}

func (c *cacheKV[K, V]) Set(_ context.Context, k K, v V) error {
func (c *cacheKV[K, V]) Set(ctx context.Context, k K, v V) error {
c.cache.Set(k, v, c.cacheOptions(k)...)
if c.src != nil {
return c.src.Set(ctx, k, v)
}
return nil
}

Expand All @@ -127,8 +130,11 @@ func (c *cacheKV[K, V]) cacheOptions(k K) []cache.ItemOption {
return opts
}

func (c *cacheKV[K, V]) Del(_ context.Context, k K) error {
func (c *cacheKV[K, V]) Del(ctx context.Context, k K) error {
c.cache.Delete(k)
if c.src != nil {
return c.src.Del(ctx, k)
}
return nil
}

Expand Down
2 changes: 0 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
github.com/chenyanchen/db v0.1.5 h1:ko8gPnzscRiNjmoF4AypEnrR6MV2PjqTb/1LklFA34k=
github.com/chenyanchen/db v0.1.5/go.mod h1:GlYW1OygQk4R9ijISgIuZuTYMfXXyPomYZbI8Dw17Lk=
github.com/Code-Hex/go-generics-cache v1.3.1 h1:i8rLwyhoyhaerr7JpjtYjJZUcCbWOdiYO3fZXLiEC4g=
github.com/Code-Hex/go-generics-cache v1.3.1/go.mod h1:qxcC9kRVrct9rHeiYpFWSoW1vxyillCVzX13KZG8dl4=
github.com/chenyanchen/sync v0.1.0 h1:kaLp5i97GAxTB7wGPKbrvza1KjC6c1FGAbW79IclmX0=
Expand Down

0 comments on commit 8e88de5

Please sign in to comment.