PHPackages                             alcidesrc/cache - 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. alcidesrc/cache

ActiveLibrary[Caching](/categories/caching)

alcidesrc/cache
===============

A PHP class that allows to optimize cacheable arrays by packing/unpacking the schema

20PHP

Since Aug 25Pushed 1y ago1 watchersCompare

[ Source](https://github.com/AlcidesRC/cache)[ Packagist](https://packagist.org/packages/alcidesrc/cache)[ RSS](/packages/alcidesrc-cache/feed)WikiDiscussions main Synced yesterday

READMEChangelogDependenciesVersions (1)Used By (0)

[![Continuous Integration](https://github.com/AlcidesRC/cache/actions/workflows/ci.yml/badge.svg)](https://github.com/AlcidesRC/cache/actions/workflows/ci.yml)

Cache Packer
============

[](#cache-packer)

This repository contains a PHP class that allows to optimize the cacheable arrays in order to optimize the required memory consumption.

\[TOC\]

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

[](#installation)

You can install the package via composer:

```
$ composer require alcidesrc/cache
```

How It Works
------------

[](#how-it-works)

In many scenarios we are caching structured information (generally coming from database). Each row from this structure has the same schema than the previous row, and the same schema than the next one.

When serializing this kind of structures prior to cache the column names are stored again and again as many times as rows are contained, consuming resources to track an information is not mutable and predictable at all.

The Packer class brings a static method called `pack` which allows to create a new structure with the following schema:

```
$packed = [
  'keys' => ['column-name-1', 'column-name-2', ..., 'column-name-z'],
  'data' => [
  	[
        'row-1-value-1',
        'row-1-value-2',
        ...
        'rown-1-value-z'
    ],
    [
        'row-n-value-1',
        'row-n-value-2',
        ...
        'rown-n-value-z'
    ],
  ],
];
```

> This optimized schema can now be cached and reduce a 25% of memory consumptions in average for large datasets. See Statistics section for further details.

To revert this schema to the original structure the Packer class also brings a static method called `unpack` which restores the schema to initial stage.

### Example

[](#example)

#### Source Dataset

[](#source-dataset)

```
// $users = DB::table('users')->get()
$users = [
  [
    'id' => 1,
    'firstName' => 'Hope',
    'lastName' => 'Pacocha',
    'email' => 'eblanda@hotmail.com',
    'address' => '1939 Julio Shore. Zboncakland, HI 21531-7243',
    'city' => 'Lethatown',
    'postcode' => '84445-4109',
    'country' => 'Niger',
  ],
  [
    'id' => 2,
    'firstName' => 'Lyric',
    'lastName' => 'Parker',
    'email' => 'bennett.mitchell@balistreri.org',
    'address' => '25071 Jacklyn Dam Suite 215. Lake Alexannemouth, IL 77929-0777',
    'city' => 'Port Yvetteville',
    'postcode' => '00537',
    'country' => 'French Southern Territories',
  ],
];
```

#### Direct Serialization

[](#direct-serialization)

```
a:2:{i:0;a:8:{s:2:"id";i:1;s:9:"firstName";s:4:"Hope";s:8:"lastName";s:7:"Pacocha";s:5:"email";s:19:"eblanda@hotmail.com";s:7:"address";s:43:"1939 Julio Shore
Zboncakland, HI 21531-7243";s:4:"city";s:9:"Lethatown";s:8:"postcode";s:10:"84445-4109";s:7:"country";s:5:"Niger";}i:1;a:8:{s:2:"id";i:2;s:9:"firstName";s:5:"Lyric";s:8:"lastName";s:6:"Parker";s:5:"email";s:31:"bennett.mitchell@balistreri.org";s:7:"address";s:61:"25071 Jacklyn Dam Suite 215
Lake Alexannemouth, IL 77929-0777";s:4:"city";s:16:"Port Yvetteville";s:8:"postcode";s:5:"00537";s:7:"country";s:27:"French Southern Territories";}}
```

#### Serialization with Packer

[](#serialization-with-packer)

```
a:2:{s:4:"keys";a:8:{i:0;s:2:"id";i:1;s:9:"firstName";i:2;s:8:"lastName";i:3;s:5:"email";i:4;s:7:"address";i:5;s:4:"city";i:6;s:8:"postcode";i:7;s:7:"country";}s:4:"data";a:2:{i:0;a:8:{i:0;i:1;i:1;s:4:"Hope";i:2;s:7:"Pacocha";i:3;s:19:"eblanda@hotmail.com";i:4;s:43:"1939 Julio Shore
Zboncakland, HI 21531-7243";i:5;s:9:"Lethatown";i:6;s:10:"84445-4109";i:7;s:5:"Niger";}i:1;a:8:{i:0;i:2;i:1;s:5:"Lyric";i:2;s:6:"Parker";i:3;s:31:"bennett.mitchell@balistreri.org";i:4;s:61:"25071 Jacklyn Dam Suite 215
Lake Alexannemouth, IL 77929-0777";i:5;s:16:"Port Yvetteville";i:6;s:5:"00537";i:7;s:27:"French Southern Territories";}}}
```

##### Unserialization

[](#unserialization)

```
[
  'keys' => ['id', 'firstName', 'lastName', 'email', 'address', 'city', 'postcode', 'country'],
  'data' => [
    [
      1,
      'Hope',
      'Pacocha',
      'eblanda@hotmail.com',
      '1939 Julio Shore. Zboncakland, HI 21531-7243',
      'Lethatown',
      '84445-4109',
      'Niger',
    ],
    [
      2,
      'Lyric',
      'Parker',
      'bennett.mitchell@balistreri.org',
      '25071 Jacklyn Dam Suite 215. Lake Alexannemouth, IL 77929-0777',
      'Port Yvetteville',
      '00537',
      'French Southern Territories',
    ],
  ],
];
```

### Statistics

[](#statistics)

RowsDirect SerializedSerialized with PackerOptimization Percentage92625 bytes2131 bytes18.82 %9928440 bytes21286 bytes25.15 %999289144 bytes215390 bytes25.51 %99992914783 bytes2175029 bytes25.38 %9999929340860 bytes21941106 bytes25.22 %Usage
-----

[](#usage)

This package can be used as a library.

### Example: using the library

[](#example-using-the-library)

```
