{"id":18772438,"url":"https://github.com/eftec/cacheone","last_synced_at":"2025-04-13T08:27:31.462Z","repository":{"id":56975533,"uuid":"136766977","full_name":"EFTEC/CacheOne","owner":"EFTEC","description":"CacheOne is a cache of service for php","archived":false,"fork":false,"pushed_at":"2024-12-30T17:35:29.000Z","size":141,"stargazers_count":10,"open_issues_count":0,"forks_count":3,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-27T00:12:06.673Z","etag":null,"topics":["cachemanager","php","php-library","redis"],"latest_commit_sha":null,"homepage":"https://www.escuelainformatica.cl","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/EFTEC.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"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}},"created_at":"2018-06-10T00:14:33.000Z","updated_at":"2024-12-30T17:35:05.000Z","dependencies_parsed_at":"2024-11-07T19:35:25.658Z","dependency_job_id":"4af8648f-aa1b-4fac-a7a2-53a2ee59d514","html_url":"https://github.com/EFTEC/CacheOne","commit_stats":{"total_commits":48,"total_committers":4,"mean_commits":12.0,"dds":0.3125,"last_synced_commit":"8fda7182b5298d921f8014dbf5281fa9f5aa490d"},"previous_names":[],"tags_count":32,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/EFTEC%2FCacheOne","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/EFTEC%2FCacheOne/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/EFTEC%2FCacheOne/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/EFTEC%2FCacheOne/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/EFTEC","download_url":"https://codeload.github.com/EFTEC/CacheOne/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248683356,"owners_count":21144888,"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":["cachemanager","php","php-library","redis"],"created_at":"2024-11-07T19:28:59.564Z","updated_at":"2025-04-13T08:27:31.454Z","avatar_url":"https://github.com/EFTEC.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# CacheOne\r\nCacheOne is a cache class of service for php. It supports Redis, Memcache, PDO and/or APCU.\r\n\r\nUnlikely other cache libraries, this library is based in group (optional). So it's suitable to invalidate a single key \r\nor an entire group of elements.\r\n\r\n\r\n\r\n[![Packagist](https://img.shields.io/packagist/v/eftec/CacheOne.svg)](https://packagist.org/packages/eftec/CacheOne)\r\n[![Total Downloads](https://poser.pugx.org/eftec/CacheOne/downloads)](https://packagist.org/packages/eftec/CacheOne)\r\n[![Maintenance](https://img.shields.io/maintenance/yes/2025.svg)]()\r\n[![composer](https://img.shields.io/badge/composer-%3E1.6-blue.svg)]()\r\n[![php](https://img.shields.io/badge/php-7.4-green.svg)]()\r\n[![php](https://img.shields.io/badge/php-8.3-green.svg)]()\r\n[![CocoaPods](https://img.shields.io/badge/docs-70%25-yellow.svg)]()\r\n\r\n\r\n# Example\r\n\r\n```php\r\nuse eftec\\CacheOne;\r\ninclude \"vendor/autoload.php\"; // composer's autoload\r\n$cache=new CacheOne(\"redis\",\"127.0.0.1\",\"\",6379);\r\n\r\n$cacheValue=$cache-\u003eget('','countries'); // read the cache (if any) otherwise false\r\nif($cacheValue===false) {\r\n    echo \"generating a new list of countries..\u003cbr\u003e\";\r\n    $countries=['USA','Canada','Japan','Chile'];\r\n    $cache-\u003eset('','countries',$countries,500); // store into the cache for 500 seconds.\r\n} else {\r\n    echo \"read from cache\u003cbr\u003e\";\r\n    $countries=$cacheValue;\r\n}\r\nvar_dump($countries);\r\n```\r\n\r\n# Table of Contents\r\n\r\n\u003c!-- TOC --\u003e\r\n* [CacheOne](#cacheone)\r\n* [Example](#example)\r\n* [Table of Contents](#table-of-contents)\r\n* [Definitions](#definitions)\r\n  * [Creating a new instance of CacheOne](#creating-a-new-instance-of-cacheone)\r\n  * [Storing a value](#storing-a-value)\r\n  * [Getting a value](#getting-a-value)\r\n  * [setDefaultTTL](#setdefaultttl)\r\n  * [Pushing and Popping values form an array](#pushing-and-popping-values-form-an-array)\r\n    * [push](#push)\r\n    * [unshift](#unshift)\r\n    * [pop](#pop)\r\n    * [shift](#shift)\r\n  * [invalidate a key](#invalidate-a-key)\r\n  * [invalidate a group](#invalidate-a-group)\r\n  * [invalidate all](#invalidate-all)\r\n  * [setSerializer($serializer)](#setserializerserializer)\r\n  * [getSerializer();](#getserializer)\r\n  * [Select a database (Redis/PdoOne)](#select-a-database-redispdoone)\r\n* [CLI](#cli)\r\n    * [Example REDIS](#example-redis)\r\n* [Version](#version)\r\n* [License](#license)\r\n\u003c!-- TOC --\u003e\r\n\r\n# Definitions\r\n\r\n\r\n## Creating a new instance of CacheOne\r\n\r\nCreates a new connection using Redis (Redis is an on memory cache library)\r\n\r\n```php\r\nuse eftec\\CacheOne;\r\ninclude \"../vendor/autoload.php\";\r\n$cache=new CacheOne(\"redis\",\"127.0.0.1\",\"\",6379);\r\n```\r\n\r\nCreates a new connection using apcu (APCU is an extension for PHP to cache content)\r\n\r\n```php\r\nuse eftec\\CacheOne;\r\ninclude \"../vendor/autoload.php\";\r\n$cache=new CacheOne(\"apcu\");\r\n```\r\n\r\nCreates a new connection using **PdoOne** (PdoOne is a library to connect to the database using PDO)\r\n\r\n```php\r\nuse eftec\\PdoOne;\r\nuse eftec\\CacheOne;\r\ninclude \"../vendor/autoload.php\";\r\n\r\n$pdo=new PdoOne('mysql','127.0.0.1','root','abc.123','travisdb',false,null,1,'KVTABLE');\r\n$pdo-\u003elogLevel=3; // optional, if you want to debug the errors. \r\n$pdo-\u003eopen();\r\n// $pdo-\u003ecreateTableKV();  // you should create the key-value table if it doesn't exist.\r\n$cache=new CacheOne(\"pdoone\"); // the instance $pdo is injected automatically into CacheOne.\r\n```\r\nor you could use to create a PdoOne instance:\r\n```php\r\n$cache=new CacheOne(\r\n  \"pdoone\",\r\n  ['mysql','127.0.0.1','root','abc.123','travisdb',false,null,1,'KVTABLA']\r\n  ); \r\n```\r\n\r\nCreates a new connection using memcache (Memcache is an old, but it is still a functional memory cache server)\r\n\r\n```php\r\nuse eftec\\CacheOne;\r\ninclude \"../vendor/autoload.php\";\r\n$cache=new CacheOne(\"memcache\",\"127.0.0.1\"); // minimum configuration\r\n$cache=new CacheOne(\"memcache\",\"127.0.0.1\",11211,'schema'); // complete configuration\r\n```\r\n\r\nCreates a new connection using the class **DocumentOne** (file system)\r\n\r\nThis example requires the library **eftec/documentstoreone**\r\n\r\n```php\r\nuse eftec\\CacheOne;\r\ninclude \"../vendor/autoload.php\";\r\n$cache=new CacheOne(\"documentone\",__DIR__.\"/base\",\"schema\"); // folder /base/schema must exists\r\n```\r\n\r\nThe library **DocumentStoreOne** works with concurrency.\r\n\r\nor creating a new connection, using redis, memcache, apcu or documentone (it takes the first available)\r\n\r\n```php\r\nuse eftec\\CacheOne;\r\ninclude \"../vendor/autoload.php\";\r\n$cache=new CacheOne(\"auto\");\r\n```\r\n\r\n\r\n## Storing a value\r\n\r\n\u003e function set($group, $key, $value, $duration = 1440): bool\r\n\r\nIt stores a value inside a group and a key.\r\nIt returns false if the operation failed.\r\n\r\n\u003e Note: The duration is ignored by \"documentone\"\r\n\r\n```php\r\n$cache-\u003eset(\"group\",\"key1\",\"hello world\",500);\r\n$cache-\u003eset(\"group\",\"key2\",\"hola mundo\",500);\r\n```\r\nGroup is optional, and it could be used if we need to invalidate (delete) an entire group.\r\n\r\n## Getting a value\r\n\r\n\u003e function get($group, $key, $defaultValue = false)\r\n\r\nIt gets a value stored in a group (optional) and key. If the\r\nvalue is not found then it returns false. Note: a false value could be a valid value.\r\n\r\n```php\r\n$result=$cache-\u003eget(\"group\",\"key1\");\r\n$result=$cache-\u003eget(\"\",\"key2\");\r\n$result=$cache-\u003eget(\"\",\"key2\",\"not found\"); // if not key2 (groupless) then it returns not found \r\n```\r\n## setDefaultTTL\r\n\r\n```php\r\n$result=$cache-\u003esetDefaultTTL(50); // it sets the default time to live. \"documentone\" one uses it.\r\n$result=$cache-\u003egetDefaultTTL();   // it gets the time to live\r\n```\r\n\r\n## Pushing and Popping values form an array\r\n\r\n### push\r\n\r\nIt pushes (adds) a new value at the **end of the array**.  If the array does not exist, then it is created a new array. This command allows to limit the numbers of elements of the array.\r\n\r\nSyntax:\r\n\u003e push($groups, $key, $value, $duration = null, $limit = 0, $limitStrategy = 'shiftold') : bool\r\n\r\n* **$Limit** is used to limit the number maximum of elements of the array, if zero, then it does not limit the elements.\r\n* **$LimitStrategy** is used to determine what to do when we are adding a new element and the limit has been reached, \r\n  **shiftold** removes the first element of the array, **nonew** does not allow to add a new element, **popold** removes the latest element of the array\r\n  (if the limit has been reached).\r\n\r\n```php\r\n// cart could be [1,2,3]\r\n$cache-\u003epush('','cart',4,2000); // it adds a new element into the cart unlimitely cart is [1,2,3,4]\r\n$cache-\u003epush('','cart',5,2000,4,'shiftold'); // it limits the cart to 20 elements, pop old item if req. cart is [2,3,4,5]\r\n$cache-\u003epush('','cart',6,2000,4,'nonew'); // if the cart has 20 elements, then it doesn't add $item. cart now is [2,3,4,5]\r\n```\r\n\r\n### unshift\r\n\r\nIt unshift (add) a new value at the **beginner of the array**.  If the array does not exist, then it is created a new array. This command allows to limit the numbers of elements of the array.\r\n\r\nSyntax:\r\n\r\n\u003e unshift($groups, $key, $value, $duration = null, $limit = 0, $limitStrategy = 'popold') : bool\r\n\r\n* **$Limit** is used to limit the number maximum of elements of the array, if zero, then it does not limit the elements.\r\n* **$LimitStrategy** is used to determine what to do when we are adding a new element and the limit has been reached, **shiftold** removes the first element of the array, **nonew** does not allow to add a new element, **popold** removes the latest element of the array\r\n  (if the limit has been reached).\r\n\r\n```php\r\n// cart could be [1,2,3]\r\n$cache-\u003eunshift('','cart',4,2000); // it adds a new element into the cart unlimitely cart is [4,1,2,3]\r\n$cache-\u003eunshift('','cart',5,2000,4,'shiftold'); // it limits the cart to 20 elements, pop old item if req. cart is [2,3,4,5]\r\n$cache-\u003eunshift('','cart',6,2000,4,'nonew'); // if the cart has 20 elements, then it doesn't add $item. cart now is [2,3,4,5]\r\n```\r\n\r\n\r\n\r\n### pop\r\n\r\nIt pops (extract) a value at the **end of the array**. If the value does not exist then it returns **$defaultValue** The original array is modified removing the last element of the array.\r\n\r\nSyntax:\r\n\r\n\u003e  pop($group, $key, $defaultValue = false, $duration = null) : mixed\r\n\r\n```php\r\n// cart could be [1,2,3,4];\r\n$element=$this-\u003epop('','cart'); // now cart is [1,2,3] and $element is 4\r\n```\r\n\r\n### shift\r\n\r\nIt shifts (extract) a value at the **beginner of the array**. If the value does not exist then it returns **$defaultValue** The original array is modified removing the last element of the array.\r\n\r\nSyntax:\r\n\r\n\u003e  pop($group, $key, $defaultValue = false, $duration = null) : mixed\r\n\r\n```php\r\n// cart could be [1,2,3,4];\r\n$element=$this-\u003eshift('','cart'); // now cart is [2,3,4] and $element is 1\r\n```\r\n\r\n\r\n\r\n## invalidate a key\r\n\r\n\u003e function invalidate($group = '', $key = ''): bool \r\n\r\nIt invalidates a specific key. If the operation fails, then it returns false\r\n\r\n```php\r\n$cache-\u003einvalidate(\"group\",\"key1\"); // invalidate a key inside a group\r\n$cache-\u003einvalidate(\"\",\"key1\"); // invalidate a key without a group.\r\n```\r\n\r\n\r\n## invalidate a group\r\n\r\n\u003e invalidateGroup($group): bool\r\n\r\nIt invalidates every key(s) inside a group of groups.  It also cleans the catalog of the group and sets it to an empty array.\r\n\r\n```php\r\n$cache-\u003einvalidateGroup(\"group\"); // invalidate all keys inside group\r\n$cache-\u003einvalidateGroup([\"group1\",\"group2\"]); // invalidate all key inside group1 and group2\r\n```\r\n\r\n## invalidate all\r\n\r\n\u003e invalidateAll()\r\n\r\nIt invalidates all cache.\r\n\r\nIn **redis**, it deletes the current schema. If not schema, then it deletes all Redis\r\n\r\nIn **memcached** and \u003cb\u003eapcu\u003c/b\u003e, it deletes all cache\r\n\r\nIn \u003cb\u003edocumentone\u003c/b\u003e it deletes the content of the database-folder\r\n\r\n```php\r\n$cache-\u003einvalidateAll(); \r\n```\r\n\r\n## setSerializer($serializer)\r\n\r\nIt sets how the values are serialized.  By default, it's PHP.\r\n\r\n```php\r\n$cache-\u003esetSerializer('php'); // set the serializer to php (default value). It is fastest but it uses more space.\r\n$cache-\u003esetSerializer('json-array'); // set the serializer to json-array. It uses fewer space than PHP however it is a bit slower.\r\n$cache-\u003esetSerializer('json-object'); // set the serializer to json-object.\r\n$cache-\u003esetSerializer('none'); // set the serializer to none (the value must be serialized)\r\n \r\n```\r\n\r\n## getSerializer();\r\n\r\nGet then how the values are serialized.\r\n\r\n```php\r\n$type=$cache-\u003egetSerializer(); // get php,json-array,json-object or none\r\n```\r\n\r\n## Select a database (Redis/PdoOne)\r\n\r\n\u003e  select($dbindex) \r\n\r\nIt selects a different database. By default, the database is 0.\r\n\r\n```php\r\n$cache-\u003eselect(1);\r\n$cache-\u003eselect('table'); // PdoOne\r\n```\r\n\r\n# CLI\r\nThis library also has an interactive CLI.In this CLI, you can create the configuration, test it, load and save.\r\n![example/cli1.jpg](example/cli1.jpg)\r\n\r\nTo open it, you must run the next script:\r\n\r\n```shell\r\nphp vendor/bin/cacheonecli\r\n# or (windows)\r\nvendor/bin/cacheonecli.bat\r\n```\r\n\r\n\r\n### Example REDIS\r\n* Open the CLI\r\n* go to menu cache\r\n* select redis\r\n* select yes\r\n* and select the default information\r\n* Once it is done, select test to test the connection\r\n\r\n![example/cliredis.jpg](example/cliredis.jpg)\r\n\r\n* and finally, save the configuration (select save)\r\n* select yes\r\n* select the filename (c1 in this example)\r\n\r\n![example/cliredis2.jpg](example/cliredis2.jpg)\r\n\r\nThe configuration will be saved as: **c1.config.php**.  \r\nIt is a configuration file that you can include or copy and paste in your code\r\n\r\n```php\r\n\u003c?php // eftec/CliOne(1.28) PHP configuration file (date gen: 2023-04-08 10:19). DO NOT EDIT THIS FILE \r\n/**\r\n * It is a configuration file\r\n */\r\n$cacheOneConfig=[\r\n    'type' =\u003e 'redis',\r\n    'cacheserver' =\u003e '127.0.0.1',\r\n    'schema' =\u003e '',\r\n    'port' =\u003e '6379',\r\n    'user' =\u003e NULL,\r\n    'password' =\u003e NULL,\r\n];\r\n```\r\nexample\r\n```php\r\ninclude \"c1.config.php\";\r\n$cache=CacheOne::factory($cacheOneConfig); \r\n```\r\n\r\n\r\n# Version\r\n* 2.20 (2024-12-30)\r\n  * now compatible with php 8.4 without warnings. \r\n* 2.19 (2024-12-08)\r\n  * [apcu] solved a problem with get() and the value is not found. It returned false instead of the default value. \r\n* 2.18 (2024-03-02)\r\n  * Updating dependency to PHP 7.4. The extended support of PHP 7.2 ended 3 years ago.\r\n  * Added more type hinting in the code.\r\n* 2.17\r\n  * [composer.json] updated dependency\r\n  * [CacheOneCli] now loads a PdoOneCli instance if avaible\r\n* 2.16.1 2023-04-08\r\n  * [composer.json] added cacheonecli to the /bin\r\n  * [CacheOne] added method factory()\r\n  * [CacheOneCli] configuration now uses the variable $cacheOneConfig\r\n* 2.16 2023-04-08\r\n  * [CacheOneCli] (1.3) This version breaks dependency with PdoOne. Now, it is optional. \r\n* 2.15 2023-04-07\r\n  * [CacheOneCli] Updated to 1.2 \r\n  * [CacheOne] updated the constructor, so it allows to pass the configuration of PdoOne as an array\r\n* 2.14\r\n  * updated dependencies. \r\n* 2.13\r\n  * **[redis]** fixed a problem with redis and get() \r\n* 2.12.4\r\n  * **[fixed]** solved a problem when the cache is not found.\r\n* 2.12.3\r\n  * **[fixed]** solved a problem with invalidateCache() \r\n* 2.12 2022-06-12\r\n  * **[fixed]** CacheOne now it could be injected correctly in any case.\r\n  * **[new]** **[redis]**  In Redis, the $schema is used to set the database (if numeric), or to prefix the values.\r\n* 2.11 2022-03-20\r\n  * **[fixed]** added more type \"hinting\" (type validation) \r\n  * **[new]** It allows to obtain an instance (if any) of CacheOne using the static method CacheOne::instance()\r\n  * **[new]** It allows to obtain an instance of the provider (PdoOne, DocumentStoreOne, Redis, Memcache) $this-\u003egetInstanceProvider()\r\n* 2.10 2022-03-15\r\n  * **[new]** method getRenew() that get a value and renews its duration.\r\n  * **[update]** Provider DocumentOne now works with TTL.\r\n* 2.9 2022-03-12\r\n  * Cleared some references and added type hinting to the code. \r\n  * In Redis: invalidateAll() does not delete all the server if there is a schema.\r\n  * in PdoOne: set() does not crash in PHP 8.1 if the catalog is null.\r\n* 2.8 2022-02-10\r\n  * Added a new provider PdoOne (Pdo / Database).\r\n  * Dropped support for PHP 7.1 and lower. If you want to use an old version of this library then you can stay with the version 2.7.\r\n- 2.7 2021-06-13\r\n  * method get() used by provider, never needed the family/group, so it is removed. It is the last version for 2.7\r\n- 2.6.1 2021-06-12\r\n  * changed dependencies in composer.json\r\n- 2.6 2021-06-12\r\n    - added the methods push(), pop(), shift() and unshift()\r\n- 2.5 2020-09-20\r\n    * Separated provider in different classes. Now it also allows to use the file system (**DocumentOne**).   \r\n- 2.4 2020-09-13\r\n    * The code was refactored.   \r\n- 2.3.1\r\n    * fix: The catalog is always stored as an array, even if the serializer is json-object\r\n- 2.3\r\n    * Added method setSerializer() and getSerializer(). By default, CacheOne uses PHP for serialization.\r\n    With this feature, it is possible to serialize using json or none\r\n- 2.2.2 2020-03-13\r\n    * Now the duration of the catalog is always laster than the duration of the key\r\n    * Tested the duration and expiration of the cache.\r\n    * phpunit now is part of \"require-dev\" instead of \"require\"\r\n- 2.2.1 2020-03-12\r\n    * Internal: key names are not store inside the group. The group is store inside the schema\r\n    * Internal: The catalog has a duration defined by $cache-\u003ecatDuration (seconds)\r\n- 2.2 2020-03-12\r\n    * wrappers getCache(),setCache(),invalidateCache()\r\n- 2.1 2020-03-12\r\n    * Unit test\r\n    * get() has a default value $defaultValue\r\n    * new method invalidateAll()\r\n- 2.0 2020-03-12 Updated the whole class. Now it works as a single class.\r\n- 1.4.2 2019-07-30 Added select() to select a different database index. It also adds timeout for the constructor\r\n- 1.4.1 2018-08-15 Added an internal function that obtains the id.\r\n- 1.4   2018-09-05 Fixed the groups\r\n- 1.3.1 2018-06-09 First published version\r\n\r\n# License\r\n\r\nDual license, Commercial and MIT License. Copyright Jorge Castro Castillo\r\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Feftec%2Fcacheone","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Feftec%2Fcacheone","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Feftec%2Fcacheone/lists"}