axetools/cachingtrait

A PHP library to enable static caching trait on a class

v3.0.0 2024-07-14 00:56 UTC

This package is auto-updated.

Last update: 2024-10-27 14:32:52 UTC


README

This is a php class trait that will enable static caching on a class.

Source Code PHP Programming Language Read License Build Status

Disclaimer

In general this does not promote good design patters. This is not a good package to include when creating a new project. The power of this package is having the ability to drop it into heavy classes that are difficult to refactor.

This package can greatly reduce execution times for classes that are instantiated in multiple locations that are heavy to create or have multiple database calls on instantiation. Classes that contain data that is not expected to change in the lifetime of a request. Sometimes it is not possible to pass these classes as a dependency when refactoring and an intermediary solution is needed.

WARNING: When writing tests that involve caching classes, remember the static cache is not automatically reset between tests. Care should be taken to ensure clean class caches at the start and end of each test case.

This project uses Semantic Versioning.

Installation

The preferred method of installation is via Composer. Run the following command to install the package and add it as a requirement to your project's composer.json:

composer require axetools/cachingtrait

Usage

The CachingTrait can be used with any class and will expose several protected methods to be utilized by the class to access the static cache array.

Example

<?php
namespace test

use AxeTools\Trait\Caching\CachingTrait;

class HardToBuild {

use CachingTrait;

    /**
     * @param array<string> $parameters Assumes that the parameters alone can uniquely recall the same object.
     */
    public static function create(array $parameters){
        if(self::hasCache(parameters)) return self::getCache($parameters);
        return new self($parameters);
    }
    
}

setCache()

The self::setCache() static method to store data in the internal cache array. There is no protection to stop array keys from being overwritten. If you need to ensure you are not overwriting data utilize the self::hasCache() check to see if there is already data being stored for that key before setting the keys data.

Description

self::setCache(array<string> $key, mixed $data): void

Parameters

key
The string array that will be used to identify the data that is being stored in the cache
data
The data that will be stored in the cache

Return Value

This method has no return

hasCache()

The self::hasCache() static method is used to see if there is already a key value stored in the cache array. Since anything can be stored in the cache array for a given key, only the existence of the give key in the cache array is checked with no consideration to the data stored for the key.

Description

self::hasCache(array<string> $key): bool

Parameters

key
The string array that will be used to check for the data that is being stored in the cache

Return Value

This will return true if the key is found in the cache array and false if not found.

getCache()

The self::getCache() static method is used to retrieve the data stored in the cache assigned to the key specified. If the key is not defined in the cache array a CachingTraitMissingKeyException will be thrown.

Description

self::getCache(array<string> $key): mixed

Parameters

key
The string array that will be used return the data that is being stored in the cache for that key

Return Value

This will return the value stored in the cache for the key provided. If the key is not defined in the cache array a CachingTraitMissingKeyException will be thrown.

clearCache()

Description

self::clearCache(array<string> $key): void

Parameters

key
Optional string array that will be used to clear the data and the key that is stored in the cache. If omitted the entire cache array is cleared.

Return Value

If the key is not defined in the cache array a CachingTraitMissingKeyException will be thrown.