Skip to content

Commit

Permalink
[patch] added methods to Payload to make the underlying values availa…
Browse files Browse the repository at this point in the history
…ble in custom store implementation (#10)

[-] defining extendable types in a single place with comments
  • Loading branch information
bnkamalesh authored Oct 28, 2024
1 parent 95dcc8f commit cb89fad
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 11 deletions.
34 changes: 23 additions & 11 deletions cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,23 @@ var (
ErrValidation = errors.New("invalid")
)

type Store[K comparable, T any] interface {
Add(key K, value *Payload[T]) (evicted bool)
Get(key K) (value *Payload[T], found bool)
Remove(key K) (present bool)
}
type (
// ErrOnUpdate defines the type of the hook function, which is called
// if there's any error when trying to update a key in the background
ErrOnUpdate func(err error)

// Updater defines the function which is used to get the new value
// of a key. This is required for pocache to do background updates
Updater[K comparable, T any] func(ctx context.Context, key K) (T, error)

// Store defines the interface required for the underlying storage of pocache.
Store[K comparable, T any] interface {
Add(key K, value *Payload[T]) (evicted bool)
Get(key K) (value *Payload[T], found bool)
Remove(key K) (present bool)
}
)

type ErrOnUpdate func(err error)
type Config[K comparable, T any] struct {
// LRUCacheSize is the number of keys to be maintained in the cache
LRUCacheSize uint
Expand Down Expand Up @@ -106,7 +116,13 @@ type Payload[T any] struct {
payload T
}

type Updater[K comparable, T any] func(ctx context.Context, key K) (T, error)
func (pyl *Payload[T]) Expiry() time.Time {
return *pyl.cacheExpireAt.Load()
}

func (pyl *Payload[T]) Value() T {
return pyl.payload
}

type Tuple[K comparable, T any] struct {
Key K
Expand Down Expand Up @@ -185,10 +201,6 @@ func (ch *Cache[K, T]) deleteListener(keys <-chan K) {
}

func (ch *Cache[K, T]) updateListener(keys <-chan K) {
if ch.updater == nil {
return
}

for key := range keys {
ch.update(key)
}
Expand Down
17 changes: 17 additions & 0 deletions cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,3 +321,20 @@ func TestSanitize(tt *testing.T) {
asserter.Equal(cfg.Threshold, time.Second*59)
asserter.Equal(cfg.UpdaterTimeout, time.Second)
}

func TestPayload(tt *testing.T) {
asserter := assert.New(tt)

expireAt := time.Now().Add(time.Minute)
cea := atomic.Pointer[time.Time]{}
cea.Store(&expireAt)
value := "hello world"

pyl := Payload[string]{
cacheExpireAt: &cea,
payload: value,
}

asserter.Equal(value, pyl.Value())
asserter.EqualValues(expireAt, pyl.Expiry())
}

0 comments on commit cb89fad

Please sign in to comment.