Categorygithub.com/wang-junxi/cachey
modulepackage
1.0.2
Repository: https://github.com/wang-junxi/cachey.git
Documentation: pkg.go.dev

# README

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


: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.

 

Back to top

# Functions

New creates a new cachey client with the given redis and memory clients.

# Constants

Use memory as the cache backend.
Use redis as the cache backend.

# Structs

Client represents a cachey client that can use either redis or memory as the cache backend.
Request represents a cachey request that can execute a function and cache its result using either redis or memory as the cache backend.

# Type aliases

ClientType represents the type of cache backend to use for a request.
Func represents a function whose result can be cached by cachey.