keybase

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

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

Go to latest
Published: Feb 3, 2025 License: MIT Imports: 8 Imported by: 0

README

Keybase

GoDoc codecov Go Report Card

Keybase is a key counting database with expiring keys and optional persistence.

Usage

Keybase is designed to work out of the box with minimal configuration. To use keybase, call the Open() function:

kb, err := keybase.Open(keybase.WithStorage("/tmp/keybase.db"), keybase.WithTTL(time.Minute))

This will initialize a database at /tmp/keybase.db with a key timeout of one minute. Once keybase is open, it is ready to store and maintain keys. Each key is assigned to a namespace and can be inserted using the Put function:

_ = kb.Put(context.Background(), "namespace", "key")

Once the key is stored, various functions can be used to query key and namespace information, such as GetKeys(), which will return a slice of strings representing all keys in a given namespace:

active := true
unique := true
keys, err := kb.GetKeys(context.Background(), "namespace", active, unique)

By setting active and unique to true, the slice will include each active key once. Otherwise, the string make contain multiple copies of the same key, as well as stale keys, if it has been submitted multiple times and queried within the TTL duration. Over time, as the keys become stale, they can be removed using the PruneEntries function:

_ = kb.PruneEntries(context.Background())

This will remove the stale keys and reduce the amount of storage required by memory or filesystem. When the keybase is no longer needed, it needs to be disconnected using the Close() function:

kb.Close()

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Keybase

type Keybase struct {
	// contains filtered or unexported fields
}

Keybase concurrent key storage with timeouts and optional persistence

func Open

func Open(ctx context.Context, opts ...Option) (*Keybase, error)

Open opens new or existing keybase

func (*Keybase) ClearEntries

func (k *Keybase) ClearEntries(ctx context.Context) error

ClearEntries removes all entries.

func (*Keybase) Close

func (k *Keybase) Close()

Close closes keybase

func (*Keybase) CountEntries

func (k *Keybase) CountEntries(ctx context.Context, active, unique bool) (int, error)

CountEntries counts all keys in all namespaces

func (*Keybase) CountKey

func (k *Keybase) CountKey(ctx context.Context, namespace, key string, active bool) (int, error)

CountKey count active frequency of a specific key from a given namespace

func (*Keybase) CountKeys

func (k *Keybase) CountKeys(ctx context.Context, namespace string, active, unique bool) (int, error)

CountKeys counts the active keys from a given namespace

func (*Keybase) CountNamespaces

func (k *Keybase) CountNamespaces(ctx context.Context, active bool) (int, error)

CountNamespaces counts active namespaces

func (*Keybase) GetKeys

func (k *Keybase) GetKeys(ctx context.Context, namespace string, active, unique bool) ([]string, error)

GetKeys collects a list of active keys from a given namespace

func (*Keybase) GetNamespaces

func (k *Keybase) GetNamespaces(ctx context.Context, active bool) ([]string, error)

GetNamespace collects a list of active namespaces

func (*Keybase) MatchKey

func (k *Keybase) MatchKey(ctx context.Context, namespace, pattern string, active, unique bool) ([]string, error)

MatchKey collect list of keys from a given namespace that match a specific pattern

func (*Keybase) PruneEntries

func (k *Keybase) PruneEntries(ctx context.Context) error

PruneEntries removes stale entries.

func (*Keybase) Put

func (k *Keybase) Put(ctx context.Context, namespace, key string) error

Put inserts new value

type Option

type Option struct {
	// contains filtered or unexported fields
}

Option opaque configuration parameter

func WithStorage

func WithStorage(path string) Option

Set filepath for persistent keybase storage

func WithTTL

func WithTTL(ttl time.Duration) Option

Set TTL for keys

Jump to

Keyboard shortcuts

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