pcache

package module
v0.0.0-...-2a13de7 Latest Latest
Warning

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

Go to latest
Published: Oct 18, 2014 License: MIT Imports: 8 Imported by: 0

README

pcache

Documentation

Overview

ProxyCache - universal cache filling algorithm

Idea

Instead of trying to making cache lookups and determining if you should perform some expensive calculation, you pass calculation fetcher as a delegate into ProxyCache which will figure out caching state by itself and use fetcher if necessary.

Parameters

Algorithm considers various parameters that might affect caching behavior: item expiration, ttl (specified per-request), request timeout, throttle time between fetcher calls, results of validation (preformed by validator func).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ProxyCache

func ProxyCache(key string, target interface{}, fetcher Fetcher, spec *ResourceSpec) error

Types

type Fetcher

type Fetcher func() (interface{}, error)

type Lock

type Lock interface {
	Release()
}

type Locker

type Locker interface {
	Locked(string) bool
	Lock(string, time.Duration) Lock // (key, timeout) -> Lock
	Wait(string) bool                // key -> was_released
}

type RedisPCache

type RedisPCache struct {
	Prefix string
	Pool   *redis.Pool
}

Redis-flavor of ProxyCache, that uses redis for both Store and Locker backends

func RedisPCacheNew

func RedisPCacheNew(prefix, network, addr string) RedisPCache

func (RedisPCache) Call

func (c RedisPCache) Call(
	key string,

	target interface{},
	fetcher func() (interface{}, error),
	validator func(interface{}) bool,

	expire time.Duration,
	ttl time.Duration,
	throttle time.Duration,
	timeout time.Duration,
) error

type ResourceSpec

type ResourceSpec struct {
	Store                          Store
	Locker                         Locker
	Validator                      Validator
	Expire, Ttl, Throttle, Timeout time.Duration
}

type Store

type Store interface {
	Get(string, interface{}) (bool, *time.Time, *time.Time) // (key, target) -> (retrieved, creation_time, last_fetch_time)
	Set(string, interface{}, time.Duration) error           // (key, item, expire) -> error
}

Storage backend is assumed to implement some kind of cache replacement algorithm

type Validator

type Validator func(interface{}) bool

Jump to

Keyboard shortcuts

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