Cache
In-memory key-value cache with TTL, modelled on ElastiCache, Azure Cache for Redis, and Memorystore
aws ElastiCacheazr Cache for Redisgcp Memorystore
Emulates a managed in-memory cache — the Redis/Memcached instance you'd put in front of a database for sessions, rate counters, or hot lookups. You create a named cache instance, then store, read, and expire keys against it.
Reach for it in tests when your code does cache-aside reads, sets values with a TTL, or depends on eviction — so you can exercise those paths without standing up a real Redis. Under the hood each instance is a plain key-value store: keys map to byte values, each with an optional expiry.
| Provider | Service | Driver |
|---|---|---|
| AWS | ElastiCache | aws.ElastiCache |
| Azure | Cache for Redis | azure.Cache |
| GCP | Memorystore | gcp.Memorystore |
Create an instance#
A cache instance is the container your keys live in — the equivalent of provisioning an ElastiCache cluster or a Memorystore instance. Create one before storing anything:
import cachedriver "github.com/stackshy/cloudemu/v2/services/cache/driver"
aws.ElastiCache.CreateCacheInstance(ctx, cachedriver.CacheConfig{
Name: "session-cache",
NodeType: "cache.t3.micro",
})Store, read, and expire keys#
Set takes a TTL — once it elapses the key is gone, which is how you model session timeouts or short-lived caches. A Get on a missing or expired key returns no value:
// Store a value that lives for 30 minutes
aws.ElastiCache.Set(ctx, "session-cache", "user:123", []byte("session-data"), 30*time.Minute)
// Read it back
data, _ := aws.ElastiCache.Get(ctx, "session-cache", "user:123")
// Remove it early
aws.ElastiCache.Delete(ctx, "session-cache", "user:123")To test expiry without an actual 30-minute wait, drive time with the fake clock: advance past the TTL and the next Get sees the key as gone.
Behavior & fidelity#
- TTL expiry is lazy. An item past its TTL is treated as absent on the next read and cleaned up then — so a
Getnever returns stale data, and expiry costs nothing until you touch the key. - Thread-safe. Every operation is safe for concurrent use across goroutines and providers.
- SDK-compat status — Portable Go API only. ElastiCache, Azure Cache for Redis, and Memorystore expose the Redis/Memcached wire protocols, which sit outside cloudemu's HTTP-based SDK-compat scope. The cache management APIs may get SDK-compat handlers in a later phase; the full driver semantics above are available today through the Portable Go API.