Skip to content

berruk/in-memory

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

In-Memory Data Store

A simple in-memory data store in Go with REST API and client library.

Features

  • String and list operations
  • TTL support
  • Data persistence to disk
  • REST API
  • Go client library
  • Docker support

Quick Start

# Using Docker
docker-compose up -d

# Or run locally
go run main.go

Server runs on http://localhost:8080.

API Examples

# Set a string
curl -X POST http://localhost:8080/strings \
  -H "Content-Type: application/json" \
  -d '{"key":"user","value":"john"}'

# Get a string
curl http://localhost:8080/strings/user

# Create a list
curl -X POST http://localhost:8080/lists \
  -H "Content-Type: application/json" \
  -d '{"key":"items","value":["apple","banana"]}'

# Add to list
curl -X POST http://localhost:8080/lists/items/push \
  -H "Content-Type: application/json" \
  -d '{"value":"orange"}'

# Remove from list
curl -X POST http://localhost:8080/lists/items/pop

# Delete key
curl -X DELETE http://localhost:8080/keys/user

Go Client

package main

import (
    "fmt"
    "log"
    "in-memory/client"
)

func main() {
    c := client.New()
    
    // String operations
    err := c.Set("user", "john")
    if err != nil {
        log.Fatal(err)
    }
    
    value, err := c.Get("user")
    if err != nil {
        log.Fatal(err)
    }
    fmt.Printf("User: %s\n", value)
    
    // List operations
    err = c.SetList("items", []string{"apple", "banana"})
    if err != nil {
        log.Fatal(err)
    }
    
    err = c.Push("items", "orange")
    if err != nil {
        log.Fatal(err)
    }
    
    items, err := c.GetList("items")
    if err != nil {
        log.Fatal(err)
    }
    fmt.Printf("Items: %v\n", items)
}

Project Structure

in-memory/
├── api/                 # HTTP handlers and models
├── client/             # Go client library
├── store/              # Core in-memory store
├── docs/               # Documentation
├── main.go             # Server entry point
└── docker-compose.yml  # Docker configuration

Configuration

Configure with environment variables:

# Set port
PORT=9000 go run main.go

# Set persistence file location
PERSIST_FILE=my_data.json go run main.go

# Both settings
PORT=9000 PERSIST_FILE=my_data.json go run main.go

Persistence

Data is automatically saved to data.json when the server shuts down gracefully (Ctrl+C). On startup, any existing data is loaded from the file.

Features:

  • Graceful shutdown saves data
  • Expired entries are cleaned up during save/load
  • Configurable data file location

Testing

go test ./...

Documentation

Limitations

  • Single instance
  • No clustering
  • No authentication
  • File-based persistence only (not database)

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published