updatecache

package module
v0.0.9 Latest Latest
Warning

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

Go to latest
Published: Dec 10, 2022 License: MIT Imports: 6 Imported by: 0

README

updatecache

golang-ci

Overview

The purpose of updatecache is to update the cache conveniently.

Feature

  • prevent cache breakdown
  • perform regular update of the cache
  • limit the frequency of access to the upstream

Install

go get github.com/vearne/updatecache

Usage

use NewCache to create a local cache.

NewCache(waitUpdate bool, opts ...Option) *LocalCache
waitUpdate

when the Get() is called

(c *LocalCache) Get(key any) any
  • true: If the value corresponding to a key is being updated, wait for the update function to complete and obtain the latest value
  • false: If the value corresponding to a key is being updated, just use previous value.
opts

WithRateLimit create a Limiter to Limit the execution frequency of get value from upstream.

WithRateLimit(r float64, b int) Option
c := cache.NewCache(true, cache.WithRateLimit(200, 1))

Use environment variables to set log level

optional value: debug | info | warn | error

export SIMPLE_LOG_LEVEL=debug

Unit Test

go test .
go test -v .
go test -run TestFirstLoad ./

Example

package main

import (
	cache "github.com/vearne/updatecache"
	"log"
	"sync/atomic"
	"time"
)

func calcuDuration(value any) time.Duration {
	return time.Second
}

func main() {
	key := "aaa"
	c := cache.NewCache(true, cache.WithRateLimit(200, 1))
	var counter uint32 = 2
	if !c.Contains(key) {
		result := c.FirstLoad(key,
			1,
			func() (any, error) {
				time.Sleep(time.Second)
				log.Println("FirstLoad-GetValueFunc")
				return 2, nil
			},
			10*time.Second)
		c.DynamicUpdateLater(key, calcuDuration, func() (any, error) {
			// get value from backend(for example: MySQL, MongoDB or other application)
			log.Println("get value from backend...")
			return atomic.AddUint32(&counter, 1), nil
		})
		log.Printf("result:%v \n", result)
	}
	for i := 0; i < 30; i++ {
		time.Sleep(500 * time.Millisecond)
		log.Println(c.Get(key))
	}
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AtomicBool

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

atomicBool is a wrapper around uint32 for usage as a boolean value with atomic access.

func NewAtomicBool

func NewAtomicBool(flag bool) *AtomicBool

func (*AtomicBool) IsSet

func (ab *AtomicBool) IsSet() bool

IsSet returns whether the current boolean value is true

func (*AtomicBool) IsTrue

func (ab *AtomicBool) IsTrue() bool

func (*AtomicBool) Set

func (ab *AtomicBool) Set(value bool)

Set sets the value of the bool regardless of the previous value

type CalcTimeForNextUpdateFunc added in v0.0.2

type CalcTimeForNextUpdateFunc func(value any) time.Duration

Calculate the waiting time until the next data update

type GetValueFunc

type GetValueFunc func() (value any, err error)

type Item

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

func NewItem

func NewItem(key, value any) *Item

type LocalCache

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

func NewCache

func NewCache(waitUpdate bool, opts ...Option) *LocalCache

func (*LocalCache) Contains added in v0.0.3

func (c *LocalCache) Contains(key any) (ok bool)

func (*LocalCache) DynamicUpdateLater added in v0.0.2

func (c *LocalCache) DynamicUpdateLater(key any, cf CalcTimeForNextUpdateFunc, gf GetValueFunc)

Notice: Item and CalcTimeForNextUpdateFunc, GetValueFunc will only be bound once. 1. Calculate the waiting time until the next data update with CalcTimeForNextUpdateFunc 2. then use GetValueFunc to get value 3. update item

func (*LocalCache) FirstLoad added in v0.0.7

func (c *LocalCache) FirstLoad(key any, defaultValue any, gf GetValueFunc, d time.Duration) any

func (*LocalCache) Get

func (c *LocalCache) Get(key any) any

func (*LocalCache) Remove added in v0.0.2

func (c *LocalCache) Remove(key any)

func (*LocalCache) Set

func (c *LocalCache) Set(key any, value any, d time.Duration)

If d is less than 0, the item does not expire

func (*LocalCache) SetIfNotExist added in v0.0.7

func (c *LocalCache) SetIfNotExist(key any, value any, d time.Duration)

If d is less than 0, the item does not expire

func (*LocalCache) Size added in v0.0.2

func (c *LocalCache) Size() int

func (*LocalCache) StopLaterUpdate added in v0.0.2

func (c *LocalCache) StopLaterUpdate(key any)

func (*LocalCache) UpdateLater added in v0.0.2

func (c *LocalCache) UpdateLater(key any, d time.Duration, getValueFunc GetValueFunc)

1. wait d 2. then use GetValueFunc to get value 3. update item

type Option added in v0.0.9

type Option func(cache *LocalCache)

func WithRateLimit added in v0.0.9

func WithRateLimit(r float64, b int) Option

WithRateLimit create a new Limiter that allows events up to rate r and permits bursts of at most b tokens. rate limiter Limit the execution frequency of GetValueFunc

Directories

Path Synopsis
example
test command
test2 command
test3 command

Jump to

Keyboard shortcuts

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