go-access-cache

command module
v0.0.0-...-1dd6a0e Latest Latest
Warning

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

Go to latest
Published: Jul 4, 2020 License: BSD-2-Clause Imports: 2 Imported by: 0

README

Go Access Cache

Go Report Card

A simple in memory cache with a maximum byte size keeping most and recent requested elements in memory while rotating rarely requested elements out.

Example
m := accesscache.NewAccessCache(60)

// Adding first item, cache size will be 28
m.Set("key-a", "first string")
log.Println("Highest Prio Item:", m.GetLastViewedKey(), "Cache Size:", m.GetCacheSize())

// Adding second item, cache size will be 57
m.Set("key-b", "second string")
log.Println("Highest Prio Item:", m.GetLastViewedKey(), "Cache Size:", m.GetCacheSize())

// Requesting first item, it get the highest prio, cache size remains 57
m.Get("key-a")
log.Println("Highest Prio Item:", m.GetLastViewedKey(), "Cache Size:", m.GetCacheSize())

// Adding third item, lowest prio item gets rotated out because cache size > 60
// third item get highest prio, cache size is at 56
m.Set("key-c", "third string")
log.Println("Highest Prio Item:", m.GetLastViewedKey(), "Cache Size:", m.GetCacheSize())

// Requesting first item, wich again gets the highest prio, cache size is at 56
// containing only the first and the second item
m.Get("key-a")
log.Println("Highest Prio Item:", m.GetLastViewedKey(), "Cache Size:", m.GetCacheSize())
Usage

instantiate new access cache with a size of 1024 bytes

m := NewAccessCache(1024)

add value 42 to cache using a unique key "mykey"

m.Set("mykey", 42)

retrieve value from cache by key "mykey"

value, exists = m.Get("mykey")

Documentation

The Go Gopher

There is no documentation for this package.

Directories

Path Synopsis
Package accesscache keeping most requested elements in memory Usage: m := NewAccessCache(<maximum byte size>) Store data: m.Set(<key>, <value>) Read data: <value>, exists = m.Get(<key>)
Package accesscache keeping most requested elements in memory Usage: m := NewAccessCache(<maximum byte size>) Store data: m.Set(<key>, <value>) Read data: <value>, exists = m.Get(<key>)

Jump to

Keyboard shortcuts

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