dumbcache

package module
v1.1.7 Latest Latest
Warning

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

Go to latest
Published: Jun 6, 2022 License: Apache-2.0 Imports: 12 Imported by: 0

README

Dumbcache

Go Go Reference

Cache list, count with filter param golang, using struct, hashkey

Structure

we hash your request object to md5 hashing and add a prefix count or list. data storage to redis using get/set with expire time.(default by 2 mins)

Installing
go get github.com/teng231/dumbcache
Example
  • LIST
func ListData() {
	d := &DumbCache{}
	err := d.Connect("localhost:6379", "", 0, 5*time.Second, 5*time.Minute)
	if err != nil {
		log.Print(1, err)
	}
	data := []*Partner{}
	err = d.List(&PartnerRequest{Id: 10, Limit: 2}, &data, func() (interface{}, error) {
        list, err := db.ListPartners(req)
		return list, err
	})
	if err != nil {
		log.Print(err)
	}
	log.Print(data)
}
  • Count
func CountData(t *testing.T) {
	d := &DumbCache{}
	err := d.Connect("localhost:6379", "", 0, 5*time.Second, 5*time.Minute)
	if err != nil {
		log.Print(1, err)
	}
	var c int64 = 0
	err = d.Count(&PartnerRequest{Id: 10, Limit: 3}, &c, func() (int64, error) {
		count, err := db.CountPartners(req)
		return count, err
	})
	if err != nil {
		log.Print(err)
	}
	log.Print(c)
}
  • CalcInt
func CalcData(t *testing.T) {
	d := &DumbCache{}
	err := d.Connect("localhost:6379", "", 0, 5*time.Second, 5*time.Minute)
	if err != nil {
		log.Print(1, err)
	}
	var c int64 = 0
	err = d.CalcInt(&PartnerRequest{Id: 10, Limit: 3}, &c, func() (int64, error) {
		count, err := db.SumPartners(req)
		return count, err
	})
	if err != nil {
		log.Print(err)
	}
	log.Print(c)
}
  • Expire
func Expire() {
	d := &DumbCache{}
	err := d.Connect("localhost:6379", "", 0, 5*time.Second, 5*time.Minute)
	if err != nil {
		log.Print(1, err)
	}
	var c int64 = 0
	err = d.Expire(&PartnerRequest{Id: 10, Limit: 3})
	if err != nil {
		log.Print(err)
	}
	log.Print(c)
}

Documentation

Index

Constants

View Source
const (
	CLIST    = "c_list_"
	CCOUNT   = "c_count_"
	CCACLINT = "c_calc_int_"
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Config added in v1.1.1

type Config struct {
	Addr          string
	Password      string
	Db            int
	Timeout       time.Duration
	Duration      time.Duration
	MaxSizeLocal  int
	LocalDuration time.Duration
}

type DumbCache

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

func (*DumbCache) CalcInt added in v1.1.3

func (d *DumbCache) CalcInt(input interface{}, out *int64, handler func() (int64, error)) error

CalcInt cache count

func (*DumbCache) Connect

func (d *DumbCache) Connect(config *Config) error

func (*DumbCache) Count

func (d *DumbCache) Count(input interface{}, out *int64, handler func() (int64, error)) error

Count cache count

func (*DumbCache) Expire

func (d *DumbCache) Expire(input interface{}) error

Expire expire 2 key count and list

func (*DumbCache) List

func (d *DumbCache) List(input, out interface{}, handler func() (interface{}, error)) error

List list cache data out is a pointer

func (*DumbCache) ListWithProto added in v1.1.6

func (d *DumbCache) ListWithProto(input interface{}, out proto.Message, handler func() (proto.Message, error)) error

ListWithProto list cache data out is a pointer

func (*DumbCache) MakeHash

func (d *DumbCache) MakeHash(in interface{}) (string, error)

func (*DumbCache) ParseData

func (d *DumbCache) ParseData(input, out interface{}) error

ParseData out is a pointer

func (*DumbCache) ParseDataWithProto added in v1.1.6

func (d *DumbCache) ParseDataWithProto(input interface{}, out proto.Message) error

ParseData out is a pointer

func (*DumbCache) Set

func (d *DumbCache) Set(prefix string, input, payload interface{}) error

func (*DumbCache) SetWithProto added in v1.1.6

func (d *DumbCache) SetWithProto(prefix string, input interface{}, payload proto.Message) error

type IDumbCache

type IDumbCache interface {
	// connect redis
	Connect(config *Config) error

	// Set raw to get raw
	Set(prefix string, input, payload interface{}) error
	SetWithProto(prefix string, input interface{}, payload proto.Message) error
	// Make hash key
	MakeHash(in interface{}) (string, error)
	ParseData(input, out interface{}) error
	ParseDataWithProto(input interface{}, payload proto.Message) error

	Expire(input interface{}) error
	List(input, out interface{}, handler func() (interface{}, error)) error
	ListWithProto(input interface{}, out proto.Message, handler func() (proto.Message, error)) error

	Count(input interface{}, out *int64, handler func() (int64, error)) error
	CalcInt(input interface{}, out *int64, handler func() (int64, error)) error
}

type ILocalModule added in v1.1.1

type ILocalModule interface {
	Add(key interface{}, value interface{}) error
	Get(key interface{}) (string, error)
}

type LocalModule added in v1.1.1

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

func CreateLocalModule added in v1.1.1

func CreateLocalModule(size int, duration time.Duration) *LocalModule

func (*LocalModule) Del added in v1.1.1

func (l *LocalModule) Del(key interface{})

func (*LocalModule) Get added in v1.1.1

func (l *LocalModule) Get(key interface{}) (string, error)

func (*LocalModule) Set added in v1.1.1

func (l *LocalModule) Set(key interface{}, value interface{}) error

type Message added in v1.1.1

type Message struct {
	Created int64
	Payload string
}

Jump to

Keyboard shortcuts

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