cachee

package
v0.0.0-...-fd9e474 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 29, 2023 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CCacheKvStore

type CCacheKvStore[V any] struct {
	Cache *ccache.Cache
}

func NewCCacheKvStore

func NewCCacheKvStore[V any](cc *ccache.Cache) *CCacheKvStore[V]

func (*CCacheKvStore[V]) Del

func (cc *CCacheKvStore[V]) Del(ctx context.Context, key string) error

func (*CCacheKvStore[V]) Get

func (cc *CCacheKvStore[V]) Get(ctx context.Context, key string) (v V, exist bool, err error)

func (*CCacheKvStore[V]) Set

func (cc *CCacheKvStore[V]) Set(ctx context.Context, key string, value V, expiration time.Duration) error

type Encoder

type Encoder[V any] struct {
	Encode func(V) ([]byte, error)
	Decode func(data []byte) (V, error)
}

func JsonEncoder

func JsonEncoder[V any]() Encoder[V]

func MsgPackEncoder

func MsgPackEncoder[V any]() Encoder[V]

type FreeCacheKvStore

type FreeCacheKvStore[V any] struct {
	Cache *freecache.Cache
	Enc   codec.EncodeDecoder[V]
}
Example
store := NewFreeCacheKvStore(freecache.NewCache(32), MsgPackEncoder[string]())
store := NewFreeCacheKvStore(freecache.NewCache(32), codec.JsonCodec[string]())
ctx := context.TODO()

v, exist, err := store.Get(ctx, `name`)
fmt.Println(v == ``, exist, err)

err = store.Set(ctx, `name`, `alice`, time.Second)
fmt.Println(err)

v, exist, err = store.Get(ctx, `name`)
fmt.Println(v, exist, err)

time.Sleep(time.Second)
v, exist, err = store.Get(ctx, `name`)
fmt.Println(v == ``, exist, err)
Output:

true false <nil>
<nil>
alice true <nil>
true false <nil>

func NewFreeCacheKvStore

func NewFreeCacheKvStore[V any](fc *freecache.Cache, enc codec.EncodeDecoder[V]) *FreeCacheKvStore[V]

func (*FreeCacheKvStore[V]) Del

func (fc *FreeCacheKvStore[V]) Del(ctx context.Context, key string) error

func (*FreeCacheKvStore[V]) Get

func (fc *FreeCacheKvStore[V]) Get(ctx context.Context, key string) (v V, exist bool, err error)

func (*FreeCacheKvStore[V]) Set

func (fc *FreeCacheKvStore[V]) Set(ctx context.Context, key string, value V, expiration time.Duration) error

type KvItem

type KvItem[V any] struct {
	Key        string
	Expiration time.Duration

	Load func() (V, error)
	// contains filtered or unexported fields
}

func (*KvItem[V]) GetOrLoad

func (item *KvItem[V]) GetOrLoad(ctx context.Context) (v V, err error)

type KvStore

type KvStore[K comparable, V any] interface {
	Get(ctx context.Context, key K) (v V, exist bool, err error)
	Set(ctx context.Context, key K, value V, expiration time.Duration) error
	Del(ctx context.Context, key K) error
}

type RedisKvStore

type RedisKvStore[V any] struct {
	Client *redis.Client
	Enc    codec.EncodeDecoder[V]
}
Example
db, mock := redismock.NewClientMock()

store := NewRedisKvStore(db, codec.JsonCodec[string]())
ctx := context.TODO()

mock.ExpectGet(`name`).RedisNil()
v, exist, err := store.Get(ctx, `name`)
fmt.Println(v == ``, exist, err)

cmd1 := mock.ExpectSet(`name`, []byte(`"alice"`), time.Second)
cmd1.SetVal("")
cmd1.SetErr(nil)
err = store.Set(ctx, `name`, `alice`, time.Second)
fmt.Println(err)

cmd2 := mock.ExpectGet(`name`)
cmd2.SetVal(`"alice"`)
cmd2.SetErr(nil)
v, exist, err = store.Get(ctx, `name`)
fmt.Println(v, exist, err)

time.Sleep(time.Second)
mock.ExpectGet(`name`).RedisNil()
v, exist, err = store.Get(ctx, `name`)
fmt.Println(v == ``, exist, err)
Output:

true false <nil>
<nil>
alice true <nil>
true false <nil>

func NewRedisKvStore

func NewRedisKvStore[V any](c *redis.Client, enc codec.EncodeDecoder[V]) *RedisKvStore[V]

func (*RedisKvStore[V]) Del

func (r *RedisKvStore[V]) Del(ctx context.Context, key string) error

func (*RedisKvStore[V]) Get

func (r *RedisKvStore[V]) Get(ctx context.Context, key string) (v V, exist bool, err error)

func (*RedisKvStore[V]) Set

func (r *RedisKvStore[V]) Set(ctx context.Context, key string, value V, expiration time.Duration) error

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL