cache

package
v2.27.0 Latest Latest
Warning

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

Go to latest
Published: Jan 5, 2025 License: MIT Imports: 5 Imported by: 0

README

Cache

Usage Example

package main

import (
	"context"
	"log"
	"time"

	"github.com/redis/go-redis/v9"

	"github.com/go-kratos-ecosystem/components/v2/cache"
	redisStore "github.com/go-kratos-ecosystem/components/v2/cache/redis"
)

var ctx = context.Background()

type User struct {
	Name string
	Age  int
}

func main() {
	// 创建个 Redis 连接客户端
	rdb := redis.NewClient(&redis.Options{
		Addr: "localhost:6379",
	})
	defer rdb.Close()

	// create a redis store
	store := redisStore.New(rdb, redisStore.Prefix("example:cache"))

	// create a cache repository
	repository := cache.NewRepository(store)

	// set cache
	ok, err := repository.Set(ctx, "key", User{
		Name: "example",
		Age:  18,
	}, time.Second*10)
	if err != nil {
		log.Fatal(err)
	}
	_ = ok

	// get cache
	var user User
	err = repository.Get(ctx, "key", &user)
	if err != nil {
		log.Fatal(err)
	}

	log.Printf("user: %+v", user)
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrNotFound = errors.New("cache: the key is not found")

Functions

This section is empty.

Types

type Addable

type Addable interface {
	// Add stores the value into the cache with an expiration time if the key does not exist.
	// If the key exists, the return value will be false, and the return error will be nil.
	// If the key does not exist, the return value will be true, and the return error will be nil.
	// otherwise, the return error will be the store error.
	Add(ctx context.Context, key string, value any, ttl time.Duration) (bool, error)
}

type Locker added in v2.16.0

type Locker interface {
	Lock(key string, ttl time.Duration) locker.Locker
}

type Manager added in v2.8.0

type Manager struct {
	Repository
	// contains filtered or unexported fields
}

func NewManager added in v2.8.0

func NewManager(repository Repository) *Manager

func (*Manager) Driver added in v2.8.0

func (m *Manager) Driver(names ...string) Repository

func (*Manager) Register added in v2.8.0

func (m *Manager) Register(name string, repository Repository)

type NullStore added in v2.16.0

type NullStore struct{}

func (*NullStore) Decrement added in v2.16.0

func (n *NullStore) Decrement(context.Context, string, int) (int, error)

func (*NullStore) Flush added in v2.16.0

func (n *NullStore) Flush(context.Context) (bool, error)

func (*NullStore) Forever added in v2.16.0

func (n *NullStore) Forever(context.Context, string, any) (bool, error)

func (*NullStore) Forget added in v2.16.0

func (n *NullStore) Forget(context.Context, string) (bool, error)

func (*NullStore) Get added in v2.16.0

func (*NullStore) GetPrefix added in v2.16.0

func (n *NullStore) GetPrefix() string

func (*NullStore) Has added in v2.16.0

func (n *NullStore) Has(context.Context, string) (bool, error)

func (*NullStore) Increment added in v2.16.0

func (n *NullStore) Increment(context.Context, string, int) (int, error)

func (*NullStore) Lock added in v2.16.0

func (*NullStore) Put added in v2.16.0

type Repository

type Repository interface {
	Store
	Addable

	Missing(ctx context.Context, key string) (bool, error)
	Delete(ctx context.Context, key string) (bool, error)
	Set(ctx context.Context, key string, value any, ttl time.Duration) (bool, error)
}

func NewRepository

func NewRepository(store Store) Repository

type Snapshot added in v2.27.0

type Snapshot[K comparable, V any] struct {
	// contains filtered or unexported fields
}

func (*Snapshot[K, V]) Lookup added in v2.27.0

func (s *Snapshot[K, V]) Lookup(key K, fn func() V) V

type SnapshotWithErr added in v2.27.0

type SnapshotWithErr[K comparable, V any] Snapshot[K, valueWithError[V]]

func (*SnapshotWithErr[K, V]) Lookup added in v2.27.0

func (c *SnapshotWithErr[K, V]) Lookup(key K, fn func() (V, error)) (V, error)

type Store

type Store interface {
	Locker

	// Has returns true if the key exists in the cache.
	// If the key does not exist, the return value will be false, and the return error will be nil.
	// If the key exists, the return value will be true, and the return error will be nil.
	// otherwise, the return error will be the store error.
	Has(ctx context.Context, key string) (bool, error)

	// Get retrieves the value from the cache.
	// If the key does not exist, the dest will be unchanged, and the return error will be ErrNotFound.
	// If the key exists, the value will be unmarshaled to dest, and the return error will be nil.
	// otherwise, the return error will be the store error.
	Get(ctx context.Context, key string, dest any) error

	// Put stores the value into the cache with an expiration time.
	// If put success, the return value will be true, and the return error will be nil.
	// otherwise, the return value will be false, and the return error will be the store error.
	Put(ctx context.Context, key string, value any, ttl time.Duration) (bool, error)

	// Increment increments the value in the cache.
	// If the key does not exist, the before default value is 0.
	Increment(ctx context.Context, key string, value int) (int, error)

	// Decrement decrements the value in the cache.
	// If the key does not exist, the before default value is 0.
	Decrement(ctx context.Context, key string, value int) (int, error)

	Forever(ctx context.Context, key string, value any) (bool, error)

	Forget(ctx context.Context, key string) (bool, error)

	Flush(ctx context.Context) (bool, error)

	GetPrefix() string
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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