This example demonstrates how to persist cache data to disk and restore it.
- Saving cache to file
- Loading cache from file
- Serializing custom types
- Sharded cache serialization
- Data persistence patterns
cd examples/serialization
go run main.go-
Saving to Disk
SaveFile()- write cache to file- Gob encoding format
- Preserves expiration times
-
Loading from Disk
LoadFile()- restore cache from file- Validates data integrity
- Maintains cache structure
-
Custom Types
- Structs and complex types
- Type registration for Gob
- Safe serialization
-
Sharded Cache
- Shard-aware serialization
- Correct key redistribution
- Shard count validation
- Application Restart: Preserve cache across restarts
- Backup: Create cache snapshots
- Migration: Transfer cache between instances
- Pre-population: Load initial data from file
- Custom types must be registered with
gob.Register() - Fields must be exported (capitalized)
- Circular references not supported
- Shard count must match on load
- Changing shard count requires regenerating the file
- Basic Example - Learn core operations first
- Sharded Example - Understand sharding before serializing