World Conquest Chronicles

World Conquest Chronicles

go-cache, v1.5

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

Support running garbage collection at the same time as initializing a cache and support iteration over values and their keys in a cache.

Change Log

  • implementation of an in-memory cache:
    • operations:
      • running garbage collection at the same time as initializing a cache (optional);
      • iteration over values and their keys:
        • support stopping of iteration:
          • via a handling result;
          • via a context;
      • iteration over values and their keys with deletion of expired values:
        • support stopping of iteration:
          • via a handling result;
          • via a context;
    • options (with running garbage collection; optional):
      • context for stopping of iteration;
      • implementation of a key-value storage;
      • callback for timing;
      • callback that produces an instance of an implementation of garbage collection;
      • period of running of garbage collection;
  • refactoring:
    • use the hashmap.WithInterruption() function;
    • extract the models package:
      • move the cache.Clock type into it;
      • move the cache.Value structure into it;
  • add the example with running garbage collection at the same time as initializing a cache.

Features

  • implementation of an in-memory cache:
    • operations:
      • running garbage collection at the same time as initializing a cache (optional);
      • 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;
      • iteration over values and their keys:
        • support stopping of iteration:
          • via a handling result;
          • via a context;
      • iteration over values and their keys with deletion of expired values:
        • support stopping of iteration:
          • via a handling result;
          • via a context;
      • setting a key-value pair with a specified time to live:
        • support of key-value pairs without a set time to live (persistent);
      • deletion;
    • options (optional):
      • without running garbage collection:
        • implementation of a key-value storage;
        • callback for timing;
      • with running garbage collection:
        • context for stopping of iteration;
        • implementation of a key-value storage;
        • callback for timing;
        • callback that produces an instance of an implementation of garbage collection;
        • period of running of garbage collection;
  • implementation of garbage collection:
    • independent implementation of garbage collection running:
      • support interruption via a context;
      • support specification of a running period;
    • implementation of total garbage collection (based on a full scan):
      • options (optional):
        • callback for timing;
    • implementation of partial garbage collection (based on expiration in Redis):
      • options (optional):
        • callback for timing;
        • maximum iteration count;
        • minimum percent of expired values.

Examples

cache.NewCacheWithGC():

package main

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

    cache "github.com/thewizardplusplus/go-cache"
    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 hashmap.Key) bool {
    return key == other.(StringKey)
}

const (
    gcPeriod     = time.Millisecond
    exampleDelay = gcPeriod * 100
)

func main() {
    timeZones := cache.NewCacheWithGC(cache.WithGCAndGCPeriod(gcPeriod))
    timeZones.Set(StringKey("EST"), -5*60*60, exampleDelay/2)
    timeZones.Set(StringKey("CST"), -6*60*60, exampleDelay/2)
    timeZones.Set(StringKey("MST"), -7*60*60, exampleDelay/2)

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

    time.Sleep(exampleDelay)

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

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

Repository

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

Content: code.

License: MIT.