anycache-knowledge

Implement function, batch, and multi-level caching in Go services using the AnyCache library.

Updated Apr 30, 2026
One-click install
npx skills add https://github.com/yuezhen-huang/my-bytedance-skillhub --skill anycache-knowledge-yuezhen-huang
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: anycache-knowledge
Source: https://github.com/yuezhen-huang/my-bytedance-skillhub/tree/main/anycache-knowledge
Command: npx skills add https://github.com/yuezhen-huang/my-bytedance-skillhub --skill anycache-knowledge-yuezhen-huang

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Manually wiring cache read/write logic into Go services is error-prone and repetitive. This Skill teaches how to use the AnyCache library (code.byted.org/webcast/libs_anycache) to automatically cache function results, handle batch lookups, and apply cache strategies without hand-writing cache plumbing. ## Core Features & Use Cases - Function Cache: Wrap any data-loading function with BuildFetcherByLoader so results are cached automatically with configurable TTL and namespace. - Batch Cache: Use BuildBatchFetcherByBatchLoader to fetch many keys at once, automatically splitting cache hits from source lookups. - Multi-Level Cache & Strategies: Combine local memory and Redis caches, and choose from six source strategies such as expired-data backup and async refresh for high availability. - Use Case: When building a user profile service, wrap the database query in a Fetcher with a local-plus-Redis multi-level cache, MsgPack serialization, and SsExpiredDataBackup so reads stay fast and resilient even if the database fails. ## Quick Start Show me how to add a Redis-backed function cache with a 10-minute TTL and expired-data fallback to my Go user lookup function using AnyCache.

Frequently Asked Questions about anycache-knowledge

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I cache a function result in Go with AnyCache?

Use BuildFetcherByLoader with your data-loading function, a cache component like NewRedisCache, and a serializer such as NewJSONSerializer. Configure TTL and namespace via WithTTL and WithNamespace, then call fetcher.Get to read through the cache automatically.

How to implement batch caching for multiple keys in Go?

Use BuildBatchFetcherByBatchLoader when your source function supports batch queries, or BuildBatchFetcherByLoader to fan out concurrent single-key loads. Call batchFetcher.MGet with a key slice; cache hits return directly while misses are loaded from the source.

What cache strategies does AnyCache support?

AnyCache provides six source strategies: SsCacheFirst, SsSourceFirst, SsOnlyCache, SsOnlySource, SsExpiredDataBackup, and SsExpiredDataAndAsyncSource. Set them with WithSourceStrategy to control cache-first reads, fallback to stale data, or asynchronous refresh.

Does AnyCache support combining local memory and Redis caches?

Yes, NewMultiLevelCache composes multiple cache layers such as a LocalBytesCache for L1 and a Redis cache for L2. Lookups check the local cache first and fall through to Redis on a miss, reducing network overhead.

How do I prevent cache penetration with AnyCache?

Enable WithCacheNil(true) so empty results are also cached, blocking repeated source queries for nonexistent keys. Combine this with a reasonable TTL and WithMaxFetchItemsCount to bound batch request sizes.

JSON vs MsgPack serializer in AnyCache, which should I use?

JSON serializers (NewJSONSerializer or the faster NewJSONIterSerializer) offer broad compatibility, while NewMsgPackSerializer delivers better performance and smaller payloads. Choose MsgPack for high-throughput batch workloads and JSON for general interoperability.