Skip to content

cached calculations and aggregates #243

@matt-beanland

Description

@matt-beanland

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions