distributed_cache

package module
v0.0.0-...-3112e50 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2024 License: MIT Imports: 11 Imported by: 0

README

distributed-cache

GO Distributed Cache

Description

This is a distributed cache system that to distribute the data across multiple nodes. The system is designed to be fault-tolerant and can handle node failures. The system is implemented in Go and uses UPD Communication

Features

LRU Cache

LRU Cache With TTL

Example

package main

import (
	distributed_cache "github.com/diogenes-moreira/distributed-cache"
	"time"
)

	


func main() {
	// Create a new cache with the name "cache" "255.255.255.255" is the mask to broadcast the cache sync messages
	// and the address UDP Port ":12345".
	// Name is used to identify the cache and address is used to send messages to the cache.
	// start the listener in a goroutine. is used to listen for incoming messages.
	cache := distributed_cache.NewCache("cache","255.255.255.255", ":12345")
	cache.Set("key", "value")
	value := cache.Get("key")
	if value != nil {
		println(value.(string))
	}
	
	//In addition hoy can set a function to be called when the key is not found in the cache
	cache.Filler = func(key string) (interface{}, error) {
        return "value",nil
    }
	
	// LRU Cache Extend the Cache struct and limit the number of entries to 10.
	lruCache := distributed_cache.NewLRUCache("lru","255.255.255.255", ":12345", 10)
	lruCache.Set("key", "value")
	value = lruCache.Get("key")
	
	// LRU Cache with TTL Extend the lruCache struct and add a TTL of 10 seconds.
	lruCacheWithTTL := distributed_cache.NewLRUCacheWithTTL("lru","255.255.255.255", ":12345", 10, time.Second*10)
	lruCache.Set("key", "value")
	value = lruCache.Get("key")
	
	//If you need to stop the cache listener you can call the Stop method
	cache.StopListener()
	
	//If you need add a Hook to be called when a key is removed from the cache
	//the hook run in a goroutine
	cache.RemoveHook=func(key string, value interface{}) {
        println("Key removed: ", key)
    }
	
}

Documentation

Overview

Package distributed_cache contains all necessary files to add a distributed cache to your application. The communication between nodes using UDP.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

type Cache struct {
	Name         string             // Name of the cache
	Address      string             // Port over which the cache will communicate
	Broadcast    string             // Broadcast addresses 255.255.255.255 for IPV4 all network
	StopListener context.CancelFunc // Cancel function
	// to stop the listener
	Filler func(string) (interface{}, error) // Function to fill the cache
	// when the key is not found
	RemoveHook func(string, interface{}) // Function to remove the key from the cache
	// contains filtered or unexported fields
}

Cache is a simple cache interface, to create a cache you must Use NewCache Method

func NewCache

func NewCache(name, broadcast, address string) *Cache

NewCache creates a new Cache with the given name and address It also starts a listener to receive messages from other nodes

func (*Cache) Clean

func (c *Cache) Clean()

Clean deletes all values from the cache and sends the sendClean message to the other nodes

func (*Cache) Delete

func (c *Cache) Delete(key string)

Delete deletes a value from the cache and sends the delete message to the other nodes

func (*Cache) Get

func (c *Cache) Get(key string) interface{}

Get gets a value from the cache

func (*Cache) Set

func (c *Cache) Set(key string, value interface{})

Set sets a value in the cache and sends it to the other nodes

type LRUCache

type LRUCache struct {
	Cache
	MaxEntries int
	// contains filtered or unexported fields
}

LRUCache is a cache that uses the Least Recently Used algorithm to evict entries

func NewLRUCache

func NewLRUCache(name, broadcast, address string, maxEntries int) *LRUCache

func (*LRUCache) Clean

func (c *LRUCache) Clean()

Clean deletes all values from the cache and sends the sendClean message to the other nodes

func (*LRUCache) Delete

func (c *LRUCache) Delete(key string)

Delete deletes a value from the cache and sends the delete message to the other nodes

func (*LRUCache) Set

func (c *LRUCache) Set(key string, value interface{})

type LRUCacheWithTTL

type LRUCacheWithTTL struct {
	LRUCache
	TTL time.Duration
	// contains filtered or unexported fields
}

LRUCacheWithTTL is a cache that uses the Least Recently Used (LRU) algorithm to evict entries when the cache is full. It also has a time-to-live (TTL) for each entry if an entry is not accessed, it will be deleted after the TTL expires. The Evict method is called periodically (TTL Period) to delete entries that have expired.

func NewLRUCacheWithTTL

func NewLRUCacheWithTTL(name, broadcast, address string, maxEntries int, ttl time.Duration) *LRUCacheWithTTL

NewLRUCacheWithTTL creates a new LRUCacheWithTTL with the given name, address, maxEntries and TTL. It also starts a listener to receive messages from other nodes and starts a for cache eviction. name is the name of the cache address is the address of the cache maxEntries is the maximum number of entries that the cache can have ttl is the time-to-live for each entry in the cache

func (*LRUCacheWithTTL) Clean

func (c *LRUCacheWithTTL) Clean()

Clean deletes all values from the cache and sends the sendClean message to the other nodes

func (*LRUCacheWithTTL) Delete

func (c *LRUCacheWithTTL) Delete(key string)

Delete deletes a value from the cache and sends the delete message to the other nodes

func (*LRUCacheWithTTL) Get

func (c *LRUCacheWithTTL) Get(key string) interface{}

Get gets a value from the cache

func (*LRUCacheWithTTL) Set

func (c *LRUCacheWithTTL) Set(key string, value interface{})

Jump to

Keyboard shortcuts

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