blitzcache

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Oct 21, 2025 License: MIT Imports: 10 Imported by: 0

README

BlitzCache ⚡

A high-performance, sub-millisecond in-memory cache written in Go with Redis protocol compatibility.

Features

  • Sub-millisecond latency (<1ms p99)
  • 256-way sharding for reduced lock contention
  • Timing wheel for efficient TTL expiration
  • Redis-compatible RESP protocol
  • 10M+ ops/sec throughput
  • Zero dependencies

Installation

As a Library
  • go get github.com/PriestYKing/blitzcache
As a Binary
  • go install github.com/PriestYKing/blitzcache/cmd/blitzcache@latest

Quick Start

Using as Library

package main

import ( "fmt" "time" "github.com/YOUR_USERNAME/blitzcache" )

func main() { cache := blitzcache.NewCache(256) defer cache.Close()

// Set with TTL cache.Set("key", []byte("value"), 60*time.Second)

// Get if value, ok := cache.Get("key"); ok { fmt.Println(string(value)) }

// Stats stats := cache.Stats() fmt.Printf("Hits: %d, Misses: %d\n", stats["hits"], stats["misses"])

Running as Server

blitzcache -addr :6380 -shards 256

Connect with Redis CLI:

redis-cli -p 6380 SET mykey "Hello" EX 60 GET mykey STATS

Benchmarks

  • BenchmarkCacheGet-8 10000000 127 ns/op 0 B/op 0 allocs/op
  • BenchmarkCacheSet-8 5000000 234 ns/op 48 B/op 1 allocs/op
  • BenchmarkCacheConcurrent-8 50000000 34 ns/op 0 B/op 0 allocs/op

API Reference

Cache Methods
  • NewCache(shardCount int) *Cache - Create new cache instance
  • Set(key string, value []byte, ttl time.Duration) error - Set key with TTL
  • Get(key string) ([]byte, bool) - Get key value
  • Delete(key string) bool - Delete key
  • Stats() map[string]int64 - Get cache statistics
  • Flush() - Clear all keys
  • Close() - Shutdown cache
Server Commands (RESP Protocol)
  • SET key value [EX seconds]
  • GET key
  • DEL key
  • STATS
  • FLUSH
  • PING

License

MIT

Documentation

Index

Constants

View Source
const (
	DefaultShardCount   = 256
	DefaultTickDuration = 100 * time.Millisecond
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

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

func NewCache

func NewCache(shardCount int) *Cache

func (*Cache) Close

func (c *Cache) Close()

func (*Cache) Count

func (c *Cache) Count() int64

func (*Cache) Delete

func (c *Cache) Delete(key string) bool

func (*Cache) Flush

func (c *Cache) Flush()

func (*Cache) Get

func (c *Cache) Get(key string) ([]byte, bool)

func (*Cache) Set

func (c *Cache) Set(key string, value []byte, ttl time.Duration) error

func (*Cache) Stats

func (c *Cache) Stats() map[string]int64

type CacheItem

type CacheItem struct {
	Value     []byte
	ExpiresAt int64
	Version   uint64
}

type Server

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

func NewServer

func NewServer(addr string, cache *Cache) *Server

func (*Server) Start

func (s *Server) Start() error

type Shard

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

type Stats

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

type TimingWheel

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

func NewTimingWheel

func NewTimingWheel(tickDuration, wheelDuration time.Duration, onExpire func(string)) *TimingWheel

func (*TimingWheel) Add

func (tw *TimingWheel) Add(key string, ttl time.Duration)

func (*TimingWheel) Start

func (tw *TimingWheel) Start()

func (*TimingWheel) Stop

func (tw *TimingWheel) Stop()

Directories

Path Synopsis
cmd
blitzcache command

Jump to

Keyboard shortcuts

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