valkey

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Feb 25, 2026 License: AGPL-3.0 Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func LoadAllScripts

func LoadAllScripts(ctx context.Context, c *Client) error

LoadAllScripts preloads all built-in scripts on the server This is optional but improves performance by avoiding EVAL fallback

Types

type Client

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

Client wraps the Valkey client with application-specific methods

func New

func New(cfg *config.Config) (*Client, error)

New creates a new Valkey client

func (*Client) Close

func (c *Client) Close()

Close closes the client connection

func (*Client) DBSize

func (c *Client) DBSize(ctx context.Context) (int64, error)

DBSize returns the number of keys in the current database

func (*Client) Del

func (c *Client) Del(ctx context.Context, keys ...string) (int64, error)

Del deletes keys

func (*Client) Expire

func (c *Client) Expire(ctx context.Context, key string, ttl time.Duration) (bool, error)

Expire sets a TTL on a key

func (*Client) FlushDB

func (c *Client) FlushDB(ctx context.Context) error

FlushDB deletes all keys in the current database

func (*Client) GeoAdd

func (c *Client) GeoAdd(ctx context.Context, key string, longitude, latitude float64, member string) error

GeoAdd adds a member with coordinates to a geospatial index

func (*Client) GeoPos

func (c *Client) GeoPos(ctx context.Context, key string, members ...string) ([]*GeoPosition, error)

GeoPos returns the coordinates of members in a geospatial index Returns nil for members that don't exist

func (*Client) Get

func (c *Client) Get(ctx context.Context, key string) (string, error)

Get returns the value of a key

func (*Client) GetKeyMetadata

func (c *Client) GetKeyMetadata(ctx context.Context, key string) (*KeyMetadata, error)

GetKeyMetadata atomically retrieves key type, size, and TTL Returns nil if the key doesn't exist

func (*Client) GetMemoryStats

func (c *Client) GetMemoryStats(ctx context.Context) (*MemoryStats, error)

GetMemoryStats returns memory usage statistics from INFO memory

func (*Client) GetNotifyKeyspaceEvents

func (c *Client) GetNotifyKeyspaceEvents(ctx context.Context) (string, error)

GetNotifyKeyspaceEvents returns the current notify-keyspace-events setting

func (*Client) HDel

func (c *Client) HDel(ctx context.Context, key string, fields ...string) error

HDel removes fields from a hash

func (*Client) HExists

func (c *Client) HExists(ctx context.Context, key, field string) (bool, error)

HExists checks if a field exists in a hash

func (*Client) HGetAll

func (c *Client) HGetAll(ctx context.Context, key string) (map[string]string, error)

HGetAll returns all fields and values in a hash

func (*Client) HLen

func (c *Client) HLen(ctx context.Context, key string) (int64, error)

HLen returns the number of fields in a hash

func (*Client) HRename

func (c *Client) HRename(ctx context.Context, key, oldField, newField string) (string, error)

HRename atomically renames a hash field, preserving its value Returns the value and an error if the old field doesn't exist or the new field already exists

func (*Client) HScan

func (c *Client) HScan(ctx context.Context, key string, cursor uint64, count int64) (map[string]string, uint64, error)

HScan returns fields and values of a hash using cursor-based pagination

func (*Client) HSet

func (c *Client) HSet(ctx context.Context, key, field, value string) error

HSet sets a field value in a hash

func (*Client) IncrByFloat

func (c *Client) IncrByFloat(ctx context.Context, key string, amount float64) (string, error)

IncrByFloat increments a key by a float amount (handles both int and float)

func (*Client) Info

func (c *Client) Info(ctx context.Context, section string) (string, error)

Info returns server information

func (*Client) Keys

func (c *Client) Keys(ctx context.Context, pattern string, cursor uint64, count int64) ([]string, uint64, error)

Keys returns keys matching the pattern

func (*Client) LLen

func (c *Client) LLen(ctx context.Context, key string) (int64, error)

LLen returns the length of a list

func (*Client) LPush

func (c *Client) LPush(ctx context.Context, key string, values ...string) error

LPush prepends values to a list

func (*Client) LRange

func (c *Client) LRange(ctx context.Context, key string, start, stop int64) ([]string, error)

LRange returns elements from a list

func (*Client) LRemByIndex

func (c *Client) LRemByIndex(ctx context.Context, key string, index int64) error

LRemByIndex removes the element at the given index atomically using a Lua script This prevents race conditions where the list could be modified between LSET and LREM

func (*Client) LSet

func (c *Client) LSet(ctx context.Context, key string, index int64, value string) error

LSet sets the value at an index in a list

func (*Client) PFAdd

func (c *Client) PFAdd(ctx context.Context, key string, elements ...string) error

PFAdd adds elements to a HyperLogLog

func (*Client) PFCount

func (c *Client) PFCount(ctx context.Context, key string) (int64, error)

PFCount returns the approximate cardinality of the HyperLogLog

func (*Client) Persist

func (c *Client) Persist(ctx context.Context, key string) (bool, error)

Persist removes the TTL from a key

func (*Client) Ping

func (c *Client) Ping(ctx context.Context) error

Ping tests the connection

func (*Client) RPush

func (c *Client) RPush(ctx context.Context, key string, values ...string) error

RPush appends values to a list

func (*Client) Raw

func (c *Client) Raw() valkey.Client

Raw returns the underlying valkey client for direct access

func (*Client) Rename

func (c *Client) Rename(ctx context.Context, key, newkey string) error

Rename renames a key

func (*Client) SAdd

func (c *Client) SAdd(ctx context.Context, key string, members ...string) error

SAdd adds members to a set

func (*Client) SAddIfNotExists

func (c *Client) SAddIfNotExists(ctx context.Context, key, member string) (bool, error)

SAddIfNotExists atomically adds a member to a set only if it doesn't exist Returns true if added, false if already exists

func (*Client) SCard

func (c *Client) SCard(ctx context.Context, key string) (int64, error)

SCard returns the number of members in a set

func (*Client) SIsMember

func (c *Client) SIsMember(ctx context.Context, key string, member string) (bool, error)

SIsMember checks if a member exists in a set

func (*Client) SMembers

func (c *Client) SMembers(ctx context.Context, key string) ([]string, error)

SMembers returns all members of a set

func (*Client) SRem

func (c *Client) SRem(ctx context.Context, key string, members ...string) error

SRem removes members from a set

func (*Client) SRename

func (c *Client) SRename(ctx context.Context, key, oldMember, newMember string) error

SRename atomically renames a set member (removes old member, adds new member)

func (*Client) SScan

func (c *Client) SScan(ctx context.Context, key string, cursor uint64, count int64) ([]string, uint64, error)

SScan returns members of a set using cursor-based pagination

func (*Client) Set

func (c *Client) Set(ctx context.Context, key, value string, ttl time.Duration) error

Set sets the value of a key

func (*Client) SetNotifyKeyspaceEvents

func (c *Client) SetNotifyKeyspaceEvents(ctx context.Context, value string) error

SetNotifyKeyspaceEvents enables/disables keyspace notifications

func (*Client) SubscribeKeyspace

func (c *Client) SubscribeKeyspace(ctx context.Context, db int) (<-chan KeyEvent, error)

SubscribeKeyspace subscribes to keyspace notifications for a specific database. Returns a channel that emits KeyEvent for each key operation. The channel is closed when the context is cancelled or an error occurs.

func (*Client) TTL

func (c *Client) TTL(ctx context.Context, key string) (int64, error)

TTL returns the TTL of a key in seconds (-1 if no TTL, -2 if key doesn't exist)

func (*Client) Type

func (c *Client) Type(ctx context.Context, key string) (string, error)

Type returns the type of a key

func (*Client) XAddMulti

func (c *Client) XAddMulti(ctx context.Context, key string, fields map[string]string) (string, error)

XAddMulti appends an entry with multiple fields to a stream

func (*Client) XDel

func (c *Client) XDel(ctx context.Context, key string, ids ...string) (int64, error)

XDel removes one or more entries from a stream by ID

func (*Client) XLen

func (c *Client) XLen(ctx context.Context, key string) (int64, error)

XLen returns the number of entries in a stream

func (*Client) XRange

func (c *Client) XRange(ctx context.Context, key, start, stop string, count int64) ([]StreamEntry, error)

XRange returns entries from a stream

func (*Client) XRangePage

func (c *Client) XRangePage(ctx context.Context, key string, startAfterID string, pageSize int64) ([]StreamEntry, string, error)

XRangePage fetches a specific page of stream entries using ID-based pagination startAfterID: if provided, starts after this ID (for cursor-based pagination) If startAfterID is empty, starts from beginning

func (*Client) ZAdd

func (c *Client) ZAdd(ctx context.Context, key string, member string, score float64) error

ZAdd adds a member with score to a sorted set

func (*Client) ZCard

func (c *Client) ZCard(ctx context.Context, key string) (int64, error)

ZCard returns the number of members in a sorted set

func (*Client) ZIncrBy

func (c *Client) ZIncrBy(ctx context.Context, key string, member string, amount float64) (float64, error)

ZIncrBy increments the score of a member in a sorted set

func (*Client) ZRangeWithScores

func (c *Client) ZRangeWithScores(ctx context.Context, key string, start, stop int64) ([]ZMember, error)

ZRangeWithScores returns members with scores from a sorted set

func (*Client) ZRem

func (c *Client) ZRem(ctx context.Context, key string, members ...string) error

ZRem removes members from a sorted set

func (*Client) ZRename

func (c *Client) ZRename(ctx context.Context, key, oldMember, newMember string) (float64, error)

ZRename atomically renames a sorted set member, preserving its score Returns an error if the old member doesn't exist or the new member already exists

type GeoMember

type GeoMember struct {
	Member    string  `json:"member"`
	Longitude float64 `json:"longitude"`
	Latitude  float64 `json:"latitude"`
}

GeoMember represents a member with geographic coordinates

type GeoPosition

type GeoPosition struct {
	Longitude float64 `json:"longitude"`
	Latitude  float64 `json:"latitude"`
}

GeoPosition represents geographic coordinates

type KeyEvent

type KeyEvent struct {
	Operation string // "set", "del", "expire", "expired", "rename_from", "rename_to", etc.
	Key       string
}

KeyEvent represents a keyspace notification event

type KeyMetadata

type KeyMetadata struct {
	Type string
	Size int64
	TTL  int64
}

KeyMetadata represents metadata about a key

type MemoryStats

type MemoryStats struct {
	UsedMemory      int64
	UsedMemoryHuman string
}

MemoryStats represents memory usage statistics

type Script

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

Script represents a Lua script that can be executed atomically

func NewScript

func NewScript(script string) *Script

NewScript creates a new Script with the given Lua code

func (*Script) Eval

func (s *Script) Eval(ctx context.Context, c *Client, keys []string, args []string) (interface{}, error)

Eval executes the script with the given keys and args Uses EVALSHA for efficiency, falls back to EVAL if script not cached

func (*Script) Load

func (s *Script) Load(ctx context.Context, c *Client) error

Load preloads the script on the server using SCRIPT LOAD This is optional but can improve performance if the script will be used many times

type StreamEntry

type StreamEntry struct {
	ID     string            `json:"id"`
	Fields map[string]string `json:"fields"`
}

StreamEntry represents an entry in a stream

type ZMember

type ZMember struct {
	Member string  `json:"member"`
	Score  float64 `json:"score"`
}

ZMember represents a member with score in a sorted set

Jump to

Keyboard shortcuts

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