Download the PHP package studocu/cacheable-entities without Composer

On this page you can find all versions of the php package studocu/cacheable-entities. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package cacheable-entities

Cacheable entities

Tests Codecov

Cacheable entities is an opinionated infrastructure acts as an abstraction layer to extract away cache-related responsibilities.

Features

Table of contents

Installation

You can install the package via composer:

Backstory

At Studocu we deal with a large stream of data and requests. At some point of our growth we found ourselves drowning in cache keys and caching all over the place. Thus, to bring a uniform approach to caching across our codebase, we cooked up an internally standardized (or as we like to call it, opinionated) infrastructure known as "Cacheable Entities". This infrastructure acts as an abstraction layer to extract away cache-related responsibilities. We isolated this infrastructure into a standalone Laravel package and made it open-source.

Read more about the backstory at <Cacheable Entities: A Laravel package with a story>.

Usage

Defining a cacheable entity

To make a class cacheable entity, it has to implement the StuDocu\CacheableEntities\Contracts\Cacheable contract.

The interface implementation requires defining the following methods:

Accessing a cacheable entity

Caching Strategies

In some cases, you might need to have the same entity cached/accessed differently; Either blocking or non-blocking.

Access

Via cache

To use any of the two caching strategies described above, we have access to two available utility classes: SyncCache and AsyncCache.

⚠️ Important: If you have multi servers infrastructure, and you plan to use a cacheable entity asynchronously, make sure to create and deploy the entity separately first without using asyncCache. Otherwise, you might have class (Job) unserialization errors when deploying. Some regions might be deployed before others.

Example

Without cache

To get a new fresh value of the entity, you can simply call @get on any instance of a cacheable entity. This will compute the value without interacting with the cache.

Serialization/Unserialization

In some cases, you don't want to cache the actual value but rather the metadata of the value, for example, an array of ids. Later, when you access the cache, you would run a query to find records by their ids.

To make a cacheable entity serializable, it has to implement the following contract StuDocu\CacheableEntities\Contracts\SerializableCacheable.

The interface implementation requires defining the following methods:

Example

Corrupt serialized cache value

In some cases, you might find yourself dealing with an invalid cache value when unserializing.

It might be things like:

When this happens, it is beneficial to destroy that cache value and start over. Cacheable entities infrastructure is built with this in mind, to instruct it that the value is corrupt, and it needs to (1) forget it and (2) compute a fresh value, you need to throw the following exception \StuDocu\CacheableEntities\Exceptions\CorruptSerializedCacheValueException from your @unserialize method.

Example,

Caveat when unserializing

Depending on how you serialize your models, you might lose the original order when unserializing, for example, when only caching the IDs. For Entities where the order matters, make sure to retain the original order when unserializing.

Here are some ways of doing so

Purging the cache

Anytime you want to invalidate the cache value of a cacheable entity, you need to use the SyncCache::forget method.

The use of SyncCache for this is because the invalidation happens on the spot.

Here are some examples of how to do so

Async cache default value

When using AsyncCache utility, it will return null if the cached yet. In some cases, you might need to change the default value.

All you need to do is make the cacheable entity implement the following interface StuDocu\CacheableEntities\Contracts\SupportDefaultCacheValue.

The interface implementation requires defining the following method:

Self Cacheable Entities

Normally to access the cache synchronously or asynchronously, we need the intermediate utility classes. However, it might be the case that we need to hide this detail away for convenience.

In this case, we can use the self-cached entities concept.

To make an entity self-cacheable, it has to use the following concern StuDocu\CacheableEntities\Concerns\SelfCacheable

Any class with that trait has access to the following methods:

Example

Generic Annotation

To ensure the safe usage of the cacheable entities in terms of type, the implementation comes with generic templates. Anytime you define a Cacheable entity, it has to also specify its generic, unless you don't have static analysis in place.

Here is an example of specifying the generic for all the contracts and concerns.

Cacheable Generic

This contract accepts one generic definition <TReturn>, which is what the entity will return when calling get to compute its value.

SerializableCacheable

This contract accepts two generic definitions <TUnserialized, TSerialized>

SupportsDefaultValue

This contract accepts one generic definition <TDefault>, which is what the entity will return when missing the cache while using the AsyncCache utility.

Examples

Some examples of cacheable entities were included to learn more on how to:

*The stale cache technique is a way to avoid having any user-facing request hitting a cache miss. You can read more about it and its pros and cons in the blog post.

Changelog

Please see CHANGELOG for more information on what has changed recently.

License

The MIT License (MIT). Please see License File for more information.


All versions of cacheable-entities with dependencies

PHP Build Version
Package Version
Requires php Version ^8.1
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package studocu/cacheable-entities contains the following files

Loading the files please wait ....