# README

Cachey
About | Features | Technologies | Requirements | Install | Use Example | License | Author
:dart: About
Cachey is a simple, easy-to-use caching of function values based on redis or memory in Go.
:sparkles: Features
:heavy_check_mark: Simple and chainable methods for settings and execute;
:heavy_check_mark: Predefined result structure to handle function return value;
:heavy_check_mark: Auto unmarshal result;
:rocket: Technologies
The following tools were used in this project:
:white_check_mark: Requirements
Before starting :checkered_flag:, you need to have Git and Go installed.
:checkered_flag: Install
go get -u github.com/wang-junxi/cachey
:checkered_flag: 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.
:memo: License
This project is under license from MIT. For more details, see the LICENSE file.