All Projects → shaj13 → Libcache

shaj13 / Libcache

Licence: mit
A Lightweight in-memory key:value cache library for Go.

Programming Languages

go
31211 projects - #10 most used programming language
golang
3204 projects

Projects that are alternatives of or similar to Libcache

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 (+1259.87%)
Mutual labels:  cache, caching, in-memory
Hazelcast Cpp Client
Hazelcast IMDG C++ Client
Stars: ✭ 67 (-55.92%)
Mutual labels:  caching, in-memory, library
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 (-43.42%)
Mutual labels:  cache, in-memory
Hazelcast Python Client
Hazelcast IMDG Python Client
Stars: ✭ 92 (-39.47%)
Mutual labels:  caching, in-memory
Cacache Rs
💩💵 but for your 🦀
Stars: ✭ 116 (-23.68%)
Mutual labels:  cache, library
Cashew
A simple and elegant yet powerful HTTP client cache for .NET
Stars: ✭ 70 (-53.95%)
Mutual labels:  cache, caching
Keshi
A better in-memory cache for Node and the browser
Stars: ✭ 75 (-50.66%)
Mutual labels:  cache, in-memory
Gcache
An in-memory cache library for golang. It supports multiple eviction policies: LRU, LFU, ARC
Stars: ✭ 1,787 (+1075.66%)
Mutual labels:  cache, in-memory
Fastcache
Fast thread-safe inmemory cache for big number of entries in Go. Minimizes GC overhead
Stars: ✭ 1,051 (+591.45%)
Mutual labels:  cache, caching
Sequelize Transparent Cache
Simple to use and universal cache layer for Sequelize
Stars: ✭ 137 (-9.87%)
Mutual labels:  cache, caching
Hazelcast Nodejs Client
Hazelcast IMDG Node.js Client
Stars: ✭ 124 (-18.42%)
Mutual labels:  caching, in-memory
Hazelcast Go Client
Hazelcast IMDG Go Client
Stars: ✭ 140 (-7.89%)
Mutual labels:  caching, in-memory
Cucache
Fast PUT/GET/DELETE in-memory key-value store for lookaside caching
Stars: ✭ 63 (-58.55%)
Mutual labels:  caching, in-memory
Minicache
📦 Python memory caching utilities
Stars: ✭ 79 (-48.03%)
Mutual labels:  cache, caching
Ansible Role Memcached
Ansible Role - Memcached
Stars: ✭ 54 (-64.47%)
Mutual labels:  cache, caching
Node Cache
a node internal (in-memory) caching module
Stars: ✭ 1,660 (+992.11%)
Mutual labels:  cache, caching
Synchrotron
Caching layer load balancer.
Stars: ✭ 42 (-72.37%)
Mutual labels:  cache, caching
Easycaching
💥 EasyCaching is an open source caching library that contains basic usages and some advanced usages of caching which can help us to handle caching more easier!
Stars: ✭ 1,047 (+588.82%)
Mutual labels:  cache, caching
Cash
HTTP response caching for Koa. Supports Redis, in-memory store, and more!
Stars: ✭ 122 (-19.74%)
Mutual labels:  cache, caching
Ngx Cache
Cache utility for Angular
Stars: ✭ 144 (-5.26%)
Mutual labels:  cache, caching

PkgGoDev Go Report Card Coverage Status CircleCI

Libcache

A Lightweight in-memory key:value cache library for Go.

Introduction

Caches are tremendously useful in a wide variety of use cases.
you should consider using caches when a value is expensive to compute or retrieve,
and you will need its value on a certain input more than once.
libcache is here to help with that.

Libcache are local to a single run of your application.
They do not store data in files, or on outside servers.

Libcache previously an go-guardian package and designed to be a companion with it.
While both can operate completely independently.

Features

  • Rich caching API
  • Maximum cache size enforcement
  • Default cache TTL (time-to-live) as well as custom TTLs per cache entry
  • Thread safe as well as non-thread safe
  • Event-Driven callbacks (OnExpired,OnEvicted)
  • Dynamic cache creation
  • Multiple cache replacement policies:
    • FIFO (First In, First Out)
    • LIFO (Last In, First Out)
    • LRU (Least Recently Used)
    • MRU (Most Recently Used)
    • LFU (Least Frequently Used)
    • ARC (Adaptive Replacement Cache)

Quickstart

Installing

Using libcache is easy. First, use go get to install the latest version of the library.

go get github.com/shaj13/libcache

Next, include libcache in your application:

import (
    _ "github.com/shaj13/libcache/<desired-replacement-policy>"
    "github.com/shaj13/libcache"
)

Examples

Note: All examples use the LRU cache replacement policy for simplicity, any other cache replacement policy can be applied to them.

Basic

package main 
import (
    "fmt" 

    "github.com/shaj13/libcache"
    _ "github.com/shaj13/libcache/lru"
)

func main() {
    size := 10
    cache := libcache.LRU.NewUnsafe(size)
    for i:= 0 ; i < 10 ; i++ {
        cache.Store(i, i)
    }
    fmt.Println(cache.Load(0)) // nil, false  
    fmt.Println(cache.Load(1)) // 1, true
}

Thread Safe

package main

import (
	"fmt"

	"github.com/shaj13/libcache"
	_ "github.com/shaj13/libcache/lru"
)

func main() {
	done := make(chan struct{})

	f := func(c libcache.Cache) {
		for !c.Contains(5) {
		}
		fmt.Println(c.Load(5)) // 5, true
		done <- struct{}{}
	}

	size := 10
	cache := libcache.LRU.New(size)
	go f(cache)

	for i := 0; i < 10; i++ {
		cache.Store(i, i)
	}

	<-done
}

Unlimited Size

zero capacity means cache has no limit and replacement policy turned off.

package main 
import (
    "fmt" 

    "github.com/shaj13/libcache"
    _ "github.com/shaj13/libcache/lru"
)

func main() {
	cache := libcache.LRU.New(0)
    for i:= 0 ; i < 100000 ; i++ {
        cache.Store(i, i)
    }
	fmt.Println(cache.Load(55555))
}

TTL

package main 
import (
	"fmt"
	"time"

	"github.com/shaj13/libcache"
	_ "github.com/shaj13/libcache/lru"
)

func main() {
	cache := libcache.LRU.New(10)
	cache.SetTTL(time.Second) // default TTL 
	
	for i:= 0 ; i < 10 ; i++ {
        cache.Store(i, i)
	}
	fmt.Println(cache.Expiry(1))

	cache.StoreWithTTL("mykey", "value", time.Hour) // TTL per cache entry 
	fmt.Println(cache.Expiry("mykey"))

}

Events

Timed expiration by default is performed with lazy maintenance during reads operations,
Evict an expired entry immediately can be done using on expired callback.
You may also specify a eviction listener for your cache to perform some operation when an entry is evicted.
Note: Expiration events relying on golang runtime timers heap to call on expired callback when an entry TTL elapsed.

package main 
import (
	"fmt"
	"time"

	"github.com/shaj13/libcache"
	_ "github.com/shaj13/libcache/lru"
)

func main() {
	cache := libcache.LRU.New(10)
	cache.RegisterOnEvicted(func(key, value interface{}) {
		fmt.Printf("Cache Key %v Evicted\n", key)
	})

	cache.RegisterOnExpired(func(key, value interface{}) {
		fmt.Printf("Cache Key %v Expired, Removing it from cache\n", key)
		// use delete directly when your application 
		// guarantee no other goroutine can store items with the same key.
		// Peek also invoke lazy expiry. 
		// 
		// Note this should done only with safe cache.
		cache.Peek(key) 
	})	

	for i:= 0 ; i < 10 ; i++ {
        cache.StoreWithTTL(i, i, time.Microsecond)
	}

	time.Sleep(time.Second)
	fmt.Println(cache.Len())
}

Contributing

  1. Fork it
  2. Download your fork to your PC (git clone https://github.com/your_username/libcache && cd libcache)
  3. Create your feature branch (git checkout -b my-new-feature)
  4. Make changes and add them (git add .)
  5. Commit your changes (git commit -m 'Add some feature')
  6. Push to the branch (git push origin my-new-feature)
  7. Create new pull request

License

Libcache is released under the MIT license. See LICENSE

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