Help us shape the future of Go! Take the 2026 Go Developer Survey

cache

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 22, 2016 License: BSD-3-Clause Imports: 1 Imported by: 0

README

Golang local cache

Implementation of simple local cache.

Build Status

Features & todos:

  • prevents "dogpile effect"
  • check data expiration time
  • lru policy

How to use


import "github.com/sima-land/go-local-cache"

getter := func(keys Keys) (Result, error) {
	r := Result{}
	for _, k := range keys {
		// obtain data for database
	}
	return r, nil
}

c := cache.New()

// 1. Next call is concurrently safe
// 2. If too many go-routines in our application are trying to call
// this getter with queue key equals 2 then only first call will actually be done
result, err := c.Get(int[]{1,2,3,4}, getter, 2)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

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

Cache represents cache

func New

func New() *Cache

New initializes new instance of cache

func (*Cache) Get

func (c *Cache) Get(keys Keys, getter Getter, queueKey interface{}) (Result, error)

Get returns data from cache by keys

If data not exist Getter function will be called to get data Data returned by Getter function will be placed into cache

func (*Cache) Len

func (c *Cache) Len() int

Len returns the number of items in the cache

func (*Cache) Set

func (c *Cache) Set(data Result)

Set saves result into cache

type Getter

type Getter func(keys Keys) (Result, error)

Getter func must be implemented by client and returns data by it's keys

type Keys

type Keys []int

Keys represents set of cache keys It always slice of int for two reasons: - we do not have separate Get and GetMulti functions - integers are most common used primary keys in db

type Queue

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

Queue of calls. Allows to prevent go pile effect

func NewQueue

func NewQueue() *Queue

NewQueue initialize new Queue

func (*Queue) Do

func (q *Queue) Do(keys Keys, getter Getter, queueKey interface{}) (Result, error)

Do place call in queue of calls with same queueKey and returns results of first call in queue

type Result

type Result map[int]interface{}

Result represents result returned by cache function

Jump to

Keyboard shortcuts

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