Redis 与 BoltCache
Redis vs. BoltCache

原始链接: https://github.com/wutlu/boltcache

## BoltCache:高性能Redis替代方案 BoltCache是一个用Go构建的快速、可扩展的内存缓存系统,提供与Redis兼容的功能,并具有显著的性能提升(基准测试中快30-50%)。它通过现代的**RESTful API**(以及TCP支持)、基于WebSocket的强大的**发布/订阅消息传递**以及全面的企业级功能而与众不同。 主要特性包括**TTL支持**、使用无锁数据结构进行**线程安全**操作、**持久化**到JSON磁盘存储以及**集群**选项,支持主从复制。BoltCache支持常见数据类型(字符串、列表、集合、哈希)和**Lua脚本**,用于复杂操作。 安全性通过**基于令牌的身份验证**和**速率限制**来解决,而**监控**则通过内置指标和健康检查来提供。配置通过YAML文件管理。BoltCache易于使用**Docker**和**Kubernetes**部署,并包含一个Web客户端,用于交互式测试。它专为在**微服务**架构中使用而设计,并为Redis提供了一个引人注目的替代方案。 [https://github.com/wutlu/boltcache](https://github.com/wutlu/boltcache)

黑客新闻 新 | 过去 | 评论 | 提问 | 展示 | 招聘 | 提交 登录 Redis vs. BoltCache (github.com/wutlu) 8 分,由 spotlayn 发表于 2 小时前 | 隐藏 | 过去 | 收藏 | 讨论 指南 | 常见问题 | 列表 | API | 安全 | 法律 | 申请YC | 联系 搜索:
相关文章

原文

High-performance, Redis-compatible in-memory cache with RESTful API

BoltCache is a modern, fast, and scalable in-memory cache system built in Go. It provides Redis-like functionality with better performance, RESTful API support, and enterprise-grade features.

Go Version License API PRs Welcome

Keywords: redis, cache, in-memory, golang, rest-api, pub-sub, high-performance, microservices, docker, kubernetes

  • High Performance: 30-50% faster than Redis using Go's concurrency
  • 🌐 RESTful API: Modern HTTP/JSON interface alongside TCP protocol
  • 🔄 Pub/Sub Messaging: Real-time messaging with WebSocket support
  • TTL Support: Automatic key expiration and cleanup
  • 🔒 Thread-Safe: Concurrent operations with lock-free data structures
  • 💾 Persistence: JSON-based disk storage with backup support
  • 🔗 Clustering: Master-slave replication and high availability
  • 📊 Complex Data Types: String, List, Set, Hash support
  • 🔧 Lua Scripting: Execute custom scripts for complex operations
  • 🛡️ Security: Token-based authentication and rate limiting
  • 📈 Monitoring: Built-in metrics, health checks, and profiling
  • ⚙️ Configuration: YAML-based configuration management
# Clone the repository
git clone https://github.com/wutlu/boltcache.git
cd boltcache

# Install dependencies
go mod download

# Generate default configuration
make generate-config

# Start the server
make run-dev

The server will start on:

# Health check
curl http://localhost:8090/ping

# Set a value
curl -X PUT http://localhost:8090/cache/hello \
  -H "Content-Type: application/json" \
  -d '{"value": "world"}'

# Get the value
curl http://localhost:8090/cache/hello

# Test TCP protocol
telnet localhost 6380
SET mykey myvalue
GET mykey
PING

BoltCache provides a comprehensive RESTful API:

# Set value
PUT /cache/{key}
{"value": "data", "ttl": "5m"}

# Get value
GET /cache/{key}

# Delete key
DELETE /cache/{key}
# Push to list
POST /list/{key}
["item1", "item2"]

# Pop from list
DELETE /list/{key}
# Add to set
POST /set/{key}
["member1", "member2"]

# Get set members
GET /set/{key}
# Set hash field
PUT /hash/{key}/{field}
{"value": "data"}

# Get hash field
GET /hash/{key}/{field}
# Publish message
POST /publish/{channel}
{"message": "Hello World"}

# Subscribe (WebSocket)
GET /subscribe/{channel}
# Execute script
POST /eval
{
  "script": "redis.call('SET', KEYS[1], ARGV[1])",
  "keys": ["mykey"],
  "args": ["myvalue"]
}

BoltCache uses YAML configuration files:

# config.yaml
server:
  mode: "rest"  # tcp, rest, both
  rest:
    port: 8090
    cors_enabled: true
  tcp:
    port: 6380

cache:
  max_memory: "1GB"
  cleanup_interval: "1m"
  eviction_policy: "lru"

persistence:
  enabled: true
  file: "./data/cache.json"
  interval: "30s"

security:
  auth:
    enabled: true
    tokens: ["your-secret-token"]

Environment-Specific Configs

# Development
make run-dev

# Production
make run-prod

# Custom config
go run main-config.go -config custom.yaml
# Server operations
make run              # Run with default config
make run-dev          # Run development mode
make run-prod         # Run production mode
make build            # Build binary

# Configuration
make generate-config  # Generate default config
make validate-config  # Validate configuration
make show-config      # Show current config

# Testing
make test-rest        # Test REST API
make test-auth        # Test authentication
make test-pubsub      # Test Pub/Sub messaging
# Web client: http://localhost:8090/rest-client.html

# Clustering
make cluster-master   # Start cluster master
make cluster-slave    # Start cluster slave
  1. Web Client: Interactive browser-based client

    # Start server
    make run-dev
    
    # Open web client
    open http://localhost:8090/rest-client.html
  2. Postman Collection: Import BoltCache.postman_collection.json

  3. cURL Scripts:

    ./examples/rest-examples.sh
    ./examples/auth-examples.sh
    ./examples/test-pubsub.sh
  4. Interactive Client:

    go run client.go interactive

BoltCache supports token-based authentication:

# Using Authorization header
curl -H "Authorization: Bearer your-token" \
  http://localhost:8090/cache/key

# Using X-API-Token header
curl -H "X-API-Token: your-token" \
  http://localhost:8090/cache/key

# Using query parameter
curl "http://localhost:8090/cache/key?token=your-token"
security:
  auth:
    enabled: true
    method: "token"
    tokens:
      - "production-token-1"
      - "production-token-2"
  rate_limit:
    enabled: true
    requests_per_second: 1000
    burst: 100

BoltCache vs Redis:

Operation BoltCache Redis Improvement
SET 180k ops/s 120k ops/s +50%
GET 200k ops/s 150k ops/s +33%
Memory 45MB 67MB -33%
Latency 0.8ms 1.2ms -33%
performance:
  max_goroutines: 10000
  gc_percent: 100
  connection_pool:
    max_idle: 100
    max_active: 1000
  • Storage Engine: Lock-free sync.Map for concurrent access
  • Network Layer: HTTP/REST and TCP protocol support
  • Pub/Sub System: Channel-based non-blocking messaging
  • Persistence Layer: JSON-based storage with compression
  • Authentication: Token-based security middleware
  • Configuration: YAML-based configuration management
Client Request → Auth Middleware → Router → Cache Engine → Storage
                                      ↓
Pub/Sub System ← Background Tasks ← Persistence Layer
# Build image
docker build -t boltcache .

# Run container
docker run -p 8090:8090 -v ./data:/app/data boltcache
# Start cluster
docker-compose up
# Deploy to Kubernetes
kubectl apply -f k8s/
  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

This project is licensed under the MIT License - see the LICENSE file for details.

  • Inspired by Redis architecture
  • Built with Go's excellent concurrency primitives
  • Uses Gorilla WebSocket and Mux libraries

Created by Alper Mutlu Toksöz with ❤️

联系我们 contact @ memedata.com