syncmap

package
v0.0.0-...-e4e64be Latest Latest
Warning

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

Go to latest
Published: Sep 14, 2026 License: MIT Imports: 9 Imported by: 0

README

RemoteMap

RemoteMap is a Go package that extends sync.Map to synchronize its values from a remote JSON URL. It provides a thread-safe way to keep a local cache of remote JSON data that is periodically refreshed.

Features

  • Extends the standard Go sync.Map with all its methods
  • Periodically fetches and synchronizes data from a remote JSON endpoint
  • Configurable refresh period, timeout, and TLS verification
  • Custom HTTP headers support
  • Error handling callback
  • Data transformation capability
  • Type-specific getters for common types (string, int, int64, float, bool, map)

Installation

go get github.com/user/syncmap

Usage

package main

import (
	"fmt"
	"log"
	"time"

	"github.com/user/syncmap"
)

func main() {
	// Create with default options
	rm := syncmap.NewRemoteMap("https://api.example.com/data", nil)
	
	// Or with custom options
	options := &syncmap.Options{
		RefreshPeriod:   30 * time.Second,
		Timeout:         10 * time.Second,
		IgnoreTLSVerify: false,
		Headers: map[string]string{
			"User-Agent": "MyApp/1.0",
		},
		ErrorHandler: func(err error) {
			log.Printf("Error refreshing map: %v", err)
		},
		TransformFunc: func(data map[string]interface{}) map[string]interface{} {
			// Transform the data if needed
			return data
		},
	}
	rm = syncmap.NewRemoteMap("https://api.example.com/data", options)

	// Start automatic refresh
	rm.Start()
	defer rm.Stop()

	// Access values using type-specific getters
	if name, ok := rm.GetString("name"); ok {
		fmt.Printf("Name: %s\n", name)
	}

	if count, ok := rm.GetInt("count"); ok {
		fmt.Printf("Count: %d\n", count)
	}

	// Or use standard sync.Map methods
	value, ok := rm.Load("key")
	
	// Iterate over all values
	rm.Range(func(key, value interface{}) bool {
		fmt.Printf("%v: %v\n", key, value)
		return true
	})
}

Default Values

  • Default refresh period: 5 minutes
  • Default HTTP timeout: 30 seconds
  • Default TLS verification: enabled (not ignored)

License

MIT

Documentation

Index

Constants

View Source
const DefaultRefreshPeriod = 5 * time.Minute

DefaultRefreshPeriod is the default time between refreshes of the remote data

View Source
const DefaultTimeout = 30 * time.Second

DefaultTimeout is the default timeout for HTTP requests

Variables

This section is empty.

Functions

This section is empty.

Types

type RemoteMap

type RemoteMap struct {
	sync.Map
	// contains filtered or unexported fields
}

RemoteMap extends sync.Map to synchronize with a remote JSON endpoint

func NewRemoteMap

func NewRemoteMap(url string) *RemoteMap

NewRemoteMap creates a new RemoteMap that synchronizes with the provided URL

func (*RemoteMap) GetBool

func (rm *RemoteMap) GetBool(key string) (bool, bool)

GetBool retrieves a bool value from the map

func (*RemoteMap) GetBoolMap

func (rm *RemoteMap) GetBoolMap(key string) (map[string]bool, bool)

GetBoolMap retrieves a map of boolean values from the map

func (*RemoteMap) GetBoolMapWithDefault

func (rm *RemoteMap) GetBoolMapWithDefault(key string, defaultValue map[string]bool) map[string]bool

GetBoolMapWithDefault retrieves a map of boolean values from the map or returns a default value if not found

func (*RemoteMap) GetBoolWithDefault

func (rm *RemoteMap) GetBoolWithDefault(key string, defaultValue bool) bool

GetBoolWithDefault retrieves a bool value from the map or returns a default value if not found

func (*RemoteMap) GetFloat

func (rm *RemoteMap) GetFloat(key string) (float64, bool)

GetFloat retrieves a float64 value from the map

func (*RemoteMap) GetFloatWithDefault

func (rm *RemoteMap) GetFloatWithDefault(key string, defaultValue float64) float64

GetFloatWithDefault retrieves a float64 value from the map or returns a default value if not found

func (*RemoteMap) GetInt

func (rm *RemoteMap) GetInt(key string) (int, bool)

GetInt retrieves an int value from the map

func (*RemoteMap) GetInt64

func (rm *RemoteMap) GetInt64(key string) (int64, bool)

GetInt64 retrieves an int64 value from the map

func (*RemoteMap) GetInt64WithDefault

func (rm *RemoteMap) GetInt64WithDefault(key string, defaultValue int64) int64

GetInt64WithDefault retrieves an int64 value from the map or returns a default value if not found

func (*RemoteMap) GetIntWithDefault

func (rm *RemoteMap) GetIntWithDefault(key string, defaultValue int) int

GetIntWithDefault retrieves an int value from the map or returns a default value if not found

func (*RemoteMap) GetMap

func (rm *RemoteMap) GetMap(key string) (map[string]interface{}, bool)

GetMap retrieves a nested map from the map

func (*RemoteMap) GetMapWithDefault

func (rm *RemoteMap) GetMapWithDefault(key string, defaultValue map[string]interface{}) map[string]interface{}

GetMapWithDefault retrieves a nested map from the map or returns a default value if not found

func (*RemoteMap) GetString

func (rm *RemoteMap) GetString(key string) (string, bool)

GetString retrieves a string value from the map

func (*RemoteMap) GetStringMap

func (rm *RemoteMap) GetStringMap(key string) (map[string]string, bool)

GetStringMap retrieves a map of string values from the map

func (*RemoteMap) GetStringMapWithDefault

func (rm *RemoteMap) GetStringMapWithDefault(key string, defaultValue map[string]string) map[string]string

GetStringMapWithDefault retrieves a map of string values from the map or returns a default value if not found

func (*RemoteMap) GetStringSlice

func (rm *RemoteMap) GetStringSlice(key string) ([]string, bool)

GetStringSlice retrieves a slice of strings from the map

func (*RemoteMap) GetStringSliceMap

func (rm *RemoteMap) GetStringSliceMap(key string) (map[string][]string, bool)

GetStringSliceMap retrieves a map of string slice values from the map

func (*RemoteMap) GetStringSliceMapWithDefault

func (rm *RemoteMap) GetStringSliceMapWithDefault(key string, defaultValue map[string][]string) map[string][]string

GetStringSliceMapWithDefault retrieves a map of string slice values from the map or returns a default value if not found

func (*RemoteMap) GetStringSliceWithDefault

func (rm *RemoteMap) GetStringSliceWithDefault(key string, defaultValue []string) []string

GetStringSliceWithDefault retrieves a slice of strings from the map or returns a default value if not found

func (*RemoteMap) GetStringWithDefault

func (rm *RemoteMap) GetStringWithDefault(key string, defaultValue string) string

GetStringWithDefault retrieves a string value from the map or returns a default value if not found

func (*RemoteMap) Keys

func (rm *RemoteMap) Keys() []string

Keys returns all keys in the map as a slice of strings

func (*RemoteMap) Load

func (rm *RemoteMap) Load(key interface{}) (interface{}, bool)

Load retrieves a value from the map This is an override of sync.Map's Load method to handle type conversions

func (*RemoteMap) LoadOrStore

func (rm *RemoteMap) LoadOrStore(key string, defaultValue interface{}) (interface{}, bool)

LoadOrStore loads the value for a key or stores the default value if it doesn't exist Returns the actual value and a boolean indicating whether the value was loaded

func (*RemoteMap) Refresh

func (rm *RemoteMap) Refresh() error

Refresh immediately updates the map from the remote URL and returns any error

func (*RemoteMap) Start

func (rm *RemoteMap) Start() *RemoteMap

Start begins the periodic refresh of the map from the remote URL and returns the RemoteMap for chaining

func (*RemoteMap) Started

func (rm *RemoteMap) Started() bool

Started returns whether the RemoteMap is currently running

func (*RemoteMap) Stop

func (rm *RemoteMap) Stop() *RemoteMap

Stop halts the periodic refresh of the map and returns the RemoteMap for chaining

func (*RemoteMap) WithDeleteCallback

func (rm *RemoteMap) WithDeleteCallback(callback func([]string)) *RemoteMap

WithDeleteCallback sets a function to be called when keys are deleted from the map

func (*RemoteMap) WithErrorHandler

func (rm *RemoteMap) WithErrorHandler(handler func(error)) *RemoteMap

WithErrorHandler sets a function to be called when an error occurs during refresh

func (*RemoteMap) WithHeader

func (rm *RemoteMap) WithHeader(key, value string) *RemoteMap

WithHeader adds an HTTP header to include in requests

func (*RemoteMap) WithHeaders

func (rm *RemoteMap) WithHeaders(headers map[string]string) *RemoteMap

WithHeaders sets all HTTP headers to include in requests

func (*RemoteMap) WithIgnoreTLSVerify

func (rm *RemoteMap) WithIgnoreTLSVerify(ignore bool) *RemoteMap

WithIgnoreTLSVerify sets whether to disable TLS certificate verification

func (*RemoteMap) WithRefreshCallback

func (rm *RemoteMap) WithRefreshCallback(callback func()) *RemoteMap

WithRefreshCallback sets a function to be called after each refresh operation

func (*RemoteMap) WithRefreshPeriod

func (rm *RemoteMap) WithRefreshPeriod(period time.Duration) *RemoteMap

WithRefreshPeriod sets the time between refreshes of the remote data

func (*RemoteMap) WithTimeout

func (rm *RemoteMap) WithTimeout(timeout time.Duration) *RemoteMap

WithTimeout sets the timeout for HTTP requests

func (*RemoteMap) WithTransformFunc

func (rm *RemoteMap) WithTransformFunc(transform func(map[string]interface{}) map[string]interface{}) *RemoteMap

WithTransformFunc sets a function to transform the fetched data before storing

func (*RemoteMap) WithUpdateCallback

func (rm *RemoteMap) WithUpdateCallback(callback func([]string)) *RemoteMap

WithUpdateCallback sets a function to be called when keys are updated in the map

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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