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 | SDK-compat | Driver |
|---|---|---|---|
| AWS | ElastiCache | ✓ Live | aws.ElastiCache |
| Azure | Cache for Redis | ✓ Live | azure.Cache |
| GCP | Memorystore | ✓ Live | 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.CreateCache(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 — Get returns *Item; the payload is item.Value
item, _ := aws.ElastiCache.Get(ctx, "session-cache", "user:123")
_ = item.Value
// 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#
| Behavior | What happens |
|---|---|
| Full instance and data surface | Beyond store/read/delete, the driver covers instance management, key enumeration, TTL management, and atomic counters — enough for rate-limit buckets and hot-key patterns. |
| Lazy TTL expiry | An item past its TTL reads as absent and is cleaned up then, so a Get never returns stale data and expiry costs nothing until you touch the key. |
| AWS-only optional capabilities | ElastiCache adds subnet groups and replication groups (a primary plus replicas behind one endpoint); Azure and GCP don't implement them. |
| Thread-safe | Every operation is safe for concurrent use across goroutines and providers. |
SDK-compat — Live#
The management API is served — real elasticache, armredis, and Memorystore admin clients drive cache-cluster, subnet-group, and replication-group lifecycle end-to-end. See SDK-Compat for the full operation list.
The cache data plane (the Redis/Memcached GET/SET wire protocol) is TCP, not HTTP, so it stays on the Portable Go API — Set, Get, Incr, Expire, and the rest.