memorycache

package module
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Jun 28, 2023 License: MIT Imports: 6 Imported by: 0

README

go-memorycache

en ru

In-memory cache with expiration and eviction.

Description

A thread-safe in-memory cache implementation.

Memory is an expensive resource, so an implementation of clearing obsolete records is provided, as well as an implementation of expelling records when the specified limit is reached.
Set CleanupInterval to enable obsolete records clearing.
Set LimitEntries to enable records expelling when count limit is reached.
Obsolete records are evicted first, then record are expelled by durability and FIFO method.

Special thanks to ks-troyan.

Install

go get github.com/a-projects/go-memorycache@latest

Usage


import (
	"fmt"
	"time"

	"github.com/a-projects/go-memorycache"
)

func main() {
	// create cache instance
	cache := memorycache.New(memorycache.MemoryCacheOptions{
		// periodic records clearing, every 15 min
		// if not set, then it does not start
		CleanupInterval: time.Minute * 15,
		// cache entries limit, recods
		// if not set, then unlimited
		LimitEntries: 65_536,
		// file used to restore data from disc when app is restarted
		// if not set, it does not restore
		FileName: "cache.bin",
	})

	// retrieve an item from cache by key "foo"
	res, ok := cache.Get("foo")

	// if item with given key are not found
	// reasons:
	//   - entry was never stored in cache
	//   - entry was stored, but expired
	//   - entry was stored, but was removed from cache
	//   - entry was stored, but was expelled
	if !ok {
		// retrieving item from external sources
		res = "bar"

		// add entry to cache as a key value pair
		cache.Set("foo", res, memorycache.MemoryCacheEntryOptions{
			// set lifetime
			// if not set, then not stored
			Lifetime: time.Minute * 5,
			// set expellence resistence
			// if not set, then Normal
			Durability: memorycache.Normal,
		})
	}

	// cast result and print to console
	fmt.Printf(res.(string))

	// close cache instance, all chache data will be writen to FileName file
	cache.Close()
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type MemoryCache

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

MemoryCache cache

func New

func New(options MemoryCacheOptions) (memorycache *MemoryCache)

New construct and load cache data from file if set FileName

func (*MemoryCache) Close

func (m *MemoryCache) Close()

Close destruct and save cache data in file if set FileName

func (*MemoryCache) Count

func (m *MemoryCache) Count() (count int)

Count provide count entries in cache

func (*MemoryCache) Del

func (m *MemoryCache) Del(key string)

Del delete entry by key

func (*MemoryCache) Get

func (m *MemoryCache) Get(key string) (value interface{}, ok bool)

Get provide entry value by key

func (*MemoryCache) Reset

func (m *MemoryCache) Reset()

Reset remove all entries from cache

func (*MemoryCache) Set

func (m *MemoryCache) Set(key string, value interface{}, options MemoryCacheEntryOptions)

Set add entry or update value by key

type MemoryCacheEntryDurability

type MemoryCacheEntryDurability int

MemoryCacheEntryDurability resistance to evicting entries from cache when limit is reached

const (
	Weak   MemoryCacheEntryDurability = -1 // lowest resistance, evicted first
	Normal MemoryCacheEntryDurability = 0  // evicted if there are no records with priority Weak
	Strong MemoryCacheEntryDurability = 1  // evicted if there are no records with priority Normal
)

type MemoryCacheEntryOptions

type MemoryCacheEntryOptions struct {
	// Lifetime entry lifetime, if not set, then 0
	Lifetime time.Duration
	// Durability eviction resistance, if not set, then Normal
	Durability MemoryCacheEntryDurability
}

MemoryCacheEntryOptions

type MemoryCacheOptions

type MemoryCacheOptions struct {
	// CleanupInterval interval for clearing cache from obsolete entries, if not set, then it does not start
	CleanupInterval time.Duration

	// LimitEntries limit of entries in cache, upon reaching which eviction begins, if not set, then unlimited
	LimitEntries int

	// FileName file to restore cache when application is restarted, if not set, it does not restore
	FileName string
}

MemoryCacheOptions cache options

Jump to

Keyboard shortcuts

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