{"id":21989196,"url":"https://github.com/micropackage/cache","last_synced_at":"2025-04-30T11:33:33.001Z","repository":{"id":46337077,"uuid":"232101379","full_name":"micropackage/cache","owner":"micropackage","description":"WordPress cache wrapper","archived":false,"fork":false,"pushed_at":"2023-06-26T20:24:05.000Z","size":56,"stargazers_count":13,"open_issues_count":0,"forks_count":3,"subscribers_count":3,"default_branch":"develop","last_synced_at":"2025-04-16T19:36:27.482Z","etag":null,"topics":["cache-driver","composer-library","micropackage","wordpress","wordpress-cache"],"latest_commit_sha":null,"homepage":null,"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/micropackage.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-01-06T13:00:18.000Z","updated_at":"2025-01-03T15:21:25.000Z","dependencies_parsed_at":"2022-09-18T23:51:15.084Z","dependency_job_id":null,"html_url":"https://github.com/micropackage/cache","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/micropackage%2Fcache","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/micropackage%2Fcache/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/micropackage%2Fcache/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/micropackage%2Fcache/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/micropackage","download_url":"https://codeload.github.com/micropackage/cache/tar.gz/refs/heads/develop","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":251691654,"owners_count":21628365,"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-driver","composer-library","micropackage","wordpress","wordpress-cache"],"created_at":"2024-11-29T19:28:20.976Z","updated_at":"2025-04-30T11:33:32.973Z","avatar_url":"https://github.com/micropackage.png","language":"PHP","readme":"# Cache\n\n[![BracketSpace Micropackage](https://img.shields.io/badge/BracketSpace-Micropackage-brightgreen)](https://bracketspace.com)\n[![Latest Stable Version](https://poser.pugx.org/micropackage/cache/v/stable)](https://packagist.org/packages/micropackage/cache)\n[![PHP from Packagist](https://img.shields.io/packagist/php-v/micropackage/cache.svg)](https://packagist.org/packages/micropackage/cache)\n[![Total Downloads](https://poser.pugx.org/micropackage/cache/downloads)](https://packagist.org/packages/micropackage/cache)\n[![License](https://poser.pugx.org/micropackage/cache/license)](https://packagist.org/packages/micropackage/cache)\n\n\u003cp align=\"center\"\u003e\n    \u003cimg src=\"https://bracketspace.com/extras/micropackage/micropackage-small.png\" alt=\"Micropackage logo\"/\u003e\n\u003c/p\u003e\n\n## 🧬 About Cache\n\nThis micropackage is a wrapper for WordPress cache with two drivers implemented:\n\n- [WP Object Cache API](https://codex.wordpress.org/Class_Reference/WP_Object_Cache)\n- [Transients API](https://codex.wordpress.org/Transients_API)\n\nIt provides a unified, object-oriented way to manipulate WordPress Cache, witch the Cache manipulator object for even easier setting and getting the cache.\n\n## 💾 Installation\n\n``` bash\ncomposer require micropackage/cache\n```\n\n## 🕹 Usage\n\n### Object Cache\n\nConstructing the Object Cache driver:\n\n```php\nuse Micropackage\\Cache\\Driver\\ObjectCache;\n\n$object_cache = new ObjectCache( $group = 'my_group', $expiration = DAY_IN_SECONDS );\n$object_cache-\u003eset_key( 'cache_key' );\n```\n\nGroup parameter allows you to store the cache under the same key across multiple groups. Default is empty string.\n\nBy default the expiration is set to `0` which means the transient never expires.\n\n#### Available methods\n\n| Method                                    | Description                                                  | Returns                                                      |\n| ----------------------------------------- | ------------------------------------------------------------ | ------------------------------------------------------------ |\n| ```set_key( (string) $key )```            | Sets the cache key                                           | $this                                                        |\n| ```get_key()```                           | Gets the cache key                                           | (string) Cache key                                           |\n| ```set_group( (string) $group )```        | Sets the cache group                                         | $this                                                        |\n| ```get_group```                           | Gets the cache group                                         | (string) Default empty string                                |\n| ```set_expiration( (int) $expiration )``` | Sets the cache expiration in seconds                         | $this                                                        |\n| ```get_expiration()```                    | Gets the cache expiration                                    | (int) Expiration seconds \u003cbr /\u003eDefault 0 which means the cache  \u003cbr /\u003edoesn't expire |\n| ```set( (mixed) $value )```               | Sets the cache                                               | void                                                         |\n| ```add( (mixed) $value )```               | Sets the cache \u003cbr /\u003eonly if it  wasn't set before           | void                                                         |\n| ```get()```                               | Gets the cached value                                        | mixed\\|false\u003cbr /\u003eFalse if not set                           |\n| ```force_get()```                         | Gets the cached value  \u003cbr /\u003eand updates the local cache \u003cbr /\u003efrom persistent cache | mixed\\|false \u003cbr /\u003eFalse if not set                          |\n| ```delete()```                            | Deletes the cache                                            | void                                                         |\n\n### Transient Cache\n\nConstructing the Transient Cache driver:\n\n```php\nuse Micropackage\\Cache\\Driver\\Transient;\n\n$transient_cache = new Transient( $expiration = DAY_IN_SECONDS );\n$transient_cache-\u003eset_key( 'cache_key' );\n```\n\nBy default the expiration is set to `0` which means the transient never expires.\n\n#### Available methods\n\n| Method                                    | Description                                  | Returns                                                      |\n| ----------------------------------------- | -------------------------------------------- | ------------------------------------------------------------ |\n| ```set_key( (string) $key )```            | Sets the cache key                           | $this                                                        |\n| ```get_key()```                           | Gets the cache key                           | (string) Cache key                                           |\n| ```set_expiration( (int) $expiration )``` | Sets the cache expiration in seconds         | $this                                                        |\n| ```get_expiration()```                    | Gets the cache expiration                    | (int) Expiration seconds \u003cbr /\u003eDefault 0 which means the cache \u003cbr /\u003e doesn't expire |\n| ```set( (mixed) $value )```               | Sets the cache                               | void                                                         |\n| ```add( (mixed) $value )```               | Sets the cache only if it  wasn't set before | void                                                         |\n| ```get()```                               | Gets the cached value                        | mixed\\|false \u003cbr /\u003eFalse if not set                          |\n| ```delete()```                            | Deletes the cache                            | void                                                         |\n\n### Cache manipulator\n\nThe Cache manipulator object allows you to use the `collect` method to easily get/store the cache value.\n\nSee the below example with Object Cache (you can pass the Transient Driver as well).\n\n```php\nuse Micropackage\\Cache\\Cache;\nuse Micropackage\\Cache\\Driver\\ObjectCache;\n\n$driver = new ObjectCache( $group = 'my_group', $expiration = DAY_IN_SECONDS );\n$cache  = new Cache( $driver, $cache_key = 'extremaly_important_thing' );\n\n$the_thing = $cache-\u003ecollect( function() {\n\treturn 'The value was not set apparently';\n} );\n```\n\nThe `collect` method takes a callable function as an argument. If the cache wasn't set for the key provided in cache construtor, the callable is called which should return the value for cache. The value is stored and returned.\n\nUsing variables from outside the callable:\n\n```php\n$some_var = 'I am awesome!';\n\n$the_thing = $cache-\u003ecollect( function() use ( $some_var ) {\n\treturn $some_var;\n} );\n```\n\n## 📦 About the Micropackage project\n\nMicropackages - as the name suggests - are micro packages with a tiny bit of reusable code, helpful particularly in WordPress development.\n\nThe aim is to have multiple packages which can be put together to create something bigger by defining only the structure.\n\nMicropackages are maintained by [BracketSpace](https://bracketspace.com).\n\n## 📖 Changelog\n\n[See the changelog file](./CHANGELOG.md).\n\n## 📃 License\n\nThis software is released under MIT license. See the [LICENSE](./LICENSE) file for more information.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmicropackage%2Fcache","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmicropackage%2Fcache","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmicropackage%2Fcache/lists"}