LRU-based cache package for Go.

cache Go Reference Go Report Card GitHub go.mod Go version GitHub release (latest SemVer) LICENSE

cache is LRU-based cache package written in vanilla Go - with no package dependency. LRU stands for Least Recently Used and it is one of the famous cache replacement algorithm. It replaces newly added data with the least recently used one.

  • Written in Vanilla Go, with no dependencies.
  • Safe for concurrent use.
  • Supports any data type for keys and values.
  • Supports time expiration.

Installation

go get github.com/gozeloglu/cache

Example

Here, there is an example usage of the package.

You can import like this.

import "github.com/gozeloglu/cache"

Then, you need to create a cache variable with New() function. It takes one parameter to specify cache capacity.

Add new data

cache.Add("foo", "bar", 0) // Without expiration time
cache.Add("key", "value", time.Hour * 2) // With expiration time

Get data

val, found := cache.Get("foo")
if !found {
    fmt.Println("key does not exist. val is nil.")
}
fmt.Println(val)

Get all keys

keys := cache.Keys()
for _, k := range keys {
    fmt.Println(k)
}

Contains, Peek and Remove

found := cache.Contains("foo")
if found {
    val, _ := cache.Peek("foo")
    cache.Remove("foo")
}

Remove Oldest

k, v, ok := cache.RemoveOldest()
if ok {
    fmt.Printf("Oldest key-value pair removed: %s-%s", k, v)
}

Resize

cache.Resize(20) // Capacity will be 20

Update value, update expiration date, and replace

newItem, err := cache.UpdateVal("foo", "foobar") // Cache data order is also updated
if err != nil {
    fmt.Printf("New item key and value is %s-%s", newItem.Key, newItem.Val)
}
newItem, err := c.UpdateExpirationDate("foo", time.Hour * 4) // Cache data order is also updated
if err != nil {
    fmt.Printf("%v", newItem.Expiration)
}

err = c.Replace("foo", "fuzz")  // Change value of the key without updating cache access order
if err != nil {
	fmt.Printf(err.Error())
}

Testing

You can run the tests with the following command.

go test .

LICENSE

MIT

Owner
Gökhan Özeloğlu
Graduated from @HacettepeUniversity CS - Software Engineer @yemeksepeti
Gökhan Özeloğlu
Similar Resources

A mem cache base on other populator cache, add following feacture

memcache a mem cache base on other populator cache, add following feacture add lazy load(using expired data, and load it asynchronous) add singlefligh

Oct 28, 2021

Cache - A simple cache implementation

Cache A simple cache implementation LRU Cache An in memory cache implementation

Jan 25, 2022

Gin-cache - Gin cache middleware with golang

Gin-cache - Gin cache middleware with golang

Nov 28, 2022

groupcache is a caching and cache-filling library, intended as a replacement for memcached in many cases.

groupcache Summary groupcache is a distributed caching and cache-filling library, intended as a replacement for a pool of memcached nodes in many case

Dec 31, 2022

☔️ A complete Go cache library that brings you multiple ways of managing your caches

☔️ A complete Go cache library that brings you multiple ways of managing your caches

Gocache Guess what is Gocache? a Go cache library. This is an extendable cache library that brings you a lot of features for caching data. Overview He

Jan 1, 2023

fastcache - fast thread-safe inmemory cache for big number of entries in Go

Fast thread-safe inmemory cache for big number of entries in Go. Minimizes GC overhead

Dec 27, 2022

Efficient cache for gigabytes of data written in Go.

BigCache Fast, concurrent, evicting in-memory cache written to keep big number of entries without impact on performance. BigCache keeps entries on hea

Dec 30, 2022

An in-memory key:value store/cache (similar to Memcached) library for Go, suitable for single-machine applications.

go-cache go-cache is an in-memory key:value store/cache similar to memcached that is suitable for applications running on a single machine. Its major

Dec 29, 2022
lru: the most concise and efficient LRU algorithm based on golang

lru This package of lru is the most concise and efficient LRU algorithm based on golang. Example Quick start: package main import ( "fmt" "github.

Dec 27, 2021
Cache library for golang. It supports expirable Cache, LFU, LRU and ARC.
Cache library for golang. It supports expirable Cache, LFU, LRU and ARC.

GCache Cache library for golang. It supports expirable Cache, LFU, LRU and ARC. Features Supports expirable Cache, LFU, LRU and ARC. Goroutine safe. S

Dec 30, 2022
Least-recently-used-LRU- - Design CacheEvictionPolicy with 2 strategy LRU(Least recently used)

Least-recently-used-LRU- Design CacheEvictionPolicy with 2 strategy LRU(Least re

Jan 4, 2022
LRU-based cache package for Go.

cache is LRU-based cache package written in vanilla Go - with no package dependency. LRU stands for Least Recently Used and it is one of the famous cache replacement algorithm

Sep 8, 2022
Thread-safe LRU cache with permanency and context-based expiration

go-wlru Thread-safe LRU cache with permanency and context-based expiration Operational Complexity (Time) Operation Best Average Worst Access Θ(1) Θ(1)

Mar 7, 2022
LevelDB style LRU cache for Go, support non GC object.

Go语言QQ群: 102319854, 1055927514 凹语言(凹读音“Wa”)(The Wa Programming Language): https://github.com/wa-lang/wa LRU Cache Install go get github.com/chai2010/c

Jul 5, 2020
An in-memory cache library for golang. It supports multiple eviction policies: LRU, LFU, ARC

GCache Cache library for golang. It supports expirable Cache, LFU, LRU and ARC. Features Supports expirable Cache, LFU, LRU and ARC. Goroutine safe. S

May 31, 2021
Solution for Leetcode problem: 146. LRU Cache

Solution for Leetcode problem: 146. LRU Cache link My solution for the above lee

Jan 30, 2022
Package cache is a middleware that provides the cache management for Flamego.

cache Package cache is a middleware that provides the cache management for Flamego. Installation The minimum requirement of Go is 1.16. go get github.

Nov 9, 2022
Dec 28, 2022