knife

package
v1.1.3 Latest Latest
Warning

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

Go to latest
Published: Oct 9, 2022 License: Apache-2.0 Imports: 4 Imported by: 0

README

knife

Provides cache on context.Context.

Install

go get github.com/go-spring/spring-base@v1.1.0-rc2 

Import

import "github.com/go-spring/spring-base/knife"

Example

ctx = knife.New(context.Background())
err = knife.Store(ctx, "a", "b")
v, err := knife.Load(ctx, "a")
v, err = knife.LoadOrStore(ctx, "a", "b")

Documentation

Overview

Package knife provides cache on `context.Context`.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Delete

func Delete(ctx context.Context, key string)

Delete deletes the key and value.

func Load

func Load(ctx context.Context, key string) (interface{}, error)

Load returns the value stored for a key, or nil if no value is present. If knife is uninitialized, the error of knife uninitialized will be returned.

func LoadOrStore

func LoadOrStore(ctx context.Context, key string, val interface{}) (actual interface{}, loaded bool, err error)

LoadOrStore returns the existing value for the key if present. Otherwise, it stores and returns the given value. The loaded result is true if the value was loaded, false if stored. If knife is uninitialized, the error of knife uninitialized will be returned.

func New

func New(ctx context.Context) (_ context.Context, cached bool)

New returns a new context.Context with a *sync.Map bound. If a *sync.Map is already bound, then returns the input context.Context.

func Range

func Range(ctx context.Context, f func(key, value interface{}) bool)

Range calls f sequentially for each key and value.

func Store

func Store(ctx context.Context, key string, val interface{}) error

Store stores the key and value in the context.Context, if the key is already in the context.Context, the error of duplicate key will be returned. If knife is uninitialized, the error of knife uninitialized will be returned.

Types

This section is empty.

Jump to

Keyboard shortcuts

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