cachey

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Mar 4, 2023 License: MIT Imports: 10 Imported by: 0

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


🎯 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

Documentation

Overview

Package cachey provides a simple and flexible way to cache the results of functions using either redis or memory as the backend.

Package cachey provides a simple and flexible way to cache the results of functions using either redis or memory as the backend.

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
}

Client represents a cachey client that can use either redis or memory as the cache backend.

func New

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

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

func (*Client) EnableDebug added in v1.0.2

func (r *Client) EnableDebug() *Client

EnableDebug enable debugging more detailed message with zerolog.

func (*Client) M

func (c *Client) M() *Request

M returns a new request that uses memory as the cache backend.

func (*Client) R

func (c *Client) R() *Request

R returns a new request that uses redis as the cache backend.

type ClientType

type ClientType uint8

ClientType represents the type of cache backend to use for a request.

const (
	RedisClient  ClientType = iota // Use redis as the cache backend
	MemoryClient                   // Use memory as the cache backend
)

type Func

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

Func represents a function whose result can be cached by cachey.

type Request

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

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

func (*Request) Execute

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

Execute executes this request by first trying to get the result from the cache with the given key. If it fails, it executes the function and caches its result with the given key and expiration time. It returns the result and an error if any.

func (*Request) SetCacheKey

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

SetCacheKey sets the cache key for this request and returns itself for chaining.

func (*Request) SetExpiration

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

SetExpiration sets the expiration time for caching the result of this request and returns itself for chaining.

func (*Request) SetFunc

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

SetFunc sets the function to execute and cache its result for this request and returns itself for chaining.

func (*Request) SetResultType

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

SetResultType sets the type of result expected from the function execution for this request and returns itself for chaining.

Jump to

Keyboard shortcuts

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