World Conquest Chronicles

World Conquest Chronicles

go-cache, v1.1

The library that implements an in-memory cache with garbage collection in two modes: total (based on a full scan) and partial (based on expiration in Redis).

Implementation of total garbage collection (based on a full scan).

Change Log

  • implementation of an in-memory cache:
    • make public the expired value model;
  • implementation of garbage collection:
    • implementation of total garbage collection (based on a full scan):
      • support interruption via a context;
      • support specification of a running period.

Features

  • implementation of an in-memory cache:
    • operations:
      • getting a value by a key:
        • signaling a reason for the absence of a key - missed or expired;
      • getting a value by a key with deletion of expired values:
        • signaling a reason for the absence of a key - missed or expired;
      • setting a key-value pair with a specified time to live:
        • support of key-value pairs without a set time to live (persistent);
      • deletion;
  • implementation of garbage collection:
    • implementation of total garbage collection (based on a full scan):
      • support interruption via a context;
      • support specification of a running period.

Examples

cache.NewCache():

package main

import (
    "context"
    "fmt"
    "hash/fnv"
    "io"
    "time"

    cache "github.com/thewizardplusplus/go-cache"
    "github.com/thewizardplusplus/go-cache/gc"
    hashmap "github.com/thewizardplusplus/go-hashmap"
)

type StringKey string

func (key StringKey) Hash() int {
    hash := fnv.New32()
    io.WriteString(hash, string(key))

    return int(hash.Sum32())
}

func (key StringKey) Equals(other interface{}) bool {
    return key == other.(StringKey)
}

func main() {
    storage := hashmap.NewConcurrentHashMap()
    gc := gc.NewTotalGC(time.Millisecond, storage, time.Now)
    go gc.Run(context.Background())

    timeZones := cache.NewCache(storage, time.Now)
    timeZones.Set(StringKey("EST"), -5*60*60, 100*time.Millisecond)
    timeZones.Set(StringKey("CST"), -6*60*60, 100*time.Millisecond)
    timeZones.Set(StringKey("MST"), -7*60*60, 100*time.Millisecond)

    estOffset, err := timeZones.Get(StringKey("EST"))
    fmt.Println(estOffset, err)

    time.Sleep(200 * time.Millisecond)

    estOffset, err = timeZones.Get(StringKey("EST"))
    fmt.Println(estOffset, err)

    // Output:
    // -18000 <nil>
    // <nil> key missed
}

Benchmarks

With the total GC:

BenchmarkCacheGetting_withTotalGC/Get-8              5000000          3567 ns/op         534 B/op         40 allocs/op
BenchmarkCacheGetting_withTotalGC/GetWithGC-8        2000000          9114 ns/op        1026 B/op         69 allocs/op

Repository

Link: https://github.com/thewizardplusplus/go-cache/tree/v1.1.

Content: code.

License: MIT.