hypercache

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jan 3, 2024 License: MIT Imports: 10 Imported by: 0

README

hypercache

License Release Build Status

An in-memory cache backed by redis for cache synchronization between instances.

Install

    $ go get -u github.com/migzzi/hypercache

Usage

package main

import (
	"time"

	"github.com/migzzi/hypercache"
)

type testStruct struct {
    Name string
    Title  string
}

func main() {
	cache := NewSynchronizedCache(createRedisClient(), chanName, 10)

    // Add simple string values with 1 min TTL.
    cache.Set("key1", "val1", 60 * time.Seconds)

    // At this point the cache is populated to all instances of this app.
    val := ""
    if err := cache.Get("key1", &val); err != nil {
        panic("Shit!")
    }

    Assert(val == "val1")

    // Set ttl to 0 so the entry never expires.
    cache.Set("key2", "val2", 0)

    // Can Cache complex types as well (structs, slices, and maps)
    cache.Set("key3", testStruct{ Name: "val3", Title: "Title3" }, 0)
    val3 := testStruct{}
    if err := cache.Get("key3", &val3); err != nil {
        panic("Shit!")
    }

    Assert(val3.Name == "val3")

}

Documentation

Index

Constants

View Source
const (
	HASH_SLOT_COUNT = 16384 // Redis cluster has 16384 hash slots.
)

Variables

View Source
var (
	DEBUG = false

	ErrCacheMiss = errors.New("cache: key is missing")
)

Functions

func NewSynchronizedCache

func NewSynchronizedCache(clients redis.UniversalClient, updateChannelName string, maxEntries int64) *synchronizedCache

Types

type EvictionPolicy

type EvictionPolicy interface {
	// This method is called when a new entry is added to the cache.
	// It should return true if the entry should be added to the cache,
	// or false if it should be discarded.
	ShouldAddEntry(key string, value interface{}) bool
	// This method is called when an entry is accessed from the cache.
	// It should return true if the entry should be kept in the cache,
	// or false if it should be discarded.
	ShouldKeepEntry(key string, value interface{}) bool
	// This method is called when an entry is removed from the cache.
	// It should return true if the entry should be removed from the
	// cache, or false if it should be kept.
	ShouldRemoveEntry(key string, value interface{}) bool
}

Jump to

Keyboard shortcuts

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