Skip to content

worldline-go/cache

Repository files navigation

cache

Simple cache with using different libraries.

go get github.com/worldline-go/cache

Usage

Result of cache get, set and delete methods; stores could be force to types.

Get(ctx context.Context, key K) (V, bool, error)
Set(ctx context.Context, key K, value V) error
Delete(ctx context.Context, key K) error

memory

priceCache, err := cache.New[string, int](ctx,
    memory.Store,
    cache.WithStoreConfig(memory.Config{
        MaxItems: 100,
        TTL:      10 * time.Minute,
    }),
)

err := priceCache.Set(ctx, "key", 100)
v, ok, err := vcache.Get(ctx, "key")

redis

Use github.com/worldline-go/conn/connredis to create a redis client.

redisClient, err := connredis.New(connredis.Config{
    Address: s.container.Address(),
})
if err != nil {
    // handle error
}

c, err := cache.New(s.T().Context(),
    redis.Store(redisClient),
    cache.WithStoreConfig(redis.Config{
        TTL: 3 * time.Second,
    }),
)
if err != nil {
    // handle error
}