requestcache

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2019 License: MIT Imports: 12 Imported by: 10

README

requestcache

A library of caches for on-demand generated content.

Documentation is at http://godoc.org/github.com/ctessum/requestcache.

Documentation

Overview

Package requestcache provides functions for caching on-demand generated data.

Index

Constants

This section is empty.

Variables

View Source
var FileExtension = ".dat"

FileExtension is appended to request key names to make up the names of files being written to disk.

Functions

func MarshalGob

func MarshalGob(data interface{}) ([]byte, error)

MarshalGob marshals an interface to a byte array and fulfills the requirements for the Disk cache marshalFunc input.

func UnmarshalGob

func UnmarshalGob(b []byte) (interface{}, error)

UnmarshalGob unmarshals an interface from a byte array and fulfills the requirements for the Disk cache unmarshalFunc input.

Types

type Cache

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

Cache is a holder for one or multiple caches.

func NewCache

func NewCache(processor ProcessFunc, numProcessors int, cachefuncs ...CacheFunc) *Cache

NewCache creates a new set of caches for on-demand generated content, where processor is the function that creates the content, numProcessors is the number of processors that will be working in parallel, and cachefuncs are the caches to be used, listed in order of priority.

func (*Cache) NewRequest

func (c *Cache) NewRequest(ctx context.Context, requestPayload interface{}, key string) *Request

NewRequest creates a new request where requestPayload is the input data that will be used to generate the results and key is a unique key.

func (*Cache) Requests

func (c *Cache) Requests() []int

Requests returns the number of requests that each cache has received. The last index in the output is the number of requests received by the processor. So, for example, the miss rate for the first cache in c is r[len(r)-1] / r[0], where r is the result of this function.

type CacheFunc

type CacheFunc func(in chan *Request) (out chan *Request)

A CacheFunc can be used to store request results in a cache.

func Deduplicate

func Deduplicate() CacheFunc

Deduplicate avoids duplicating requests.

func Disk

func Disk(dir string, marshalFunc func(interface{}) ([]byte, error), unmarshalFunc func([]byte) (interface{}, error)) CacheFunc

Disk manages an on-disk cache of results, where dir is the directory in which to store results, marshalFunc is the function to be used to marshal the data object to binary form, and unmarshalFunc is the function to be used to unmarshal the data from binary form.

func GoogleCloudStorage

func GoogleCloudStorage(ctx context.Context, bucket, subdir string, marshalFunc func(interface{}) ([]byte, error), unmarshalFunc func([]byte) (interface{}, error)) (CacheFunc, error)

GoogleCloudStorage manages an cache of results in Google Cloud Storage, where bucket is the bucket in which to store results, subdir is the bucket subdirectory, if any, that should be used, marshalFunc is the function to be used to marshal the data object to binary form, and unmarshalFunc is the function to be used to unmarshal the data from binary form.

func HTTP

func HTTP(addr string, unmarshalFunc func([]byte) (interface{}, error)) CacheFunc

HTTP retrieves cached requests over an HTTP connection, where addr is the address where results are stored and unmarshalFunc is the function to be used to unmarshal the data from binary form. This function does not cache requests, it only retrieves previously cached requests.

func Memory

func Memory(maxEntries int) CacheFunc

Memory manages an in-memory cache of results, where maxEntries is the max number of items in the cache. If the results returned by this cache are modified by the caller, they may also be modified in the cache.

func SQL

func SQL(ctx context.Context, db *sql.DB, marshalFunc func(interface{}) ([]byte, error), unmarshalFunc func([]byte) (interface{}, error)) (CacheFunc, error)

SQL manages a cache of results in an SQL database, where db is the database connection, marshalFunc is the function to be used to marshal the data object to binary form, and unmarshalFunc is the function to be used to unmarshal the data from binary form.

type ProcessFunc

type ProcessFunc func(ctx context.Context, requestPayload interface{}) (resultPayload interface{}, err error)

ProcessFunc defines the format of functions that can be used to process a request payload and return a resultPayload.

type Request

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

Request holds information about a request that is to be handled either by a cache or a ProcessFunc.

func (*Request) Result

func (r *Request) Result() (interface{}, error)

Result sends the request for processing, waits for the result, and returns the result and any errors that occurred while processing.

Jump to

Keyboard shortcuts

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