惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

博客园 - 叶小钗
Last Week in AI
Last Week in AI
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
V
V2EX
J
Java Code Geeks
The GitHub Blog
The GitHub Blog
博客园_首页
U
Unit 42
人人都是产品经理
人人都是产品经理
Engineering at Meta
Engineering at Meta
IT之家
IT之家
G
Google Developers Blog
L
LangChain Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
爱范儿
爱范儿
罗磊的独立博客
博客园 - 三生石上(FineUI控件)
Martin Fowler
Martin Fowler
Jina AI
Jina AI
有赞技术团队
有赞技术团队
Apple Machine Learning Research
Apple Machine Learning Research
Vercel News
Vercel News
小众软件
小众软件
H
Help Net Security

DEV Community

Authentication Security Deep Dive: From Brute Force to Salted Hashing (With Java Examples) Why AI Systems Don’t Fail — They Drift Spilling beans for how i learn for exam😁"Reinforcement Learning Cheat Sheet" I Replaced Chrome with Safari for AI Browser Automation. Here's What Broke (and What Finally Worked) How Python Borrows Other People's Work The $40 Architecture: Processing 1 Billion API Requests with 99.99% Uptime Vibe Coding: A Workflow Guide (From Zero to SaaS) Most webhook security guides protect the wrong side. The scary part is delivery. Headless CMS for TanStack Start: Build a Blog with Cosmic EU Age Verification App "Hacked in 2 Minutes" — What Actually Happened Comfy Cloud’s delete function does not actually remove files Running AI Models on GPU Cloud Servers: A Beginner Guide Event-driven media intelligence with AWS Step Functions and Bedrock I scored 500 AI prompts across 8 quality dimensions — here's what broke How to Call Google Gemini API from Next.js (Free Tier, No Backend Needed) The Portal Protocol: Reclaiming Human Connection in the Age of AI How to Fix Your Team's Scattered Knowledge Problem With a Self-Hosted Forum Intro to tc Cloud Functors: A Graph-First Mental Model for the Modern Cloud Designing Multi-Tenant Backends With Both Ownership and Team Access I Built a Neumorphic CSS Library with 77+ Components — Here's What I Learned PostgreSQL Performance Optimization: Why Connection Pooling Is Critical at Scale Cómo construí un SaaS multi-rubro para gestionar expensas en Argentina con FastAPI + Vue 3 🚀 I Built an Ethical Hacking Scanner Tool – Open Source Project I Replaced /usage and /context in Claude Code With a Single Statusline A Pythonic Way to Handle Emails (IMAP/SMTP) with Auto-Discovery and AI-Ready Design I Collected 8.9 Million Polymarket Price Points — Here's What I Found About How Markets Really Move EcoTrack AI — Carbon Footprint Tracker & Dashboard Everyone's Using AI. No One Agrees How. 5 self-hosted ebook managers worth trying in 2026 Building Your First AI Agent with LangChain: From Chatbot to Autonomous Assistant
I have created yet another cache go library
Dmitry Maslyukov · 2026-05-04 · via DEV Community

Dmitry Maslyukov

I have recently been frustrated by one thing: there is practically no library for caching singleton values.

The thing is most caches are designed to keep a lot of things in one caching container.

What if I did not need the key-value generic map?
So on one hand there was complexity and generic implementations of caching with ttl, but nevertheless well-maintained, but on the other hand there were no solutions for caching singletons.

So I have decided to create one myself. This was not very painful, but I feel strange that practically no-one has done this before.

Sincerely, I am writing this post in a thought that someone comes across it and has same demand.

I will be happy to get someone's day easier. Do not spend AI tokens for this boilerplate, just download it.

The license is MIT, so you can use, fork or do whatever you want in any kind of product.

Single Value Cache

This is a tiny, focused Go library designed for those moments when you need to cache just one thing the best way, that cache libraries do not provide.

Whether it's a configuration object, a token, or a result from a heavy database query, this library helps you handle it without the boilerplate of a full-blown key-value store.

Why use this?

Caching can be tricky. You often have to deal with:

  • Race conditions: Multiple goroutines trying to update the same value.
  • Thundering herds: A sudden spike in requests when the cache expires, all hitting your database at once.
  • Type safety: Casting any back and forth.

This library solves these problems by combining Generics, TTL (Time-To-Live), and Singleflight protection in a simple, human-friendly package.

Features

  • Type Safe: Built with Go Generics, so no more type assertions.
  • Smart Loading: Using golang.org/x/sync/singleflight, it ensures that even if 100…


package your_best_program

import "github.com/maslyukov0/single_value_cache"

func loadData(ctx context.Context) (int, error) {
    select {
    case <- time.After(time.Minute):
       return 42
    case <- ctx.Done():
       return errors.New("my most liked error")
    }
}

var cache = cache.NewSingleValueCache[int](time.Hour, loadData)

func main() {

    // It will propagate context.Cancelled or any other error from    your loader
    value, err := cache.Get(context.Background())
    fmt.Printf("Value = %v, error = %v\n", value, err)
}


As you can see, it is simple as hell.

I have already got it into action into product I am building: there is a domain access whitelist and it works fine.

Also feel free to open an issue or comment a question.

Happy developing day!