PHPackages                             axetools/cachingtrait - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Caching](/categories/caching)
4. /
5. axetools/cachingtrait

ActiveLibrary[Caching](/categories/caching)

axetools/cachingtrait
=====================

A PHP library to enable static caching trait on a class

v3.0.0(1y ago)011MITPHPPHP ^8.0

Since Jul 12Pushed 1y agoCompare

[ Source](https://github.com/AxeTools/CachingTrait)[ Packagist](https://packagist.org/packages/axetools/cachingtrait)[ RSS](/packages/axetools-cachingtrait/feed)WikiDiscussions 3.x Synced 1mo ago

READMEChangelog (4)Dependencies (3)Versions (8)Used By (0)

AxeTools/CachingTrait
=====================

[](#axetoolscachingtrait)

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

 [![Source Code](https://camo.githubusercontent.com/c06c456546c2ced17015d4529aeba1ae40e19a0011743f4518eccc02a7e1f90f/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f736f757263652d417865546f6f6c732f63616368696e6754726169742d626c75652e7376673f7374796c653d666c61742d737175617265)](https://github.com/AxeTools/CachingTrait) [![PHP Programming Language](https://camo.githubusercontent.com/3ee8f40a29e3e0269e1e3267b45e33a34531cd8ded03b41fab5fdf25faaf7c02/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f7068702d762f417865546f6f6c732f43616368696e6754726169742e7376673f7374796c653d666c61742d73717561726526636f6c6f72423d253233383839324246)](https://php.net) [![Read License](https://camo.githubusercontent.com/eacf78606730f89b6116ae511a50209458c8611f6de7a1ec1290a9b6d7996d87/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f6c2f417865546f6f6c732f43616368696e6754726169742e7376673f7374796c653d666c61742d73717561726526636f6c6f72423d6461726b6379616e)](https://github.com/AxeTools/CachingTrait/blob/3.x/LICENSE) [![Build Status](https://camo.githubusercontent.com/1458a845178f94269149f056a414dbd1737d84d60b5dd96ebd1ca9b0e0a98eab/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f417865546f6f6c732f43616368696e6754726169742f7068702e796d6c3f6272616e63683d312e78266c6f676f3d676974687562267374796c653d666c61742d737175617265)](https://github.com/AxeTools/CachingTrait/actions/workflows/php.yml)

Disclaimer
----------

[](#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](https://semver.org/spec/v2.0.0.html).

Installation
------------

[](#installation)

The preferred method of installation is via [Composer](http://getcomposer.org/). 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
-----

[](#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

[](#example)

```
