All Projects → gozeloglu → cache

gozeloglu / cache

Licence: MIT License
LRU-based cache package for Go.

Programming Languages

go
31211 projects - #10 most used programming language

Projects that are alternatives of or similar to cache

disk-lru-cache
💾 Disk LRU cache with persisted journal
Stars: ✭ 21 (-16%)
Mutual labels:  cache, lru, lru-cache
Olric
Distributed cache and in-memory key/value data store. It can be used both as an embedded Go library and as a language-independent service.
Stars: ✭ 2,067 (+8168%)
Mutual labels:  cache, lru, lru-cache
golib
Open version of common golang libraries useful to many projects.
Stars: ✭ 47 (+88%)
Mutual labels:  lru, lru-cache
elara
Elara DB is an easy to use, lightweight key-value database that can also be used as a fast in-memory cache. Manipulate data structures in-memory, encrypt database files and export data. 🎯
Stars: ✭ 93 (+272%)
Mutual labels:  cache, lru-cache
Gcache
An in-memory cache library for golang. It supports multiple eviction policies: LRU, LFU, ARC
Stars: ✭ 1,787 (+7048%)
Mutual labels:  cache, lru
lru
LRU cache using go generics
Stars: ✭ 108 (+332%)
Mutual labels:  lru, lru-cache
gotcha
[Not Safe For Production] gotcha: inmemory-cache in Go (Golang) with customizable algorithm
Stars: ✭ 13 (-48%)
Mutual labels:  cache, lru-cache
gocache
High performance and lightweight in-memory cache library with LRU and FIFO support as well as memory-usage-based-eviction
Stars: ✭ 15 (-40%)
Mutual labels:  lru, lru-cache
LruClockCache
A low-latency LRU approximation cache in C++ using CLOCK second-chance algorithm. Multi level cache too. Up to 2.5 billion lookups per second.
Stars: ✭ 35 (+40%)
Mutual labels:  lru, lru-cache
go-cache-benchmark
Cache benchmark for Golang
Stars: ✭ 47 (+88%)
Mutual labels:  lru, lru-cache
memo-async-lru
Memoize Node.js style callback-last functions, using an in-memory LRU store
Stars: ✭ 17 (-32%)
Mutual labels:  lru, lru-cache
hyperlru
Tiny & Fast LRU Implementation as possible.
Stars: ✭ 34 (+36%)
Mutual labels:  lru, lru-cache
tacky
Primitive Object Memoization for Ruby
Stars: ✭ 14 (-44%)
Mutual labels:  cache
infinitree
Scalable and encrypted embedded database with 3-tier caching
Stars: ✭ 80 (+220%)
Mutual labels:  cache
bazel-cache
Minimal cloud oriented Bazel gRPC cache
Stars: ✭ 33 (+32%)
Mutual labels:  cache
tiny-cache
Cache WordPress post content, template part, translations and nav menu output in persistent object cache
Stars: ✭ 26 (+4%)
Mutual labels:  cache
regex-cache
Memoize the results of a call to the RegExp constructor, avoiding repetitious runtime compilation of the same string and options, resulting in dramatic speed improvements.
Stars: ✭ 39 (+56%)
Mutual labels:  cache
cache-command
Manages object and transient caches.
Stars: ✭ 12 (-52%)
Mutual labels:  cache
memoize
Caching library for asynchronous Python applications.
Stars: ✭ 53 (+112%)
Mutual labels:  cache
growl
gorm, redis and local cache layer
Stars: ✭ 19 (-24%)
Mutual labels:  cache

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 .

Code Coverage

You can get the code coverage information with the following command:

go test -cover

If you want to generate a graphical coverage report, you can run the following command:

go tool cover -html=coverage.out

A browser tab will be opened and you will be able to see the graphical report. It shows not tracked, not covered, and covered line on the source code.

LICENSE

MIT

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].