samrap/acf-fluent

Cache internal classes where appropriate.

samrap opened this issue · 1 comments

The package creates new instances of the following objects on every call:

  • Builder
  • Runner
  • Behavior (Field or SubField)

That means that 3 objects are created for every ACF call. Where appropriate, these classes should be cached and reused. The Builder acts as a container for the "queries", so it is not safe to cache this object. But the Runner and the behaviors should absolutely be instantiated no more than once.

A cache container should be created to store instances of each cacheable object to reduce the amount of resources used.

Although this would reduce the number of class instances created, some obvious bugs could occur.

Consider the following:

$title = Acf::field('title');
$button = Acf::subField('button');

echo $title->get();

By storing a single instance of a Runner for reuse, by the time $title->get() is called, the Behavior set on the Runner will be a SubFieldBehavior due to the Acf::subField call. The $title Builder is intended to get a field, not a sub field, which will yield wrong results. While it is not intended to use ACF Fluent in this way, this is a strange bug that is likely to occur. A more complex system could be used but is most likely overkill.

If performance becomes an issue later on, I will revisit this.