cachey

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Mar 3, 2023 License: MIT Imports: 9 Imported by: 0

README

Cachey  

Cachey

Github top language Github language count Repository size License

About   |   Features   |   Technologies   |   Requirements   |   Starting   |   License   |   Author


🎯 About

Cachey is a simple, easy-to-use caching of function values based on redis or memory in Go.

✨ Features

✔ Simple and chainable methods for settings and execute;
✔ Predefined result structure to handle function return value;
✔ Auto unmarshal result;

🚀 Technologies

The following tools were used in this project:

✅ Requirements

Before starting 🏁, you need to have Git and Go installed.

🏁 Install

go get -u github.com/wang-junxi/cachey

🏁 Use Example

Using memory to cache function values

mc := cache.New(time.Hour, time.Hour)
c := New(nil, mc)
// c := New(nil, nil) // Or just use memory cache with default config

// when caching 'string' value with memory
var (
  strPlaceholder string
  getName        = func(args ...interface{}) (interface{}, error) {
    return "fake-name", nil
  }
)

res, err := c.M().
  SetCacheKey("cache_key_name").
  SetFunc(getName).
  SetResult(strPlaceholder).
  Execute()

fmt.Println(res.(string), err)

// when caching 'Person' struct with memory
type Person struct {
  Name string
  Age  int
}

var (
  person    = Person{Name: "fake-name", Age: 25}
  getPerson = func(args ...interface{}) (interface{}, error) {
    return person, nil
  }
)

res, err = c.M().
  SetCacheKey("cache_key_person").
  SetFunc(getPerson).
  SetResult(Person{}).
  Execute()

fmt.Println(res.(Person), err)

Using redis to cache function values

rc := redis.NewClient(&redis.Options{Addr: "localhost:6379"})
c := New(rc, nil)

// when caching '[]int' slice with redis
var (
  intSlicePlaceholder []int
  getAges             = func(args ...interface{}) (interface{}, error) {
    return []int{25, 21, 28}, nil
  }
)

res, err := c.R().
  SetCacheKey("cache_key_ages").
  SetFunc(getAges).
  SetResult(intSlicePlaceholder).
  Execute()

fmt.Println(res.([]int), err)

// when caching '[]Person' slice with redis
type Person struct {
  Name string
  Age  int
}

var (
  person     = &Person{Name: "fake-name", Age: 25}
  persons    = []*Person{person, person}
  getPersons = func(args ...interface{}) (interface{}, error) {
    return persons, nil
  }
)

res, err = c.R().
  SetCacheKey("cache_key_persons").
  SetFunc(getPersons).
  SetResult([]*Person{}).
  Execute()

fmt.Println(res.([]*Person), err)

📝 License

This project is under license from MIT. For more details, see the LICENSE file.

 

Back to top

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

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

func New

func New(rc *redis.Client, cc *cache.Cache) *Client

func (*Client) M

func (c *Client) M() *Request

func (*Client) R

func (c *Client) R() *Request

type ClientType

type ClientType uint8
const (
	RedisClient ClientType = iota
	MemoryClient
)

type Func

type Func func(args ...interface{}) (interface{}, error)

type Request

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

func (*Request) Execute

func (r *Request) Execute(args ...interface{}) (interface{}, error)

func (*Request) SetCacheKey

func (r *Request) SetCacheKey(cacheKey string) *Request

func (*Request) SetExpiration

func (r *Request) SetExpiration(expiration time.Duration) *Request

func (*Request) SetFunc

func (r *Request) SetFunc(f Func) *Request

func (*Request) SetResultType

func (r *Request) SetResultType(result interface{}) *Request

Jump to

Keyboard shortcuts

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