Skip to content

wang-junxi/cachey

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

13 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Cachey  

Cachey

Github top language Github language count Repository size License Go Report Go Reference codecov

About   |   Features   |   Technologies   |   Requirements   |   Install   |   Use Example   |   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).EnableDebug()
/*
  // Or just use memory cache with default config
  c := New(nil, nil)
*/

// 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)

For more details, see the 'TestRequest_Execute' in request_test.go file.

📝 License

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

 

Back to top

About

Simple, easy-to-use caching of function values based on redis or memory in Go.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages