Skip to main content
Version: 1.5.0

Memcached

In-Memory Cachebackend/memcached

Memcached is a high-performance, distributed memory object caching system. It's designed to speed up dynamic web applications by alleviating database load through caching data and objects in RAM.

📦 Installation

go get github.com/azrod/kivigo/backend/memcached

Requires one or more Memcached servers. You can run Memcached locally with Docker: `docker run -d -p 11211:11211 memcached`

Dependencies

  • Memcached server(s) running
  • Network connectivity to Memcached servers
  • Sufficient memory allocation on servers

✨ Features

Basic Operations
Batch Operations
Health Checks
TTL/Expiration
Distributed Caching
High Performance
Memory Efficiency
Simple Protocol

🚀 Usage

package main

import (
"time"

"github.com/azrod/kivigo"
"github.com/azrod/kivigo/backend/memcached"
)

func main() {
// Single server configuration
opt := memcached.NewOptions()
opt.Servers = []string{"localhost:11211"}
opt.Timeout = 1 * time.Second
opt.MaxIdleConns = 2

// Multiple servers with consistent hashing
multiOpt := memcached.NewOptions()
multiOpt.Servers = []string{
"memcached1:11211",
"memcached2:11211",
"memcached3:11211",
}
multiOpt.Timeout = 500 * time.Millisecond
multiOpt.MaxIdleConns = 10
multiOpt.KeepAlive = 30 * time.Second

// Optional: Configure default TTL
multiOpt.DefaultTTL = 3600 // 1 hour

// Create backend
kvStore, err := memcached.New(multiOpt)
if err != nil {
panic(err)
}
defer kvStore.Close()

// Create client
client, err := kivigo.New(kvStore)
if err != nil {
panic(err)
}
}

📝 Notes

  • Memcached is purely in-memory - data is lost when the server restarts
  • Maximum value size is 1MB by default
  • Keys have a maximum length of 250 characters
  • Use consistent hashing for multi-server setups to minimize rehashing
  • Monitor memory usage to prevent evictions of important data
  • Consider using connection pooling for high-traffic applications
  • Memcached doesn't natively support key enumeration (List operation may be limited)
  • For persistent caching, consider Redis as an alternative

🔗 Additional Resources