All Projects → bluele → Gcache

bluele / Gcache

Licence: mit
An in-memory cache library for golang. It supports multiple eviction policies: LRU, LFU, ARC

Programming Languages

go
31211 projects - #10 most used programming language

Projects that are alternatives of or similar to Gcache

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 (+15.67%)
Mutual labels:  cache, in-memory, lru
Libcache
A Lightweight in-memory key:value cache library for Go.
Stars: ✭ 152 (-91.49%)
Mutual labels:  cache, in-memory
mcache
An in-memory cache library for golang. support: lru,lfu,hash-lru,hash-lfu,arc. 一个高性能本地内存缓存,带有各种内存淘汰算法
Stars: ✭ 311 (-82.6%)
Mutual labels:  lru, lfu
cache
LRU-based cache package for Go.
Stars: ✭ 25 (-98.6%)
Mutual labels:  cache, lru
Cache Chunk Store
In-memory LRU (least-recently-used) cache for abstract-chunk-store compliant stores
Stars: ✭ 24 (-98.66%)
Mutual labels:  cache, in-memory
incache
Powerful key/value in-memory storage or on disk to persist data
Stars: ✭ 16 (-99.1%)
Mutual labels:  cache, in-memory
gocache
High performance and lightweight in-memory cache library with LRU and FIFO support as well as memory-usage-based-eviction
Stars: ✭ 15 (-99.16%)
Mutual labels:  lru, in-memory
Keshi
A better in-memory cache for Node and the browser
Stars: ✭ 75 (-95.8%)
Mutual labels:  cache, in-memory
disk-lru-cache
💾 Disk LRU cache with persisted journal
Stars: ✭ 21 (-98.82%)
Mutual labels:  cache, lru
transitory
In-memory cache with high hit rates via LFU eviction for Node and browsers. Supports time-based expiration, automatic loading and metrics.
Stars: ✭ 24 (-98.66%)
Mutual labels:  cache, lfu
Go Cache
Go in-memory cache library
Stars: ✭ 15 (-99.16%)
Mutual labels:  cache, in-memory
Ignite Book Code Samples
All code samples, scripts and more in-depth examples for the book high performance in-memory computing with Apache Ignite. Please use the repository "the-apache-ignite-book" for Ignite version 2.6 or above.
Stars: ✭ 86 (-95.19%)
Mutual labels:  cache, in-memory
Api Restful Con Laravel Guia Definitiva
Repositorio para el código base del curso "API RESTful con Laravel - Guía Definitiva"
Stars: ✭ 95 (-94.68%)
Mutual labels:  cache
Fastapi cache
FastAPI simple cache
Stars: ✭ 96 (-94.63%)
Mutual labels:  cache
Drone Volume Cache
Drone plugin for caching to a locally mounted volume
Stars: ✭ 93 (-94.8%)
Mutual labels:  cache
Hazelcast Python Client
Hazelcast IMDG Python Client
Stars: ✭ 92 (-94.85%)
Mutual labels:  in-memory
Wikipediap2p
WikipediaP2P.org Chrome Extension
Stars: ✭ 105 (-94.12%)
Mutual labels:  cache
Memoize
A method caching macro for elixir using CAS on ETS.
Stars: ✭ 100 (-94.4%)
Mutual labels:  cache
Neardb
Simple document db made for infinitely scalable globally distributed reads.
Stars: ✭ 92 (-94.85%)
Mutual labels:  cache
Next Offline
make your Next.js application work offline using service workers via Google's workbox
Stars: ✭ 1,306 (-26.92%)
Mutual labels:  cache

GCache

Test GoDoc

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

Features

  • Supports expirable Cache, LFU, LRU and ARC.

  • Goroutine safe.

  • Supports event handlers which evict, purge, and add entry. (Optional)

  • Automatically load cache if it doesn't exists. (Optional)

Install

$ go get github.com/bluele/gcache

Example

Manually set a key-value pair.

package main

import (
  "github.com/bluele/gcache"
  "fmt"
)

func main() {
  gc := gcache.New(20).
    LRU().
    Build()
  gc.Set("key", "ok")
  value, err := gc.Get("key")
  if err != nil {
    panic(err)
  }
  fmt.Println("Get:", value)
}
Get: ok

Manually set a key-value pair, with an expiration time.

package main

import (
  "github.com/bluele/gcache"
  "fmt"
  "time"
)

func main() {
  gc := gcache.New(20).
    LRU().
    Build()
  gc.SetWithExpire("key", "ok", time.Second*10)
  value, _ := gc.Get("key")
  fmt.Println("Get:", value)

  // Wait for value to expire
  time.Sleep(time.Second*10)

  value, err = gc.Get("key")
  if err != nil {
    panic(err)
  }
  fmt.Println("Get:", value)
}
Get: ok
// 10 seconds later, new attempt:
panic: ErrKeyNotFound

Automatically load value

package main

import (
  "github.com/bluele/gcache"
  "fmt"
)

func main() {
  gc := gcache.New(20).
    LRU().
    LoaderFunc(func(key interface{}) (interface{}, error) {
      return "ok", nil
    }).
    Build()
  value, err := gc.Get("key")
  if err != nil {
    panic(err)
  }
  fmt.Println("Get:", value)
}
Get: ok

Automatically load value with expiration

package main

import (
  "fmt"
  "time"

  "github.com/bluele/gcache"
)

func main() {
  var evictCounter, loaderCounter, purgeCounter int
  gc := gcache.New(20).
    LRU().
    LoaderExpireFunc(func(key interface{}) (interface{}, *time.Duration, error) {
      loaderCounter++
      expire := 1 * time.Second
      return "ok", &expire, nil
    }).
    EvictedFunc(func(key, value interface{}) {
      evictCounter++
      fmt.Println("evicted key:", key)
    }).
    PurgeVisitorFunc(func(key, value interface{}) {
      purgeCounter++
      fmt.Println("purged key:", key)
    }).
    Build()
  value, err := gc.Get("key")
  if err != nil {
    panic(err)
  }
  fmt.Println("Get:", value)
  time.Sleep(1 * time.Second)
  value, err = gc.Get("key")
  if err != nil {
    panic(err)
  }
  fmt.Println("Get:", value)
  gc.Purge()
  if loaderCounter != evictCounter+purgeCounter {
    panic("bad")
  }
}
Get: ok
evicted key: key
Get: ok
purged key: key

Cache Algorithm

  • Least-Frequently Used (LFU)

Discards the least frequently used items first.

func main() {
  // size: 10
  gc := gcache.New(10).
    LFU().
    Build()
  gc.Set("key", "value")
}
  • Least Recently Used (LRU)

Discards the least recently used items first.

func main() {
  // size: 10
  gc := gcache.New(10).
    LRU().
    Build()
  gc.Set("key", "value")
}
  • Adaptive Replacement Cache (ARC)

Constantly balances between LRU and LFU, to improve the combined result.

detail: http://en.wikipedia.org/wiki/Adaptive_replacement_cache

func main() {
  // size: 10
  gc := gcache.New(10).
    ARC().
    Build()
  gc.Set("key", "value")
}
  • SimpleCache (Default)

SimpleCache has no clear priority for evict cache. It depends on key-value map order.

func main() {
  // size: 10
  gc := gcache.New(10).Build()
  gc.Set("key", "value")
  v, err := gc.Get("key")
  if err != nil {
    panic(err)
  }
}

Loading Cache

If specified LoaderFunc, values are automatically loaded by the cache, and are stored in the cache until either evicted or manually invalidated.

func main() {
  gc := gcache.New(10).
    LRU().
    LoaderFunc(func(key interface{}) (interface{}, error) {
      return "value", nil
    }).
    Build()
  v, _ := gc.Get("key")
  // output: "value"
  fmt.Println(v)
}

GCache coordinates cache fills such that only one load in one process of an entire replicated set of processes populates the cache, then multiplexes the loaded value to all callers.

Expirable cache

func main() {
  // LRU cache, size: 10, expiration: after a hour
  gc := gcache.New(10).
    LRU().
    Expiration(time.Hour).
    Build()
}

Event handlers

Evicted handler

Event handler for evict the entry.

func main() {
  gc := gcache.New(2).
    EvictedFunc(func(key, value interface{}) {
      fmt.Println("evicted key:", key)
    }).
    Build()
  for i := 0; i < 3; i++ {
    gc.Set(i, i*i)
  }
}
evicted key: 0

Added handler

Event handler for add the entry.

func main() {
  gc := gcache.New(2).
    AddedFunc(func(key, value interface{}) {
      fmt.Println("added key:", key)
    }).
    Build()
  for i := 0; i < 3; i++ {
    gc.Set(i, i*i)
  }
}
added key: 0
added key: 1
added key: 2

Author

Jun Kimura

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].