go
47 lines · 1 tab
Leah Thompson
Jan 2026
1 tab
package cache
import (
"context"
"sync"
"time"
"golang.org/x/sync/singleflight"
)
type item struct {
v string
expires time.Time
}
type Cache struct {
mu sync.Mutex
data map[string]item
group singleflight.Group
}
func New() *Cache { return &Cache{data: make(map[string]item)} }
func (c *Cache) Get(ctx context.Context, key string, ttl time.Duration, loader func(context.Context) (string, error)) (string, error) {
c.mu.Lock()
if it, ok := c.data[key]; ok && time.Now().Before(it.expires) {
v := it.v
c.mu.Unlock()
return v, nil
}
c.mu.Unlock()
v, err, _ := c.group.Do(key, func() (any, error) {
val, err := loader(ctx)
if err != nil {
return "", err
}
c.mu.Lock()
c.data[key] = item{v: val, expires: time.Now().Add(ttl)}
c.mu.Unlock()
return val, nil
})
if err != nil {
return "", err
}
return v.(string), nil
}
1 file · go
Explain with highlit
When a cache key expires, it’s easy for a burst of requests to stampede the database. I use singleflight.Group to ensure only one goroutine performs the expensive fill per key while others wait for the shared result. This doesn’t replace proper TTLs or circuit breakers, but it smooths the “sawtooth” load pattern you get with synchronized expirations. The code is intentionally small: check the cache, call group.Do on miss, and set the cache on success. The other detail I care about is error behavior: if the fill fails, I don’t poison the cache, and I return the underlying error so the caller can decide whether to serve stale data. It’s one of the highest ROI concurrency primitives in Go.
Related snips
go
package api
import (
"net/http"
"runtime/debug"
)
Expose build metadata for debugging deploys
go
observability
build
by Leah Thompson
1 tab
typescript
export interface RetryOptions {
retries: number;
baseMs: number;
maxMs: number;
signal?: AbortSignal;
onRetry?: (attempt: number, delay: number, err: unknown) => void;
Exponential backoff with jitter for retries
typescript
reliability
retry
by codesnips
2 tabs
go
package dbutil
import (
"context"
"github.com/jackc/pgconn"
Retry Postgres serialization failures with bounded attempts
go
postgres
transactions
by Leah Thompson
1 tab
rust
use crossbeam::channel::unbounded;
use std::thread;
fn main() {
let (tx, rx) = unbounded();
Crossbeam for advanced concurrent data structures
rust
concurrency
lock-free
by Marcus Chen
1 tab
javascript
// Creating a Promise
const myPromise = new Promise((resolve, reject) => {
const success = true;
setTimeout(() => {
if (success) {
Promises and async/await patterns for asynchronous JavaScript
javascript
promises
async-await
by Alex Chang
1 tab
rust
use std::sync::mpsc;
use std::thread;
fn main() {
let (tx, rx) = mpsc::channel();
Channels (mpsc) for message passing between threads
rust
concurrency
channels
by Marcus Chen
1 tab
Share this code
Here's the card — post it anywhere.