{"id":18841272,"url":"https://github.com/dmhendricks/wordpress-object-cache-helper","last_synced_at":"2025-07-01T01:33:10.875Z","repository":{"id":88694432,"uuid":"163130660","full_name":"dmhendricks/wordpress-object-cache-helper","owner":"dmhendricks","description":"A simple MU plugin for WordPress that acts as a wrapper for WP Object Cache functions, with support for flushing cache by group.","archived":false,"fork":false,"pushed_at":"2019-01-01T03:21:11.000Z","size":15,"stargazers_count":0,"open_issues_count":0,"forks_count":1,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-02-15T12:56:34.481Z","etag":null,"topics":["mu-plugin","object-cache","wordpress"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dmhendricks.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-12-26T03:11:08.000Z","updated_at":"2021-08-08T09:46:16.000Z","dependencies_parsed_at":null,"dependency_job_id":"090dfa6a-58db-4c6a-9b16-5694437bd07d","html_url":"https://github.com/dmhendricks/wordpress-object-cache-helper","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dmhendricks%2Fwordpress-object-cache-helper","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dmhendricks%2Fwordpress-object-cache-helper/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dmhendricks%2Fwordpress-object-cache-helper/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dmhendricks%2Fwordpress-object-cache-helper/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dmhendricks","download_url":"https://codeload.github.com/dmhendricks/wordpress-object-cache-helper/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":239780122,"owners_count":19695734,"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":["mu-plugin","object-cache","wordpress"],"created_at":"2024-11-08T02:50:37.866Z","updated_at":"2025-02-20T04:41:48.802Z","avatar_url":"https://github.com/dmhendricks.png","language":"PHP","readme":"[![Author](https://img.shields.io/badge/author-Daniel%20M.%20Hendricks-lightgrey.svg?colorB=9900cc\u0026style=flat-square)](https://www.danhendricks.com/?utm_source=github.com\u0026utm_medium=campaign\u0026utm_content=button\u0026utm_campaign=wordpress-object-cache-helper)\n[![GitHub License](https://img.shields.io/badge/license-GPLv2-yellow.svg?style=flat-square)](https://raw.githubusercontent.com/dmhendricks/wordpress-object-cache-helper/master/LICENSE)\n[![Analytics](https://ga-beacon.appspot.com/UA-67333102-2/dmhendricks/wordpress-object-cache-helper?flat)](https://github.com/igrigorik/ga-beacon/?utm_source=github.com\u0026utm_medium=referral\u0026utm_content=button\u0026utm_campaign=dmhendricks%2Fwordpress-object-cache-helper)\n[![Get Flywheel](https://img.shields.io/badge/hosting-Flywheel-green.svg?style=flat-square\u0026label=compatible\u0026colorB=AE2A21)](https://share.getf.ly/e25g6k?utm_source=github.com\u0026utm_medium=campaign\u0026utm_content=button\u0026utm_campaign=dmhendricks%2Fwordpress-object-cache-helper)\n[![Twitter](https://img.shields.io/twitter/url/https/github.com/dmhendricks/wordpress-object-cache-helper.svg?style=social)](https://twitter.com/danielhendricks)\n\n# WordPress Object Cache Wrapper Class\n\nA simple [MU plugin](https://codex.wordpress.org/Must_Use_Plugins) for WordPress that acts as a wrapper for [WP Object Cache](https://codex.wordpress.org/Class_Reference/WP_Object_Cache) functions, with support for flushing cache by _group_.\n\nIt was created as an MU plugin so that it is loaded and available for use in the theme as well as standard custom plugins, useful to cache heavy operations that fall outside of WordPress's built-in caching (such as [direct database queries](https://codex.wordpress.org/Class_Reference/wpdb), file and remote operations, etc).\n\n## Installation\n\nSimply copy the `object-cache-helper.php` file to your `wp-content/mu-plugins` directory (create one if it does not exist).\n\n## Usage\n\n### Class Instantiation\n\nWithout arguments:\n\n```php\n$cache = new \\MU_Plugins\\WP_Cache_Object();\n```\n\nWith [arguments](#arguments):\n\n```php\n$cache = new \\MU_Plugins\\WP_Cache_Object([\n   'expire' =\u003e HOUR_IN_SECONDS * 8,\n   'group' =\u003e 'my_cache_group'\n]);\n```\n\n### Getting/Setting Cache Value\n\nIn this example, we will retrieve the public IP address of the server from [SeeIP](https://seeip.org/) using an anonymous function as the callback and cache it for one day:\n\n```php\nfunction get_public_ip_address() {\n  $result = wp_remote_get( 'https://ip4.seeip.org' );\n  return isset( $result['body'] ) ? $result['body'] : null;\n}\n\n$cache = new \\MU_Plugins\\WP_Cache_Object( [ 'expire' =\u003e DAY_IN_SECONDS ] );\n$ip_address = $cache-\u003eget_object( 'my_server_public_ip_address', 'get_public_ip_address' );\n\necho 'Public IP Address: ' . $ip_address;\n```\n\nNote that in this example, we have one line of code in the callback function, but you can add as much logic as you like.\n\n#### Anonymous Function as Callback\n\nIn this example, we will do the same as above with an anonymous function as the callback:\n\n\n```php\n$cache = new \\MU_Plugins\\WP_Cache_Object( [ 'expire' =\u003e DAY_IN_SECONDS ] );\n\n$ip_address = $cache-\u003eget_object( 'my_server_public_ip_address', function() {\n  $result = wp_remote_get( 'https://ip4.seeip.org' );\n  return isset( $result['body'] ) ? $result['body'] : null;\n});\n\necho 'Public IP Address: ' . $ip_address;\n```\n\n#### Passing Variables to Callback Function\n\nThis example shows how to pass variables to an anonymous callback. You would not cache this in practice and purely serves as an example for passing variables. In this example, we pass `$name` and `$age` to the callback.\n\n```php\n$cache = new \\MU_Plugins\\WP_Cache_Object( [ 'expire' =\u003e HOUR_IN_SECONDS * 12 ] );\n\n$name = 'Daniel';\n$age = 29;\n\n$greeting = $cache-\u003eget_object( 'about_me_string', function() use ( \u0026$name, \u0026$age ) {\n  return sprintf( 'Hello %s. You are %d years old.', $name, $age );\n});\n\necho $greeting; // Hello Daniel. You are 29 years old.\n```\n\n### Flushing the Cache\n\nYou can flush the entire cache using [`wp_cache_flush()`](https://developer.wordpress.org/reference/functions/wp_cache_flush/), or you can flush a _group_ that was created _using this class_ with the following:\n\n```php\n$cache-\u003eflush_group( 'my_cache_group' );\n```\n\n#### Deleting a Specific Key From a Cache Group\n\n```php\n$cache-\u003edelete_group_key( 'my_cache_key_name' ); // Removes key from default group\n\n$cache-\u003edelete_group_key( 'my_cache_key_name', 'custom_cache_group' ); // Removes key from specific group\n```\n\n## Arguments\n\nThe following arguments are supported when instantiating the class:\n\n| **Option**       | **Description**                                                               | **Type** | **Default**             |\n|------------------|-------------------------------------------------------------------------------|----------|-------------------------|\n| `group`          | The group to store the cache key in                                           | string   | {theme_dir}_cache_group |\n| `expire`         | Number of seconds to cache the value                                          | int      | 3600 (1 hour)           |\n| `single`         | Store as single key rather than group array                                   | bool     | false                   |\n| `network_global` | Set to true to store cache value for entire network, rather than per sub-site | bool     | false                   |\n| `force`          | Always return uncached value, useful for debugging                            | bool     | false                   |\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdmhendricks%2Fwordpress-object-cache-helper","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdmhendricks%2Fwordpress-object-cache-helper","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdmhendricks%2Fwordpress-object-cache-helper/lists"}