cache

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Aug 27, 2020 License: MIT Imports: 5 Imported by: 0

README

Cache middleware

Storage interface represents a cache storage

type Storage interface {
	Save(key Key, value api.Response) error
	Load(key Key) (api.Response, error)
}
Usage:
package main
import (
    "strings"
    "time"

    "github.com/SevereCloud/vksdk/api"
    sdkutil "github.com/tdakkota/vksdkutil"
    "github.com/tdakkota/vksdkutil/middleware/cache"
    "github.com/tdakkota/vksdkutil/middleware/cache/mapcache"
)

// cache all requests with method which starts with "get"
func cacheable(method string, param api.Params) bool {
    return strings.Contains(method, "get")  
}

func main() {
    storage := mapcache.NewMap() // create in-memory cache based on Go map

    vk := sdkutil.BuildSDK("token").
        WithMiddleware(cache.Create(storage, cacheable)). 
        Complete()
    // ...
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrCacheMiss = errors.New("cache miss")

Functions

func Create

func Create(c Storage, cacheable CacheableFunc) sdkutil.Middleware

Types

type CacheableFunc

type CacheableFunc = func(method string, param api.Params) bool

type Key

type Key struct {
	Method string
	Params OrderedParams
}

Key is cache key.

func NewKey

func NewKey(method string, params api.Params) Key

func (Key) String

func (k Key) String() string

type OrderedParams

type OrderedParams []Param

OrderedParams is ordered VK API method arguments.

func CreateParams

func CreateParams(params api.Params) OrderedParams

func (OrderedParams) Len

func (o OrderedParams) Len() int

func (OrderedParams) Less

func (o OrderedParams) Less(i, j int) bool

func (OrderedParams) Swap

func (o OrderedParams) Swap(i, j int)

type Param

type Param struct {
	Key   string
	Value interface{}
}

Param is VK API method argument.

type Storage

type Storage interface {
	Save(key Key, value api.Response) error
	Load(key Key) (api.Response, error)
}

Storage interface represents a cache storage instance.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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