cacheaside

package module
v1.0.5 Latest Latest
Warning

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

Go to latest
Published: Dec 30, 2022 License: MIT Imports: 10 Imported by: 0

README

cacheaside

cache 代理

使用方式

redis hash

package cacheaside

import (
	"context"
	"fmt"
	"time"

	"github.com/erkesi/cacheaside/code"
	"github.com/erkesi/cacheaside/caredis"
	"github.com/go-redis/redis"
)

func demo() {
	type User struct {
		Extra map[string]string
	}

	genUser := func(key string, field string) *User {
		return &User{
			Extra: map[string]string{field: field},
		}
	}

	ca := NewHCacheAside(&code.Json{}, &caredis.NewRedisWrap(&redis.NewClient(&redis.Options{
		Addr: "127.0.0.1:6379",
	})))

	caf := ca.HFetch(
		// 回源查询
		func(ctx context.Context, key string, fields []string, extra ...interface{}) ([]interface{}, error) {
			var res []interface{}
			for _, field := range fields {
				if field == "nil" {
					continue
				}
				res = append(res, genUser(key, field))
			}
			return res, nil
		},
		// 回源查询到的结果项(v)生成 redis hash field
		func(ctx context.Context, v interface{}, extra ...interface{}) (string, error) {
			for k := range v.(*User).Extra {
				return k, nil
			}
			return "", nil
		},
		WithTTL(time.Hour))

	// 查询 hash 多个field
	var us []*User
	err := caf.HMGet(context.Background(), "1", []string{"nil", "Name", "Age"}, &us)
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println(us)

	// 删除 hash 多个field
	err = caf.HMDel(context.Background(), "1", "Name", "Age")
	if err != nil {
		fmt.Println(err)
		return
	}

	// 删除 hash key
	err = caf.HDel(context.Background(), "1")
	if err != nil {
		fmt.Println(err)
		return
	}
}

redis string

package cacheaside

import (
	"context"
	"fmt"
	"github.com/erkesi/cacheaside/code"
	csredis "github.com/erkesi/cacheaside/redis"
	"github.com/go-redis/redis"
	"time"
)

func demo() {

	type User struct {
		Extra map[string]string
	}
	genUser := func(key string, field string) *User {
		return &User{
			Extra: map[string]string{field: field},
		}
	}

	ca := NewCacheAside(&code.Json{}, &caredis.NewRedisWrap(&redis.NewClient(&redis.Options{
		Addr: "127.0.0.1:6379",
	})))
	caf := ca.Fetch(
		// 回源查询
		func(ctx context.Context, keys []string, extra ...interface{}) ([]interface{}, error) {
			var res []interface{}
			for _, key := range keys {
				if key == "nil" {
					continue
				}
				res = append(res, genUser(key))
			}
			return res, nil
		},
		// 回源查询到的结果项(v)生成 redis hash field
		func(ctx context.Context, v interface{}, extra ...interface{}) (string, error) {
			return v.(*User).Id, nil
		},
		WithTTL(time.Hour))

	// 查询 多个key
	var us []*User
	err := caf.MGet(context.Background(), []string{"0", "1", "2", "nil"}, &us)
	if err != nil {
		fmt.Println(err)
		return
	}
	fmt.Println(us)

	// 删除 key
	err = caf.MDel(context.Background(), "0", "1")
	if err != nil {
		fmt.Println(err)
		return
	}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CacheAside

type CacheAside struct {
	// contains filtered or unexported fields
}

func NewCacheAside

func NewCacheAside(code code.Coder, cache cache.Cacher, namespance string, opts ...OptFn) *CacheAside

func NewHCacheAside

func NewHCacheAside(code code.Coder, hcache cache.HCacher, namespance string, opts ...OptFn) *CacheAside

func (*CacheAside) Fetch

func (ca *CacheAside) Fetch(fetchSource FetchSource, genCacheKey GenCacheKey, opts ...OptFn) *Fetcher

func (*CacheAside) HFetch added in v1.0.4

func (ca *CacheAside) HFetch(fetchSource FetchSourceHash, genCacheHashField GenCacheHashField, opts ...OptFn) *HFetcher

type FetchSource

type FetchSource func(ctx context.Context, keys []string,
	extra ...interface{}) ([]interface{}, error)

type FetchSourceHash added in v1.0.4

type FetchSourceHash func(ctx context.Context, key string, fields []string,
	extra ...interface{}) ([]interface{}, error)

type Fetcher added in v1.0.4

type Fetcher struct {
	// contains filtered or unexported fields
}

func (*Fetcher) Get added in v1.0.4

func (f *Fetcher) Get(ctx context.Context, key string, res interface{},
	extra ...interface{}) (bool, error)

func (*Fetcher) MDel added in v1.0.4

func (f *Fetcher) MDel(ctx context.Context, keys ...string) error

func (*Fetcher) MGet added in v1.0.4

func (f *Fetcher) MGet(ctx context.Context, keys []string, res interface{},
	extra ...interface{}) error

type GenCacheHashField added in v1.0.4

type GenCacheHashField func(ctx context.Context, v interface{},
	extra ...interface{}) (string, error)

type GenCacheKey

type GenCacheKey func(ctx context.Context, v interface{},
	extra ...interface{}) (string, error)

type HFetcher added in v1.0.4

type HFetcher struct {
	// contains filtered or unexported fields
}

func (*HFetcher) HDel added in v1.0.4

func (hf *HFetcher) HDel(ctx context.Context, key string) error

func (*HFetcher) HGet added in v1.0.4

func (hf *HFetcher) HGet(ctx context.Context, key, field string, res interface{},
	extra ...interface{}) (bool, error)

func (*HFetcher) HMDel added in v1.0.4

func (hf *HFetcher) HMDel(ctx context.Context, key string, fields ...string) error

func (*HFetcher) HMGet added in v1.0.4

func (hf *HFetcher) HMGet(ctx context.Context, key string, fields []string, res interface{},
	extra ...interface{}) error

type Logger added in v1.0.4

type Logger interface {
	Debugf(ctx context.Context, format string, v ...interface{})
	Wranf(ctx context.Context, format string, v ...interface{})
}

type OptFn added in v1.0.4

type OptFn func(opt *Option)

func WithLogger added in v1.0.4

func WithLogger(log Logger) OptFn

func WithStrategy added in v1.0.4

func WithStrategy(strategy Strategy) OptFn

func WithTTL added in v1.0.4

func WithTTL(ttl time.Duration) OptFn

func WithcacheGetErrHandler added in v1.0.4

func WithcacheGetErrHandler(cacheGetErrHandler func(ctx context.Context, err error, keys, fields []string,
	extra ...interface{})) OptFn

func WithcacheSetErrHandler added in v1.0.4

func WithcacheSetErrHandler(cacheSetErrHandler func(ctx context.Context, err error, keys, fields []string,
	extra ...interface{}) error) OptFn

type Option added in v1.0.4

type Option struct {
	// contains filtered or unexported fields
}

type Strategy added in v1.0.4

type Strategy string
const (
	// StrategyCacheFailBackToSource 读取缓存失败则回源查询
	StrategyCacheFailBackToSource Strategy = "CacheFailBackToSource"
	// StrategyFirstUseCache 优先读取缓存(默认)
	StrategyFirstUseCache Strategy = "CacheFailBackToSource"
	// StrategyOnlyUseCache 仅仅读取缓存
	StrategyOnlyUseCache Strategy = "OnlyCache"
)

Directories

Path Synopsis
cache module
code module

Jump to

Keyboard shortcuts

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