gcache

package module
v0.0.0-...-76ab7dd Latest Latest
Warning

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

Go to latest
Published: Apr 28, 2021 License: MIT Imports: 5 Imported by: 0

README

gcache

golang cache interface and some algorithm implementation

  • fifo
  • lfu
  • lru
  • lru-k
  • 2q

example

github.com/powerpuffpenguin/gcache
package main

import (
	"fmt"

	"github.com/powerpuffpenguin/gcache"
)

func main() {
	c := gcache.NewLRU(
		gcache.WithLRUCapacity(3),
	)
	for i := 0; i < 4; i++ {
		key := i
		val := fmt.Sprintf(`val %d`, i)
		c.Put(key, val)
	}

	for i := 0; i < 4; i++ {
		key := i
		val, exists := c.Get(key)
		if exists {
			fmt.Printf("key=%v val=%v\n", key, val)
		} else {
			fmt.Printf("key=%v not exists\n", key)
		}
	}
}

interface

gcache provides two interface for users to use.

  1. Cache goroutine safe cache
  2. LowCache Implementation of goroutine not safe cache low-level algorithm

Cache

Usually you should use the Cache interface directly.

type Cache interface {
	// Add the value to the cache, only when the key does not exist
	Add(key, value interface{}) (added bool)
	// Put key value to cache
	Put(key, value interface{})
	// Get return cache value
	Get(key interface{}) (value interface{}, exists bool)
	// BatchPut pairs to cache
	BatchPut(pair ...interface{})
	// BatchGet return cache values
	BatchGet(key ...interface{}) (vals []Value)
	// Delete key from cache
	Delete(key ...interface{}) (changed int)
	// Len returns the number of cached data
	Len() (count int)
	// Clear all cached data
	Clear()
}

gcache provides several NewXXX functions for creating Cache. In addition, several WithXXX settings are provided to guide parameters for the caching algorithm.

capacity := 1000
expiry := time.Minute
duration := time.Minute
// basic
lru := gcache.NewLRU(
	gcache.WithLRUCapacity(capacity), // cache capacity
	gcache.WithLRUExpiry(expiry),     // inactivity expiration time
	gcache.WithLRUClear(duration),    // the timer clears the expired cache
)
lfu := gcache.NewLFU(
	gcache.WithLFUCapacity(capacity),
	gcache.WithLFUExpiry(expiry),
	gcache.WithLFUClear(duration),
)
fifo := gcache.NewFIFO(
// WithFIFOXXX
)
// complex
lruk2 := gcache.NewLRUK(
	gcache.WithLRUK(2),
	gcache.WithLRUKHistoryOnlyKey(true),        // if true history only save key not save value, false save key and value
	gcache.WithLRUKHistory(gcache.NewLowLRU()), // history use lru
)
lruk3 := gcache.NewLRUK(
	gcache.WithLRUK(3),
)
// 2q
c2q := gcache.NewLRUK(
	gcache.WithLRUK(2),
	gcache.WithLRUKHistoryOnlyKey(false),
	gcache.WithLRUKHistory(gcache.NewLowFIFO()), // history use fifo
)

LowCache

The LowCache interface is a low-level implementation that implements the basic algorithm.

// Low-level caching is usually only used when combining multiple caching algorithms
type LowCache interface {
	// Clear Expired cache
	ClearExpired()
	// Add the value to the cache, only when the key does not exist
	Add(key, value interface{}) (added bool)
	// Put key value to cache
	Put(key, value interface{}) (delkey, delval interface{}, deleted bool)
	// Get return cache value
	Get(key interface{}) (value interface{}, exists bool)
	// Delete key from cache
	Delete(key ...interface{}) (changed int)
	// Len returns the number of cached data
	Len() int
	// Clear all cached data
	Clear()
}

gcache provides several NewLowXXX functions for creating LowCache. In addition, several WithLowXXX settings are provided to guide parameters for the caching algorithm.

fifo

gcache.NewFIFO(
	gcache.WithFIFOCapacity(1000),
	gcache.WithFIFOExpiry(time.Minute),
	gcache.WithFIFOClear(time.Minute*10),
)

lfu

gcache.NewLFU(
	gcache.WithLFUCapacity(1000),
	gcache.WithLFUExpiry(time.Minute),
	gcache.WithLFUClear(time.Minute*10),
)

lru

gcache.NewLRU(
	gcache.WithLRUCapacity(1000),
	gcache.WithLRUExpiry(time.Minute), 
	gcache.WithLRUClear(time.Minute*10),
)

lru-k

gcache.NewLRUK(
	gcache.WithLRUKCapacity(1000),
	gcache.WithLRUKExpiry(time.Minute),
	gcache.WithLRUKClear(time.Minute*10),

	gcache.WithLRUK(2),
	gcache.WithLRUKHistoryOnlyKey(false),
	gcache.WithLRUKHistory(gcache.NewLowLRU()),
)

2q

gcache.NewLRUK(
	gcache.WithLRUKCapacity(1000),
	gcache.WithLRUKExpiry(time.Minute),
	gcache.WithLRUKClear(time.Minute*10),

	gcache.WithLRUK(2),
	gcache.WithLRUKHistoryOnlyKey(false),
	gcache.WithLRUKHistory(gcache.NewLowFIFO(
		gcache.WithLowFIFOCapacity(1000),
		gcache.WithLowFIFOExpiry(time.Minute),
	)),
)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

type Cache interface {
	// Add the value to the cache, only when the key does not exist
	Add(key, value interface{}) (added bool)
	// Put key value to cache
	Put(key, value interface{})
	// Get return cache value
	Get(key interface{}) (value interface{}, exists bool)
	// BatchPut pairs to cache
	BatchPut(pair ...interface{})
	// BatchGet return cache values
	BatchGet(key ...interface{}) (vals []Value)
	// Delete key from cache
	Delete(key ...interface{}) (changed int)
	// Len returns the number of cached data
	Len() (count int)
	// Clear all cached data
	Clear()
}

type FIFO

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

func NewFIFO

func NewFIFO(opt ...FIFOOption) (fifo *FIFO)

func (FIFO) Add

func (w FIFO) Add(key, value interface{}) (added bool)

Add the value to the cache, only when the key does not exist

func (FIFO) BatchGet

func (w FIFO) BatchGet(key ...interface{}) (vals []Value)

BatchGet return cache values

func (FIFO) BatchPut

func (w FIFO) BatchPut(pair ...interface{})

BatchPut pairs to cache

func (FIFO) Clear

func (w FIFO) Clear()

Clear all cached data

func (FIFO) Delete

func (w FIFO) Delete(key ...interface{}) (changed int)

Delete key from cache

func (FIFO) Get

func (w FIFO) Get(key interface{}) (value interface{}, exists bool)

Get return cache value, if not exists then return ErrNotExists

func (FIFO) Len

func (w FIFO) Len() (count int)

Len returns the number of cached data

func (FIFO) Put

func (w FIFO) Put(key, value interface{})

Put key value to cache

type FIFOOption

type FIFOOption interface {
	// contains filtered or unexported methods
}

func WithFIFOCapacity

func WithFIFOCapacity(capacity int) FIFOOption

WithFIFOCapacity set the maximum amount of data to be cached

func WithFIFOClear

func WithFIFOClear(duration time.Duration) FIFOOption

WithFIFOClear timer clear expired cache, if <=0 not start timer.

func WithFIFOExpiry

func WithFIFOExpiry(expiry time.Duration) FIFOOption

WithFIFOExpiry if <=0, it will not expire due to time

type LFU

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

func NewLFU

func NewLFU(opt ...LFUOption) (lfu *LFU)

func (LFU) Add

func (w LFU) Add(key, value interface{}) (added bool)

Add the value to the cache, only when the key does not exist

func (LFU) BatchGet

func (w LFU) BatchGet(key ...interface{}) (vals []Value)

BatchGet return cache values

func (LFU) BatchPut

func (w LFU) BatchPut(pair ...interface{})

BatchPut pairs to cache

func (LFU) Clear

func (w LFU) Clear()

Clear all cached data

func (LFU) Delete

func (w LFU) Delete(key ...interface{}) (changed int)

Delete key from cache

func (LFU) Get

func (w LFU) Get(key interface{}) (value interface{}, exists bool)

Get return cache value, if not exists then return ErrNotExists

func (LFU) Len

func (w LFU) Len() (count int)

Len returns the number of cached data

func (LFU) Put

func (w LFU) Put(key, value interface{})

Put key value to cache

type LFUOption

type LFUOption interface {
	// contains filtered or unexported methods
}

func WithLFUCapacity

func WithLFUCapacity(capacity int) LFUOption

WithLFUCapacity set the maximum amount of data to be cached

func WithLFUClear

func WithLFUClear(duration time.Duration) LFUOption

WithLFUClear timer clear expired cache, if <=0 not start timer.

func WithLFUExpiry

func WithLFUExpiry(expiry time.Duration) LFUOption

WithLFUExpiry if <=0, it will not expire due to time

type LRU

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

func NewLRU

func NewLRU(opt ...LRUOption) (lru *LRU)

func (LRU) Add

func (w LRU) Add(key, value interface{}) (added bool)

Add the value to the cache, only when the key does not exist

func (LRU) BatchGet

func (w LRU) BatchGet(key ...interface{}) (vals []Value)

BatchGet return cache values

func (LRU) BatchPut

func (w LRU) BatchPut(pair ...interface{})

BatchPut pairs to cache

func (LRU) Clear

func (w LRU) Clear()

Clear all cached data

func (LRU) Delete

func (w LRU) Delete(key ...interface{}) (changed int)

Delete key from cache

func (LRU) Get

func (w LRU) Get(key interface{}) (value interface{}, exists bool)

Get return cache value, if not exists then return ErrNotExists

func (LRU) Len

func (w LRU) Len() (count int)

Len returns the number of cached data

func (LRU) Put

func (w LRU) Put(key, value interface{})

Put key value to cache

type LRUK

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

func NewLRUK

func NewLRUK(opt ...LRUKOption) (lruk *LRUK)

func (LRUK) Add

func (w LRUK) Add(key, value interface{}) (added bool)

Add the value to the cache, only when the key does not exist

func (LRUK) BatchGet

func (w LRUK) BatchGet(key ...interface{}) (vals []Value)

BatchGet return cache values

func (LRUK) BatchPut

func (w LRUK) BatchPut(pair ...interface{})

BatchPut pairs to cache

func (LRUK) Clear

func (w LRUK) Clear()

Clear all cached data

func (LRUK) Delete

func (w LRUK) Delete(key ...interface{}) (changed int)

Delete key from cache

func (LRUK) Get

func (w LRUK) Get(key interface{}) (value interface{}, exists bool)

Get return cache value, if not exists then return ErrNotExists

func (LRUK) Len

func (w LRUK) Len() (count int)

Len returns the number of cached data

func (LRUK) Put

func (w LRUK) Put(key, value interface{})

Put key value to cache

type LRUKOption

type LRUKOption interface {
	// contains filtered or unexported methods
}

func WithLRUK

func WithLRUK(k int) LRUKOption

WithLRUK set lru-k ,if k == 1 use lru, if k >1 use lru-k, if < 1

func WithLRUKCapacity

func WithLRUKCapacity(capacity int) LRUKOption

WithLRUKCapacity set the maximum amount of data to be cached

func WithLRUKClear

func WithLRUKClear(duration time.Duration) LRUKOption

WithLRUKClear timer clear expired cache, if <=0 not start timer.

func WithLRUKExpiry

func WithLRUKExpiry(expiry time.Duration) LRUKOption

WithLRUKExpiry if <=0, it will not expire due to time

func WithLRUKHistory

func WithLRUKHistory(history LowCache) LRUKOption

WithLRUKHistory if history is nil use lru-1, default is nil.

func WithLRUKHistoryOnlyKey

func WithLRUKHistoryOnlyKey(onlyKey bool) LRUKOption

WithLRUKHistoryOnlyKey if ture history only save key, if false history will save key and value

func WithLRUKLRU

func WithLRUKLRU(lru LowCache) LRUKOption

WithLRUKLRU if lru is nil auto create.

type LRUOption

type LRUOption interface {
	// contains filtered or unexported methods
}

func WithLRUCapacity

func WithLRUCapacity(capacity int) LRUOption

WithLRUCapacity set the maximum amount of data to be cached

func WithLRUClear

func WithLRUClear(duration time.Duration) LRUOption

WithLRUClear timer clear expired cache, if <=0 not start timer.

func WithLRUExpiry

func WithLRUExpiry(expiry time.Duration) LRUOption

WithLRUExpiry if <=0, it will not expire due to time

type LowCache

type LowCache interface {
	// Clear Expired cache
	ClearExpired()
	// Add the value to the cache, only when the key does not exist
	Add(key, value interface{}) (added bool)
	// Put key value to cache
	Put(key, value interface{}) (delkey, delval interface{}, deleted bool)
	// Get return cache value
	Get(key interface{}) (value interface{}, exists bool)
	// Delete key from cache
	Delete(key ...interface{}) (changed int)
	// Len returns the number of cached data
	Len() int
	// Clear all cached data
	Clear()
}

Low-level caching is usually only used when combining multiple caching algorithms

func NewLowFIFO

func NewLowFIFO(opt ...LowFIFOOption) LowCache

NewLowFIFO create a low-level lru, use NewFIFO unless you know exactly what you are doing.

func NewLowLFU

func NewLowLFU(opt ...LowLFUOption) LowCache

NewLowLFU create a low-level lfu, use NewLFU unless you know exactly what you are doing.

func NewLowLRU

func NewLowLRU(opt ...LowLRUOption) LowCache

NewLowLRU create a low-level lru, use NewLRU unless you know exactly what you are doing.

type LowFIFOOption

type LowFIFOOption interface {
	// contains filtered or unexported methods
}

func WithLowFIFOCapacity

func WithLowFIFOCapacity(capacity int) LowFIFOOption

WithLowFIFOCapacity set the maximum amount of data to be cached

func WithLowFIFOExpiry

func WithLowFIFOExpiry(expiry time.Duration) LowFIFOOption

WithLowFIFOExpiry if <=0, it will not expire due to time

type LowLFUOption

type LowLFUOption interface {
	// contains filtered or unexported methods
}

func WithLowLFUCapacity

func WithLowLFUCapacity(capacity int) LowLFUOption

WithLowLFUCapacity set the maximum amount of data to be cached

func WithLowLFUExpiry

func WithLowLFUExpiry(expiry time.Duration) LowLFUOption

WithLowLFUExpiry if <=0, it will not expire due to time

type LowLRUK

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

A low-level implementation of lruk, use LRUK unless you know exactly what you are doing.

func NewLowLRUK

func NewLowLRUK(history, lru LowCache, opt ...LowLRUKOption) *LowLRUK

NewLowLRUK create a low-level lru, use NewLRUK unless you know exactly what you are doing.

func (*LowLRUK) Add

func (l *LowLRUK) Add(key, value interface{}) (added bool)

Add the value to the cache, only when the key does not exist

func (*LowLRUK) Clear

func (l *LowLRUK) Clear()

Clear all cached data

func (*LowLRUK) ClearExpired

func (l *LowLRUK) ClearExpired()

Clear Expired cache

func (*LowLRUK) Delete

func (l *LowLRUK) Delete(key ...interface{}) (changed int)

Delete key from cache

func (*LowLRUK) Get

func (l *LowLRUK) Get(key interface{}) (value interface{}, exists bool)

Get return cache value

func (*LowLRUK) Len

func (l *LowLRUK) Len() int

Len returns the number of cached data

func (*LowLRUK) Put

func (l *LowLRUK) Put(key, value interface{}) (delkey, delval interface{}, deleted bool)

Put key value to cache

type LowLRUKOption

type LowLRUKOption interface {
	// contains filtered or unexported methods
}

func WithLowLRUK

func WithLowLRUK(k int) LowLRUKOption

WithLowLRUK set lru-k ,if k == 1 use lru, if k >1 use lru-k, if < 1

func WithLowLRUKHistoryOnlyKey

func WithLowLRUKHistoryOnlyKey(onlyKey bool) LowLRUKOption

WithLowLRUKHistoryOnlyKey if ture history only save key, if false history will save key and value

type LowLRUOption

type LowLRUOption interface {
	// contains filtered or unexported methods
}

func WithLowLRUCapacity

func WithLowLRUCapacity(capacity int) LowLRUOption

WithLRUCapacity set the maximum amount of data to be cached

func WithLowLRUExpiry

func WithLowLRUExpiry(expiry time.Duration) LowLRUOption

WithLowLRUExpiry if <=0, it will not expire due to time

type Value

type Value struct {
	Exists bool
	Value  interface{}
}

Jump to

Keyboard shortcuts

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