Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/mitogh/on-cache-please
:floppy_disk: WordPress - Add automatic cache on requests using the Transients API
https://github.com/mitogh/on-cache-please
Last synced: 28 days ago
JSON representation
:floppy_disk: WordPress - Add automatic cache on requests using the Transients API
- Host: GitHub
- URL: https://github.com/mitogh/on-cache-please
- Owner: mitogh
- License: mit
- Created: 2015-10-25T17:09:53.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2016-01-16T21:24:35.000Z (almost 9 years ago)
- Last Synced: 2024-04-09T10:02:27.194Z (7 months ago)
- Language: PHP
- Homepage:
- Size: 36.1 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: readme.md
Awesome Lists containing this project
README
# On Cache Please [![Build Status](https://travis-ci.org/mitogh/On-Cache-Please.svg?branch=master)](https://travis-ci.org/mitogh/On-Cache-Please)
> Because programmers are to lazy to write repeated code, this
utility automatically storages the data of a callback in a transient to
faster access the next time the data is accessed.## Description
This utility can help you to create faster cache storage of expensive
external request or hard work into the DB to faster access the next
time, to retrive the value from a cache rather than the function, this
library make uses of [transient api](https://codex.wordpress.org/Transients_API) to save the
data from the requests into the DB.## Requirements.
- WordPress
- PHP 5.4 >=## Installation.
The easiest way to install this library is using composer, in order to
add this library as a dependency for your composer file you only need to
run in your terminal:```php
composer require mitogh/on-cache-please
```To retrive the library from [packagist](https://packagist.org/packages/mitogh/on-cache-please). This will
add the on-cache-please library as a dependency in your `composer.json`
file.The other way is directly include the file:
```php
include 'On-Cache-Please/src/OnCache.php';
```Into your `functions.php` file in your theme or in your plugin.
## Methods and Properties
Here are listed only the public methods that are available to be used
from the outside of the library.### OnCache::please
This static method is the public way to storaged the data from a
function in a transient by a determined amount of time in order to
decrease the number of request and download time.**Parameters**
You can pass an `array` of arguments to the method `please` in order to
update some default values and some required params as well.- `name` *(string)* - required. This is a required param since we need a value to
identify the transient, where the data is going to be stored.
- `callback` *(string|function)* - required. The function or name of the
function to execute, in this function you can do expensive things like
an http external request or a instagram API call, just make sure to
return the data you want to save on the cache, the returned value will
be storead in the transient.
- `duration` *(int)* - optional. 1 hour by default. This value can be any
integer number and represents the life of the transient in seconds
before execute the `callback` again and update the transient, there are
few [few already defined](https://codex.wordpress.org/Transients_API#Using_Time_Constants).
constants that migh help you.**Example**
In this example we retrieve the data from `http://codepen.io/jobs.json`
and store the result in a transient that has a duration (by default of 1
hour), so the next hour we are going to have the latest updated data
wiouth having to make a request every time the page is loaded.Library installed via `composer.json`
```php
include './vendor/autoload.php';
$args = array(
'name' => 'codepen_jobs',
'callback' => function(){
$url = 'http://codepen.io/jobs.json';
return wp_remote_retrieve_body( wp_remote_get( $url ) );
},
);
$jobs = mitogh\OnCache::please( $args );
```Library installed manually:
```php
include './On-Cache-Please/src/OnCache.php';
$args = array(
'name' => 'codepen_jobs',
'callback' => function(){
$url = 'http://codepen.io/jobs.json';
return wp_remote_retrieve_body( wp_remote_get( $url ) );
},
);
$jobs = mitogh\OnCache::please( $args );
```