Skip to content

Commit

Permalink
Merge pull request #43 from wklken/ft_cache_context
Browse files Browse the repository at this point in the history
feat(cache): support ctx as the first argument for all methods
  • Loading branch information
wklken authored Jun 26, 2023
2 parents fbaa4a0 + 6fac8c0 commit d3dc33d
Show file tree
Hide file tree
Showing 8 changed files with 145 additions and 177 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@
.idea
.envrc
.coverage.cov

.vscode
1 change: 0 additions & 1 deletion VERSION

This file was deleted.

79 changes: 40 additions & 39 deletions cache/memory/base_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
package memory

import (
"context"
"fmt"
"time"

Expand Down Expand Up @@ -47,17 +48,17 @@ type EmptyCache struct {
}

// Exists returns true if the cache has a value for the given key.
func (c *BaseCache) Exists(key cache.Key) bool {
func (c *BaseCache) Exists(ctx context.Context, key cache.Key) bool {
k := key.Key()
_, ok := c.backend.Get(k)
return ok
}

// Get will get the key from cache, if missing, will call the retrieveFunc to get the data, add to cache, then return
func (c *BaseCache) Get(key cache.Key) (interface{}, error) {
func (c *BaseCache) Get(ctx context.Context, key cache.Key) (interface{}, error) {
// 1. if cache is disabled, fetch and return
if c.disabled {
value, err := c.retrieveFunc(key)
value, err := c.retrieveFunc(ctx, key)
if err != nil {
return nil, err
}
Expand All @@ -77,16 +78,16 @@ func (c *BaseCache) Get(key cache.Key) (interface{}, error) {
}

// 3. if not exists in cache, retrieve it
return c.doRetrieve(key)
return c.doRetrieve(ctx, key)
}

// doRetrieve will retrieve the real data from database, redis, apis, etc.
func (c *BaseCache) doRetrieve(k cache.Key) (interface{}, error) {
func (c *BaseCache) doRetrieve(ctx context.Context, k cache.Key) (interface{}, error) {
key := k.Key()

// 3.2 fetch
value, err, _ := c.g.Do(key, func() (interface{}, error) {
return c.retrieveFunc(k)
return c.retrieveFunc(ctx, k)
})

if err != nil {
Expand All @@ -102,19 +103,19 @@ func (c *BaseCache) doRetrieve(k cache.Key) (interface{}, error) {
}

// Set will set key-value into cache.
func (c *BaseCache) Set(key cache.Key, data interface{}) {
func (c *BaseCache) Set(ctx context.Context, key cache.Key, data interface{}) {
k := key.Key()
c.backend.Set(k, data, 0)
}

// Delete deletes the value from the cache for the given key.
func (c *BaseCache) Delete(key cache.Key) error {
func (c *BaseCache) Delete(ctx context.Context, key cache.Key) error {
k := key.Key()
return c.backend.Delete(k)
}

// DirectGet will get key from cache, without calling the retrieveFunc
func (c *BaseCache) DirectGet(key cache.Key) (interface{}, bool) {
func (c *BaseCache) DirectGet(ctx context.Context, key cache.Key) (interface{}, bool) {
k := key.Key()
return c.backend.Get(k)
}
Expand All @@ -128,8 +129,8 @@ func (c *BaseCache) Disabled() bool {

// GetString returns a string representation of the value for the given key.
// will error if the type is not a string.
func (c *BaseCache) GetString(k cache.Key) (string, error) {
value, err := c.Get(k)
func (c *BaseCache) GetString(ctx context.Context, k cache.Key) (string, error) {
value, err := c.Get(ctx, k)
if err != nil {
return "", err
}
Expand All @@ -143,8 +144,8 @@ func (c *BaseCache) GetString(k cache.Key) (string, error) {

// GetBool returns a bool representation of the value for the given key.
// will error if the type is not a bool.
func (c *BaseCache) GetBool(k cache.Key) (bool, error) {
value, err := c.Get(k)
func (c *BaseCache) GetBool(ctx context.Context, k cache.Key) (bool, error) {
value, err := c.Get(ctx, k)
if err != nil {
return false, err
}
Expand All @@ -160,8 +161,8 @@ var defaultZeroTime = time.Time{}

// GetTime returns a time representation of the value for the given key.
// will error if the type is not an time.Time.
func (c *BaseCache) GetTime(k cache.Key) (time.Time, error) {
value, err := c.Get(k)
func (c *BaseCache) GetTime(ctx context.Context, k cache.Key) (time.Time, error) {
value, err := c.Get(ctx, k)
if err != nil {
return defaultZeroTime, err
}
Expand All @@ -175,8 +176,8 @@ func (c *BaseCache) GetTime(k cache.Key) (time.Time, error) {

// GetInt returns an int representation of the value for the given key.
// will error if the type is not an int.
func (c *BaseCache) GetInt(k cache.Key) (int, error) {
value, err := c.Get(k)
func (c *BaseCache) GetInt(ctx context.Context, k cache.Key) (int, error) {
value, err := c.Get(ctx, k)
if err != nil {
return 0, err
}
Expand All @@ -190,8 +191,8 @@ func (c *BaseCache) GetInt(k cache.Key) (int, error) {

// GetInt8 returns an int8 representation of the value for the given key.
// will error if the type is not an int8.
func (c *BaseCache) GetInt8(k cache.Key) (int8, error) {
value, err := c.Get(k)
func (c *BaseCache) GetInt8(ctx context.Context, k cache.Key) (int8, error) {
value, err := c.Get(ctx, k)
if err != nil {
return 0, err
}
Expand All @@ -205,8 +206,8 @@ func (c *BaseCache) GetInt8(k cache.Key) (int8, error) {

// GetInt16 returns an int16 representation of the value for the given key.
// will error if the type is not an int16.
func (c *BaseCache) GetInt16(k cache.Key) (int16, error) {
value, err := c.Get(k)
func (c *BaseCache) GetInt16(ctx context.Context, k cache.Key) (int16, error) {
value, err := c.Get(ctx, k)
if err != nil {
return 0, err
}
Expand All @@ -220,8 +221,8 @@ func (c *BaseCache) GetInt16(k cache.Key) (int16, error) {

// GetInt32 returns an int32 representation of the value for the given key.
// will error if the type is not an int32.
func (c *BaseCache) GetInt32(k cache.Key) (int32, error) {
value, err := c.Get(k)
func (c *BaseCache) GetInt32(ctx context.Context, k cache.Key) (int32, error) {
value, err := c.Get(ctx, k)
if err != nil {
return 0, err
}
Expand All @@ -235,8 +236,8 @@ func (c *BaseCache) GetInt32(k cache.Key) (int32, error) {

// GetInt64 returns an int64 representation of the value for the given key.
// will error if the type is not an int64.
func (c *BaseCache) GetInt64(k cache.Key) (int64, error) {
value, err := c.Get(k)
func (c *BaseCache) GetInt64(ctx context.Context, k cache.Key) (int64, error) {
value, err := c.Get(ctx, k)
if err != nil {
return 0, err
}
Expand All @@ -250,8 +251,8 @@ func (c *BaseCache) GetInt64(k cache.Key) (int64, error) {

// GetUint returns an uint representation of the value for the given key.
// will error if the type is not an uint.
func (c *BaseCache) GetUint(k cache.Key) (uint, error) {
value, err := c.Get(k)
func (c *BaseCache) GetUint(ctx context.Context, k cache.Key) (uint, error) {
value, err := c.Get(ctx, k)
if err != nil {
return 0, err
}
Expand All @@ -265,8 +266,8 @@ func (c *BaseCache) GetUint(k cache.Key) (uint, error) {

// GetUint8 returns an uint8 representation of the value for the given key.
// will error if the type is not an uint8.
func (c *BaseCache) GetUint8(k cache.Key) (uint8, error) {
value, err := c.Get(k)
func (c *BaseCache) GetUint8(ctx context.Context, k cache.Key) (uint8, error) {
value, err := c.Get(ctx, k)
if err != nil {
return 0, err
}
Expand All @@ -280,8 +281,8 @@ func (c *BaseCache) GetUint8(k cache.Key) (uint8, error) {

// GetUint16 returns an uint16 representation of the value for the given key.
// will error if the type is not an uint16.
func (c *BaseCache) GetUint16(k cache.Key) (uint16, error) {
value, err := c.Get(k)
func (c *BaseCache) GetUint16(ctx context.Context, k cache.Key) (uint16, error) {
value, err := c.Get(ctx, k)
if err != nil {
return 0, err
}
Expand All @@ -295,8 +296,8 @@ func (c *BaseCache) GetUint16(k cache.Key) (uint16, error) {

// GetUint32 returns an uint32 representation of the value for the given key.
// will error if the type is not an uint32.
func (c *BaseCache) GetUint32(k cache.Key) (uint32, error) {
value, err := c.Get(k)
func (c *BaseCache) GetUint32(ctx context.Context, k cache.Key) (uint32, error) {
value, err := c.Get(ctx, k)
if err != nil {
return 0, err
}
Expand All @@ -310,8 +311,8 @@ func (c *BaseCache) GetUint32(k cache.Key) (uint32, error) {

// GetUint64 returns an uint64 representation of the value for the given key.
// will error if the type is not an uint64.
func (c *BaseCache) GetUint64(k cache.Key) (uint64, error) {
value, err := c.Get(k)
func (c *BaseCache) GetUint64(ctx context.Context, k cache.Key) (uint64, error) {
value, err := c.Get(ctx, k)
if err != nil {
return 0, err
}
Expand All @@ -325,8 +326,8 @@ func (c *BaseCache) GetUint64(k cache.Key) (uint64, error) {

// GetFloat32 returns a float32 representation of the value for the given key.
// will error if the type is not a float32.
func (c *BaseCache) GetFloat32(k cache.Key) (float32, error) {
value, err := c.Get(k)
func (c *BaseCache) GetFloat32(ctx context.Context, k cache.Key) (float32, error) {
value, err := c.Get(ctx, k)
if err != nil {
return 0, err
}
Expand All @@ -340,8 +341,8 @@ func (c *BaseCache) GetFloat32(k cache.Key) (float32, error) {

// GetFloat64 returns a float64 representation of the value for the given key.
// will error if the type is not a float64.
func (c *BaseCache) GetFloat64(k cache.Key) (float64, error) {
value, err := c.Get(k)
func (c *BaseCache) GetFloat64(ctx context.Context, k cache.Key) (float64, error) {
value, err := c.Get(ctx, k)
if err != nil {
return 0, err
}
Expand Down
Loading

0 comments on commit d3dc33d

Please sign in to comment.