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

推荐订阅源

L
LangChain Blog
Recent Announcements
Recent Announcements
GbyAI
GbyAI
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Microsoft Azure Blog
Microsoft Azure Blog
N
Netflix TechBlog - Medium
人人都是产品经理
人人都是产品经理
MongoDB | Blog
MongoDB | Blog
D
DataBreaches.Net
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
WordPress大学
WordPress大学
U
Unit 42
腾讯CDC
D
Docker
The GitHub Blog
The GitHub Blog
阮一峰的网络日志
阮一峰的网络日志
Vercel News
Vercel News
I
InfoQ
Jina AI
Jina AI
爱范儿
爱范儿
宝玉的分享
宝玉的分享
博客园 - Franky
G
Google Developers Blog
P
Proofpoint News Feed

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!