memory

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 28, 2025 License: Apache-2.0 Imports: 6 Imported by: 0

README

Memory KV Driver

A thread-safe in-memory key-value storage driver that implements the KeyValueServiceInterface for local development and testing.

Features

  • Thread-safe: Uses sync.RWMutex for concurrent access
  • Expiration support: Automatic cleanup of expired keys
  • Multiple data structures:
    • Key-value pairs with optional TTL
    • Hash maps
    • Sets
    • Sorted sets
  • JSON support: Built-in JSON serialization/deserialization
  • Memory efficient: Automatic cleanup of expired items and empty collections

Usage

import (
    kvMemory "github.com/apito-io/engine/database/kv/memory"
    "github.com/apito-io/engine/models"
)

// Create a new memory KV service
cfg := &models.Config{}
kvService, err := kvMemory.GetKVMemoryDriver(cfg)
if err != nil {
    log.Fatal(err)
}
defer kvService.Close()

// Use the service
ctx := context.Background()
err = kvService.SetValue(ctx, "key", "value", time.Hour)

Configuration

Set the KV storage engine to memory in your environment:

export KV_ENGINE=memory

Or in your configuration:

cfg.KVStorageEngine = _const.MemoryDb

Methods Implemented

  • SetValue / GetValue - Basic key-value operations
  • SetJSONObject / GetJSONObject - JSON object storage
  • SetToHashMap / GetFromHashMap / CheckKeyHashMap - Hash map operations
  • AddToSets / RemoveSets / GetStoreDomains - Set operations
  • AddToSortedSets / GetFromSortedSets - Sorted set operations
  • DelValue - Delete keys
  • CheckRedisKey - Check key existence

Automatic Cleanup

The driver automatically:

  • Removes expired keys every minute
  • Cleans up empty collections
  • Provides thread-safe access to all operations

Testing

Run the test suite:

go test -v

Use Cases

  • Local development without external dependencies
  • Unit testing
  • Lightweight caching for single-instance applications
  • Development environments where Redis/Badger is not available

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type KVMemoryService

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

KVMemoryService implements KeyValueServiceInterface using in-memory storage

func GetKVMemoryDriver

func GetKVMemoryDriver(cfg *models.Config) (*KVMemoryService, error)

func (*KVMemoryService) AddToSets

func (m *KVMemoryService) AddToSets(ctx context.Context, key string, value string) error

AddToSets adds a value to a set.

func (*KVMemoryService) AddToSortedSets

func (m *KVMemoryService) AddToSortedSets(ctx context.Context, setName string, key string, exp time.Duration) error

AddToSortedSets adds a key to a sorted set with a given TTL (Time To Live) in seconds.

func (*KVMemoryService) CheckKeyHashMap

func (m *KVMemoryService) CheckKeyHashMap(ctx context.Context, hash, key string) bool

CheckKeyHashMap checks if a key exists in a hash.

func (*KVMemoryService) CheckRedisKey

func (m *KVMemoryService) CheckRedisKey(ctx context.Context, keys ...string) (bool, error)

CheckRedisKey checks if one or more keys exist.

func (*KVMemoryService) Close

func (m *KVMemoryService) Close()

Close stops the cleanup goroutine

func (*KVMemoryService) DelValue

func (m *KVMemoryService) DelValue(ctx context.Context, key string) error

DelValue deletes a value using a key.

func (*KVMemoryService) GetFromHashMap

func (m *KVMemoryService) GetFromHashMap(ctx context.Context, hash, key string) (string, error)

GetFromHashMap retrieves a value from a hash using a key.

func (*KVMemoryService) GetFromSortedSets

func (m *KVMemoryService) GetFromSortedSets(ctx context.Context, setName string, key string) (float64, error)

GetFromSortedSets retrieves a key from a sorted set.

func (*KVMemoryService) GetJSONObject

func (m *KVMemoryService) GetJSONObject(ctx context.Context, key string) (interface{}, error)

GetJSONObject retrieves a JSON object using a key.

func (*KVMemoryService) GetStoreDomains

func (m *KVMemoryService) GetStoreDomains(ctx context.Context, sets string, member string) (bool, error)

GetStoreDomains checks if a member exists in a set (equivalent to Redis SISMEMBER)

func (*KVMemoryService) GetValue

func (m *KVMemoryService) GetValue(ctx context.Context, key string) (string, error)

GetValue retrieves a value using a key.

func (*KVMemoryService) RemoveSets

func (m *KVMemoryService) RemoveSets(ctx context.Context, key string, value string) error

RemoveSets removes a value from a set.

func (*KVMemoryService) SetJSONObject

func (m *KVMemoryService) SetJSONObject(ctx context.Context, key string, value interface{}, expiration time.Duration) error

SetJSONObject sets a JSON object with a key with a given expiration time.

func (*KVMemoryService) SetToHashMap

func (m *KVMemoryService) SetToHashMap(ctx context.Context, hash, key string, value string) error

SetToHashMap sets a key-value pair in a hash.

func (*KVMemoryService) SetValue

func (m *KVMemoryService) SetValue(ctx context.Context, key string, value string, expiration time.Duration) error

SetValue sets a value with a key with a given expiration time.

Jump to

Keyboard shortcuts

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