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

cache

package module
v0.2.0 Latest Latest
Warning

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

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

README

Golang local cache

Implementation of simple local cache.

Build Status Go Report Card

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
}

o := cache.Options({
	MaxEntries: 100,
})

c := cache.New(o)

// 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 one 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(options ...Options) *Cache

New initializes new instance of cache

func (*Cache) Flush added in v0.2.0

func (c *Cache) Flush(n int)

Flush removes last (rare used) n elements from 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) Remove added in v0.2.0

func (c *Cache) Remove(keys Keys)

Remove removes the provided keys from 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 Options added in v0.2.0

type Options struct {
	MaxEntries int
}

Options keeps cache options

type Queue

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

Queue of calls. Provides a duplicate function call suppression mechanism. Based on google group cache singleflight package.

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