memcache

package module
v0.0.0-...-8d3caf8 Latest Latest
Warning

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

Go to latest
Published: Jul 3, 2023 License: MIT Imports: 4 Imported by: 0

README

memcache 📚

Go Report Card codecov FOSSA Status

DeepSource

DHT cache, Built with consitent hashing & LRU/LFU cache

Example

Memcache
package main

import (
	"fmt"

	"github.com/escalopa/memcache"
)

func main() {
	nodes := 1_000
	capacity := 1_000_000
	mc := memcache.New(nodes, capacity, memcache.NewLRU) // Or use `memcache.NewLFU`

	// If no nodes are needed, use:
	// mc := meme.NewLRU(capacity)
	// Or
	// mc := meme.NewLFU(capacity)

	var value interface{}
	var ok bool

	value, ok = mc.Get("foo")
	fmt.Println(value, ok)
	// Output: <nil> false

	mc.Set("foo", "bar", 0)
	value, ok = mc.Get("foo")
	fmt.Println(value, ok)
	// Output: bar true

	mc.Delete("foo")
	value, ok = mc.Get("foo")
	fmt.Println(value, ok)
	// Output: <nil> false

	mc.Set("foo", "bar", 0)
	value, ok = mc.Get("foo")
	fmt.Println(value, ok)
	// Output: bar true
}

About

DHT

Distributed Hash Table (DHT) is a distributed system that provides a lookup service similar to a hash table. It assigns keys to nodes in the network using a hash function. This allows the nodes to efficiently retrieve the value associated with a given key.

Notice: This project implements DHT on a single node. It is not distributed.

LRU

Least Recently Used (LRU) is a common caching strategy. It defines that the least recently used items are discarded first.

LFU

Least Frequently Used (LFU) is a caching strategy whereby the least frequently used items are discarded first.

References

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New(nodes int, capacity int, newCacheImpl func(int) Cache) *memCache

New returns a new instance of memeCache nodes: Number of sub caches capacity: Max number of key-value pairs in each sub cache before eviction newCacheImpl: Factory function to create a new instance of Cache, e.g. `NewLFU` or `NewLRU`

Types

type Cache

type Cache interface {
	Get(key string) (value interface{}, ok bool)
	Set(key string, value interface{}, ttl time.Duration)
	Delete(key string)
}

func NewLFU

func NewLFU(capacity int) Cache

NewLFU returns a new instance of lfuCache with the given capacity

func NewLRU

func NewLRU(capacity int) Cache

NewLRU returns a new instance of lruCache with the given capacity

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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