cache

package module
v3.0.5 Latest Latest
Warning

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

Go to latest
Published: Apr 19, 2024 License: MIT Imports: 5 Imported by: 2

README

cache

cache based on go-redis

Installation

Install:

go get -u github.com/isfk/go-cache/v3

Import:

import "github.com/isfk/go-cache/v3"

QuickStart

Init with cache.New()
rdb := redisV9.NewClient(&redisV9.Options{
    Addr:     "127.0.0.1:6379",
})

c := cache.New(
    context.Background(),
    rdb,
    cache.WithPrefix("test"),
    cache.WithExpired(600*time.Second),
)
Command
  • use Tag & Set & Get

Set is the same as Set, but just with Tag together.

Get without Tag.

c.Tag([]string{"tag:user:all", "tag:user:1"}...).Set(ctx, "key:user:1", &proto.User{Id: 1, Nickname: "111"})
c.Tag([]string{"tag:user:all", "tag:user:2"}...).Set(ctx, "key:user:2", &proto.User{Id: 2, Nickname: "222"})

info := &proto.User{}
c.Get(ctx, "key:user:1", info)
c.Get(ctx, "key:user:2", info)
c.Tag([]string{"tag:user:all"}...).Flush(ctx)
  • use redis

All redis command check: https://godoc.org/github.com/redis/go-redis/v9

c.redis.Set("key", "value", time.Hour).Err()
c.redis.Get("key").Result()
c.redis.command()...

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

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

Cache Cache

func New

func New(ctx context.Context, client *redis.Client, opts ...Option) *Cache

New New

func NewCluster

func NewCluster(ctx context.Context, client *redis.ClusterClient) *Cache

NewCluster NewCluster

func (*Cache) AddTag added in v3.0.3

func (c *Cache) AddTag(tag ...string) *Cache

Tag .AddTag()

func (*Cache) Del added in v3.0.4

func (c *Cache) Del(ctx context.Context, key string) error

Set .Tag().Del()

func (*Cache) Flush

func (c *Cache) Flush(ctx context.Context) error

Flush .Tag().Flush()

func (*Cache) Get

func (c *Cache) Get(ctx context.Context, key string, val interface{}) error

Get .Get()

func (*Cache) Set

func (c *Cache) Set(ctx context.Context, key string, val interface{}) error

Set .Tag().Set()

func (*Cache) Tag

func (c *Cache) Tag(tag ...string) *Cache

Tag .Tag()

type Option

type Option func(*Options)

func WithExpired

func WithExpired(exp time.Duration) Option

func WithPrefix

func WithPrefix(prefix string) Option

type Options

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

Jump to

Keyboard shortcuts

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