{"id":18176667,"url":"https://github.com/AlcidesRC/cache","last_synced_at":"2025-04-01T18:31:18.326Z","repository":{"id":224027129,"uuid":"762202145","full_name":"AlcidesRC/cache","owner":"AlcidesRC","description":"A PHP class that allows to optimize cacheable arrays by packing/unpacking the schema","archived":false,"fork":false,"pushed_at":"2024-08-25T14:38:56.000Z","size":85,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-30T11:33:18.306Z","etag":null,"topics":["cache","packer","php8"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/AlcidesRC.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":["AlcidesRC"],"custom":["http://www.paypal.me/AlcidesRC"]}},"created_at":"2024-02-23T09:40:16.000Z","updated_at":"2024-08-25T14:38:58.000Z","dependencies_parsed_at":"2024-08-25T13:25:34.843Z","dependency_job_id":"7161475d-813c-4e74-a319-775c15e5b64f","html_url":"https://github.com/AlcidesRC/cache","commit_stats":null,"previous_names":["fonil/cache","alcidesrc/cache"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AlcidesRC%2Fcache","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AlcidesRC%2Fcache/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AlcidesRC%2Fcache/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/AlcidesRC%2Fcache/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/AlcidesRC","download_url":"https://codeload.github.com/AlcidesRC/cache/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246691510,"owners_count":20818524,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cache","packer","php8"],"created_at":"2024-11-02T17:11:05.025Z","updated_at":"2025-04-01T18:31:18.282Z","avatar_url":"https://github.com/AlcidesRC.png","language":"PHP","readme":"[![Continuous Integration](https://github.com/AlcidesRC/cache/actions/workflows/ci.yml/badge.svg)](https://github.com/AlcidesRC/cache/actions/workflows/ci.yml)\n\n# Cache Packer\n\nThis repository contains a PHP class that allows to optimize the cacheable arrays in order to optimize the required memory consumption.\n\n[TOC]\n\n## Installation\n\nYou can install the package via composer:\n\n```bash\n$ composer require alcidesrc/cache\n```\n\n## How It Works\n\nIn 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.\n\nWhen 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. \n\nThe Packer class brings a static method called `pack` which allows to create a new structure with the following schema:\n\n```php\n$packed = [\n  'keys' =\u003e ['column-name-1', 'column-name-2', ..., 'column-name-z'],\n  'data' =\u003e [\n  \t[\n        'row-1-value-1', \n        'row-1-value-2', \n        ... \n        'rown-1-value-z'\n    ],\n    [\n        'row-n-value-1', \n        'row-n-value-2', \n        ... \n        'rown-n-value-z'\n    ],\n  ],\n];\n```\n\n\u003e 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.\n\nTo revert this schema to the original structure the Packer class also brings a static method called `unpack` which restores the schema to initial stage.\n\n### Example\n\n#### Source Dataset\n\n```php\n// $users = DB::table('users')-\u003eget()\n$users = [\n  [\n    'id' =\u003e 1,\n    'firstName' =\u003e 'Hope',\n    'lastName' =\u003e 'Pacocha',\n    'email' =\u003e 'eblanda@hotmail.com',\n    'address' =\u003e '1939 Julio Shore. Zboncakland, HI 21531-7243',\n    'city' =\u003e 'Lethatown',\n    'postcode' =\u003e '84445-4109',\n    'country' =\u003e 'Niger',\n  ],\n  [\n    'id' =\u003e 2,\n    'firstName' =\u003e 'Lyric',\n    'lastName' =\u003e 'Parker',\n    'email' =\u003e 'bennett.mitchell@balistreri.org',\n    'address' =\u003e '25071 Jacklyn Dam Suite 215. Lake Alexannemouth, IL 77929-0777',\n    'city' =\u003e 'Port Yvetteville',\n    'postcode' =\u003e '00537',\n    'country' =\u003e 'French Southern Territories',\n  ],\n];\n```\n\n#### Direct Serialization\n\n```php\na: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\nZboncakland, 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\nLake 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\";}}\n```\n\n#### Serialization with Packer\n\n```php\na: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\nZboncakland, 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\nLake Alexannemouth, IL 77929-0777\";i:5;s:16:\"Port Yvetteville\";i:6;s:5:\"00537\";i:7;s:27:\"French Southern Territories\";}}}\n```\n\n##### Unserialization\n\n```php\n[\n  'keys' =\u003e ['id', 'firstName', 'lastName', 'email', 'address', 'city', 'postcode', 'country'],\n  'data' =\u003e [\n    [\n      1,\n      'Hope',\n      'Pacocha',\n      'eblanda@hotmail.com',\n      '1939 Julio Shore. Zboncakland, HI 21531-7243',\n      'Lethatown',\n      '84445-4109',\n      'Niger',\n    ],\n    [ \n      2,\n      'Lyric',\n      'Parker',\n      'bennett.mitchell@balistreri.org',\n      '25071 Jacklyn Dam Suite 215. Lake Alexannemouth, IL 77929-0777',\n      'Port Yvetteville',\n      '00537',\n      'French Southern Territories',\n    ],\n  ],\n];\n```\n\n### Statistics\n\n| Rows  | Direct Serialized | Serialized with Packer | Optimization Percentage |\n| ----- | ----------------- | ---------------------- | ----------------------- |\n| 9     | 2625 bytes        | 2131 bytes             | 18.82 %                 |\n| 99    | 28440 bytes       | 21286 bytes            | 25.15 %                 |\n| 999   | 289144 bytes      | 215390 bytes           | 25.51 %                 |\n| 9999  | 2914783 bytes     | 2175029 bytes          | 25.38 %                 |\n| 99999 | 29340860 bytes    | 21941106 bytes         | 25.22 %                 |\n\n## Usage\n\nThis package can be used as a library.\n\n### Example: using the library\n\n```php\n\u003c?php\n\nuse Cache\\Packer;\n\n// Reduce by 25% memory consuptions in average by using Packer\n$users = Packer::unpack(cache()-\u003eremember('users', 300, function () {\n    return Packer::pack(\n        DB::table('users')-\u003eget()\n    );\n}));\n```\n\n## Testing\n\nYou can run the test suite via composer:\n\n```\n$ composer tests\n```\n\n### Unit Tests\n\nThis library provides a [PHPUnit](https://phpunit.de/) testsuite with **12 unit tests** and **22 assertions**:\n\n```bash\nTime: 00:01.038, Memory: 20.00 MB\n\nOK (12 tests, 22 assertions)\n```\n\n### Code Coverage\n\nCode Coverage report summary:\n\n```\nCode Coverage Report:     \n  2024-08-25 12:03:18     \n                          \n Summary:                 \n  Classes: 100.00% (1/1)  \n  Methods: 100.00% (2/2)  \n  Lines:   100.00% (23/23)\n\nCache\\Exceptions\\WrongPackerSchemaException\n  Methods:  ( 0/ 0)   Lines:  (  0/  0)\nCache\\Packer\n  Methods: 100.00% ( 2/ 2)   Lines: 100.00% ( 23/ 23)\n```\n\n## QA\n\n### Static Analyzer\n\nYou can check this library with [PHPStan](https://phpstan.org/):\n\n```\n$ composer analyse\n```\n\nThis command generates the following report:\n\n```bash\n\u003e phpstan analyse --configuration=phpstan.neon --memory-limit 1G --ansi\n 3/3 [▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓] 100%\n\n[OK] No errors\n```\n\n### Checking the Coding Style\n\nYou can check this library with [PHP_CodeSniffer ](https://github.com/squizlabs/PHP_CodeSniffer):\n\n```bash\n$ composer check-style\n```\n\nThis command generates the following report:\n\n```bash\n\u003e phpcs -p --standard=PSR12 --exclude=Generic.Files.LineLength --runtime-set ignore_errors_on_exit 1 --runtime-set ignore_warnings_on_exit 1 src tests\n... 3 / 3 (100%)\n\nTime: 49ms; Memory: 8MB\n```\n\n## Security Vulnerabilities\n\nPlease review our security policy on how to report security vulnerabilities:\n\n\u003e **PLEASE DON'T DISCLOSE SECURITY-RELATED ISSUES PUBLICLY**\n\n### Supported Versions\n\nOnly the latest major version receives security fixes.\n\n### Reporting a Vulnerability\n\nIf you discover a security vulnerability within this project, please [open an issue here](https://github.com/alcidesrc/cache/issues). All security vulnerabilities will be promptly addressed.\n\n## License\n\nThe MIT License (MIT). Please see [License File](https://github.com/alcidesrc/cache/blob/main/LICENSE) for more information.\n","funding_links":["https://github.com/sponsors/AlcidesRC","http://www.paypal.me/AlcidesRC"],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FAlcidesRC%2Fcache","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FAlcidesRC%2Fcache","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FAlcidesRC%2Fcache/lists"}