{"modulePath":"github.com/GoCodeAlone/workflow","version":"v0.87.0","isLatest":true,"isStandardLibrary":false,"goos":"all","goarch":"all","docs":"# package diffcache\n\nPackage diffcache caches per-resource Diff results so a wfctl invocation that re-runs a Plan against unchanged inputs can skip the (sometimes network-expensive) provider-side Diff call. The cache is purely an amortization optimization, NOT a correctness mechanism — apply paths remain correct on a 100% miss rate (which is exactly what CI sees on every fresh runner).\n\n### Storage backends {#hdr-Storage_backends}\n\nCache selection is driven by the WFCTL\\_DIFFCACHE env var, resolved by [New](#New):\n\n  - \\`disabled\\` → noop cache (every Get misses; Put is a no-op). Use this when an operator wants fully-deterministic Plan/Apply timing with no shared state across invocations.\n  - \\`:memory:\\` → in-memory cache that lives only for the current process. CI workflows in this repo set WFCTL\\_DIFFCACHE=:memory: explicitly so containerized runners never write to disk.\n  - any other value (or unset) → filesystem cache rooted at \\`~/.cache/wfctl/diff/\\`.\n\n### CI ephemerality (load-bearing) {#hdr-CI_ephemerality__load_bearing_}\n\nCI runners are ephemeral — each job starts with an empty cache. Workflow correctness MUST NOT depend on a cache hit. The diff cache is an operator-local performance optimization for repeated \\`wfctl infra plan\\` invocations against the same checkout.\n\n### Cache key {#hdr-Cache_key}\n\nThe cache key is a [Key](#Key) tuple of (PluginVersion, Type, ProviderID, SHAConfig, SHAOutputs). Plugin downgrades naturally invalidate entries since PluginVersion is part of the key — old entries persist on disk until the LRU eviction reclaims them; the size cap (1024 entries / 64 MiB) bounds the disk waste.\n\n### Schema versioning {#hdr-Schema_versioning}\n\nEach cache file embeds \\[cacheSchemaVersion] in a JSON envelope. On Get, a mismatched version is treated identically to file corruption: the entry is silently evicted and the caller re-Diffs.\n\n### Known limitations {#hdr-Known_limitations}\n\nWindows: the filesystem cache uses [os.Rename](/os#Rename) for the atomic publish step on Put. On Windows, [os.Rename](/os#Rename) fails when the destination already exists, so updating an existing cache entry will fail (the entry is treated as a write failure and the operator gets a cache miss on the next Get — correct, since apply does not depend on cache hits). A future improvement is to vendor github.com/google/renameio for cross-platform atomic rename; deferred until there's a Windows-supported wfctl use case.\n\n### T3.5 / W-3a status {#hdr-T3_5___W_3a_status}\n\nThis package ships in W-3a. The consumer that wires it into platform.ComputePlan lands in W-3b/T3.6f. Until then, the cache is callable but never invoked from production code paths.\n\n## Types\n\n```go\ntype Cache interface {\n\t// Get returns the cached DiffResult for key. The boolean is true\n\t// iff the entry was found and successfully decoded; corruption,\n\t// schema-version mismatch, and missing entries all yield\n\t// (zero-value, false).\n\tGet(key Key) (interfaces.DiffResult, bool)\n\t// Put stores result under key. Errors during Put (e.g., disk\n\t// full, serialization failure) are silently swallowed because\n\t// cache misses are correct — apply behavior must not depend on\n\t// Put success.\n\tPut(key Key, result interfaces.DiffResult)\n}\n```\nCache is the diff-result cache. Implementations are safe for concurrent use (the backing fs cache uses os-level atomic file ops; the in-memory cache locks internally).\n\n```go\nfunc New() Cache\n```\nNew returns a Cache configured by the WFCTL\\_DIFFCACHE env var.\n\n  - \"disabled\"  → [NewNoop](#NewNoop)\n  - \":memory:\"  → [NewMemory](#NewMemory)\n  - default     → [NewFilesystem](#NewFilesystem) rooted at the user cache directory (typically \\`~/.cache/wfctl/diff/\\`).\n\nWhen the user cache directory cannot be resolved, falls back to the in-memory cache so the caller still gets a working Cache (the filesystem path being unavailable is the operator's hint that disk caching is off).\n\n```go\nfunc NewFilesystem(dir string) Cache\n```\nNewFilesystem returns a Cache whose entries are persisted under dir. The directory is created on first Put if absent. Callers may pass any directory; the [New](#New) factory uses \\`~/.cache/wfctl/diff/\\`.\n\n```go\nfunc NewMemory() Cache\n```\nNewMemory returns an in-memory Cache. The returned Cache does not enforce a size cap — process lifetime is the eviction horizon. This matches the rev2 lifecycle constraint that the in-memory mode is the CI default and is not relied on for correctness.\n\n```go\nfunc NewNoop() Cache\n```\nNewNoop returns a Cache that always misses. Put is a no-op.\n\n```go\ntype Key struct {\n\t// PluginVersion is the plugin's name@version string, e.g.,\n\t// \"do@v0.10.0\". Plugin downgrades naturally invalidate cache\n\t// entries via this field.\n\tPluginVersion string `json:\"plugin_version\"`\n\t// Type is the canonical resource type, e.g., \"infra.vpc\".\n\tType string `json:\"type\"`\n\t// ProviderID is the resource's cloud-side identifier; empty for\n\t// net-new resources.\n\tProviderID string `json:\"provider_id\"`\n\t// SHAConfig is the sha256-hex of canonical-marshal(spec.Config).\n\tSHAConfig string `json:\"sha_config\"`\n\t// SHAOutputs is the sha256-hex of canonical-marshal(currentState.Outputs);\n\t// empty for net-new resources.\n\tSHAOutputs string `json:\"sha_outputs\"`\n}\n```\nKey tuples the inputs to a single Diff. Two Keys are equal iff every field matches; the canonical sha256 fingerprint of the key determines the on-disk filename.\n\nJSON tags on the fields are for log / transcript serialization only — cache keying uses NUL-separated string concatenation in \\[keyFingerprint], not JSON marshaling. A reader checking the fingerprint shape should follow the keyFingerprint code, not the tags.\n\n","path":"github.com/GoCodeAlone/workflow/iac/diffcache","name":"diffcache","synopsis":"Package diffcache caches per-resource Diff results so a wfctl invocation that re-runs a Plan against unchanged inputs can skip the (sometimes network-expensive) provider-side Diff call.","isRedistributable":true}
