Filecache
A simple, safe, fast local file cache of pure Golang
Install / Use
/learn @ucwong/FilecacheREADME
filecache
A simple file cache of pure Golang
Install
First, you need to install the package:
go get -u github.com/ucwong/filecache
Overview
A file cache can be created with either the NewDefaultCache() function to
get a cache with the defaults set, or NewCache() to get a new cache with
0 values for everything; you will not be able to store items in this cache
until the values are changed; specifically, at a minimum, you should set
the MaxItems field to be > 0.
Let's start with a basic example; we'll create a basic cache and give it a maximum item size of 128M:
import (
"github.com/ucwong/filecache"
)
...
cache := filecache.NewDefaultCache()
cache.MaxSize = 128 * filecache.Megabyte
cache.Start()
...
cache.Stop()
The Kilobyte, Megabyte, and Gigabyte constants are provided as a
convience when setting cache sizes.
When cache.Start() is called, a goroutine is launched in the background
that routinely checks the cache for expired items. The delay between
runs is specified as the number of seconds given by cache.Every ("every
cache.Every seconds, check for expired items"). There are three criteria
used to determine whether an item in the cache should be expired; they are:
- Has the file been modified on disk? (The cache stores the last time of modification at the time of caching, and compares that to the file's current last modification time).
- Has the file been in the cache for longer than the maximum allowed time?
- Is the cache at capacity? When a file is being cached, a check is made to see if the cache is currently filled. If it is, the item that was last accessed the longest ago is expired and the new item takes its place. When loading items asynchronously, this check might miss the fact that the cache will be at capacity; the background scanner performs a check after its regular checks to ensure that the cache is not at capacity.
The background scanner can be disabled by setting cache.Every to 0; if so,
cache expiration is only done when the cache is at capacity.
Once the cache is no longer needed, a call to cache.Stop() will close down
the channels and signal the background scanner that it should stop.
Related Skills
node-connect
351.8kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
xurl
351.8kA CLI tool for making authenticated requests to the X (Twitter) API. Use this skill when you need to post tweets, reply, quote, search, read posts, manage followers, send DMs, upload media, or interact with any X API v2 endpoint.
frontend-design
110.9kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
351.8kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
