Overview
A mechanism to persist calculation and aggregate results as node properties, with a DSL for defining when they are invalidated and recomputed.
Scoped to AshNeo4j initially (cached values stored as Neo4j node properties). If the pattern proves out, the DSL could be extracted into a general Ash extension.
Motivation
Some calculations and aggregates involve multi-hop graph traversal (3-4 hops is common in the diffo NBN model — e.g. NNI Group → CVC → AVC → Characteristic). Running these on every read is expensive. Caching the result as a direct node property makes them available for further aggregation on parent resources without runtime traversal.
Cached calculations
A calculation whose result is persisted to the node and recomputed when declared triggers fire.
neo4j do
cache :bandwidth do
calculation MyApp.Calculations.Bandwidth
invalidate_on MyApp.Characteristic, [:create, :update, :destroy]
via [:characteristics]
end
end
Cached aggregates
An aggregate whose result is persisted to the node and recomputed when declared triggers fire.
neo4j do
cache :cvc_count do
aggregate :count, :cvcs
invalidate_on MyApp.CVC, [:create, :destroy]
via [:cvcs]
end
end
Structure (how)
- Cached value stored as a direct node property in Neo4j
- Declared as a read-only attribute on the resource — not writable by normal changesets
- Written only by the cache mechanism via the data layer
Behaviour DSL (when)
invalidate_on — resource + actions that trigger recomputation
via — relationship path back from the changed resource to the owning resource
- Implemented using Ash notifiers: on action completion, traverse back via the
via path, recompute, write result
Dependencies
Notes
- The invalidation graph maps directly onto the Neo4j graph structure
- Could generalise to other data layers (AshPostgres would store in a column); DSL shape would be the same, only storage and write-back differ
Overview
A mechanism to persist calculation and aggregate results as node properties, with a DSL for defining when they are invalidated and recomputed.
Scoped to AshNeo4j initially (cached values stored as Neo4j node properties). If the pattern proves out, the DSL could be extracted into a general Ash extension.
Motivation
Some calculations and aggregates involve multi-hop graph traversal (3-4 hops is common in the diffo NBN model — e.g. NNI Group → CVC → AVC → Characteristic). Running these on every read is expensive. Caching the result as a direct node property makes them available for further aggregation on parent resources without runtime traversal.
Cached calculations
A calculation whose result is persisted to the node and recomputed when declared triggers fire.
Cached aggregates
An aggregate whose result is persisted to the node and recomputed when declared triggers fire.
Structure (how)
Behaviour DSL (when)
invalidate_on— resource + actions that trigger recomputationvia— relationship path back from the changed resource to the owning resourceviapath, recompute, write resultDependencies
Notes