ModelCache
in package
uses
BaseTraits
Defines a ModelCache object. A ModelCache is a collection of Models and ModelSets that have already been loaded and indexed in memory. This class is responsible for ensuring that Model objects are only loaded once. This is distinctly different from the Cache class, which is responsible for caching data to/from disk.
Table of Contents
Properties
- $cache : array<string|int, mixed>
- $index : array<string|int, mixed>
- $instance : self|null
- $__log_level : int
Methods
- cache_modelset() : void
- Caches a ModelSet in the ModelCache by its signature.
- clear() : void
- Clears the ModelCache of all cached ModelSets and indexed Model objects.
- fetch_model() : Model
- Fetches a cached Model object by its Model class name and index field/value.
- fetch_modelset() : ModelSet
- Fetches a cached ModelSet by its Model class name.
- get_class_fqn() : string
- Obtains the fully qualified name of the called class.
- get_class_shortname() : string
- Obtains the shortname of the called class.
- get_classes_in_namespace() : array<string|int, mixed>
- Obtains all classes associated with this class's current namespace.
- get_instance() : self
- Retrieves the singleton instance of the ModelCache.
- has_model() : bool
- Checks if a given Model class has a cached Model object by its index field/value.
- has_modelset() : bool
- Checks if a cached ModelSet exists for a specified signature.
- index_modelset_by_field() : array<string|int, mixed>
- Indexes the ModelSet cache for a given Model class by a specified field. This method will populate the $index array for the specified Model class and index field.
- log() : void
- Writes a log entry to the applicable log file
- ensure_model_supports_indexing() : void
- Raises an error if the given Model object does not support being indexed by the specified field.
Properties
$cache
public
static array<string|int, mixed>
$cache
= []
An associative array that contains cached ModelSet objects that have been loaded into memory. This includes root ModelSets that contain all Model objects of a given Model class as well as indexed queries that have already been run against those ModelSets. Structured as: [ModelSet signature => ModelSet object]. Please note that root ModelSets are indexed by their qualified Model class name only, whereas queried ModelSets are indexed by the root signature followed by a series of query hashes separated by colons.
$index
public
static array<string|int, mixed>
$index
= []
An associative array that contains cache Model objects that have been loaded and indexed by specific fields for quick lookup. Structured as:
[Model class name] => [ [index field name] => [ [index field value] => Model object ] ]
$instance
public
static self|null
$instance
= null
The singleton instance of the ModelCache.
$__log_level
private
static int
$__log_level
Methods
cache_modelset()
Caches a ModelSet in the ModelCache by its signature.
public
static cache_modelset(ModelSet $model_set) : void
Parameters
- $model_set : ModelSet
-
The ModelSet object to cache.
clear()
Clears the ModelCache of all cached ModelSets and indexed Model objects.
public
static clear() : void
fetch_model()
Fetches a cached Model object by its Model class name and index field/value.
public
static fetch_model(string $model_class[, string $index_field = 'id' ][, mixed $index_value = null ]) : Model
Parameters
- $model_class : string
-
The Model class name to load from the cache.
- $index_field : string = 'id'
-
The index field name to look up the Model object by. Only for many enabled Models.
- $index_value : mixed = null
-
The index field value to look up the Model object by. Only for many enabled Models.
Tags
Return values
Model —The cached Model object
fetch_modelset()
Fetches a cached ModelSet by its Model class name.
public
static fetch_modelset(string $signature) : ModelSet
Parameters
- $signature : string
-
The signature of the ModelSet to fetch from the cache.
Tags
Return values
ModelSet —The cached ModelSet
get_class_fqn()
Obtains the fully qualified name of the called class.
public
static get_class_fqn() : string
Return values
string —The FQN for this object's class.
get_class_shortname()
Obtains the shortname of the called class.
public
static get_class_shortname() : string
Return values
string —The shortname for this object's class.
get_classes_in_namespace()
Obtains all classes associated with this class's current namespace.
public
get_classes_in_namespace([bool $shortnames = false ]) : array<string|int, mixed>
Parameters
- $shortnames : bool = false
Return values
array<string|int, mixed> —An array of classes currently in this object's namespace
get_instance()
Retrieves the singleton instance of the ModelCache.
public
static get_instance() : self
Return values
self —The singleton instance of the ModelCache.
has_model()
Checks if a given Model class has a cached Model object by its index field/value.
public
static has_model(string $model_class[, string $index_field = 'id' ][, mixed $index_value = null ]) : bool
Parameters
- $model_class : string
-
The Model class name to check in the cache.
- $index_field : string = 'id'
-
The index field name to look up the Model object by
- $index_value : mixed = null
-
The index field value to look up the Model object by
Return values
bool —True if a cached Model object exists for the specified Model class and index
has_modelset()
Checks if a cached ModelSet exists for a specified signature.
public
static has_modelset(string $signature) : bool
Parameters
- $signature : string
-
The signature of the ModelSet to check for in the cache.
Return values
bool —True if a cached ModelSet exists with the specified signature, false otherwise.
index_modelset_by_field()
Indexes the ModelSet cache for a given Model class by a specified field. This method will populate the $index array for the specified Model class and index field.
public
static index_modelset_by_field(string $model_class, string $index_field) : array<string|int, mixed>
Parameters
- $model_class : string
-
The Model class name whose ModelSet is to be indexed. If no cached ModelSet exists, one will be created by reading all Model objects from the data source.
- $index_field : string
-
The field name to index the Model objects by.
Return values
array<string|int, mixed> —An associative array of indexed Model objects.
log()
Writes a log entry to the applicable log file
public
static log(int $level, string $message[, string $logfile = 'restapi' ]) : void
Parameters
- $level : int
-
The log level to write. This should be one of the LOG_* constants defined by syslog.
- $message : string
-
The message to write to the log file.
- $logfile : string = 'restapi'
-
The log file to write to. This must be a valid logging facility defined in the package's info.xml file.
ensure_model_supports_indexing()
Raises an error if the given Model object does not support being indexed by the specified field.
private
static ensure_model_supports_indexing(Model $model, string $index_field) : void
Parameters
- $model : Model
-
The Model object to check for uniqueness.
- $index_field : string
-
The index field name to check for uniqueness.