concurrentcache

package module
v0.0.0-...-5695623 Latest Latest
Warning

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

Go to latest
Published: Feb 16, 2024 License: AGPL-3.0 Imports: 2 Imported by: 1

README

Concurrent Cache - Concurrent-Safe Cache for Go

Concurrent Cache is a Go package providing a concurrent-safe cache with read and write access.

Features

  • Safe concurrent read and write access to a cache.
  • Periodic updates to the cache with user-defined logic.

Installation

go get -u github.com/NIR3X/concurrentcache

Usage

package main

import (
	"bytes"
	"fmt"
	"time"

	"github.com/NIR3X/concurrentcache"
)

func main() {
	// Create a new concurrent cache with a one-second update interval.
	c := concurrentcache.NewConcurrentCache[map[string][]uint8](make(map[string][]uint8), time.Second, func(locker concurrentcache.Locker, cache map[string][]uint8) {
		locker.Lock()
		defer locker.Unlock()
		// Your custom update logic here
	})

	// Close the cache when done to stop the update goroutine.
	defer c.Close()

	// AccessWrite example
	c.AccessWrite(func(cache map[string][]uint8) {
		cache["key"] = []uint8{1, 2, 3}
	})

	// AccessRead example
	c.AccessRead(func(cache map[string][]uint8) {
		if value := cache["key"]; !bytes.Equal(value, []uint8{1, 2, 3}) {
			fmt.Println("Expected [1, 2, 3] but got", value)
		}
	})
}

License

GNU AGPLv3 Image

This program is Free Software: You can use, study share and improve it at your will. Specifically you can redistribute and/or modify it under the terms of the GNU Affero General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ConcurrentCache

type ConcurrentCache[T any] interface {
	Close()
	Access(callback func(locker Locker, cache T))
	AccessRead(callback func(cache T))
	AccessWrite(callback func(cache T))
}

func NewConcurrentCache

func NewConcurrentCache[T any](cache T, updateInterval time.Duration, update func(locker Locker, cache T)) ConcurrentCache[T]

type Locker

type Locker interface {
	RLock()
	TryRLock() bool
	RUnlock()
	Lock()
	TryLock() bool
	Unlock()
	RLocker() sync.Locker
}

Jump to

Keyboard shortcuts

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