Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions Herbert/Framework/cache.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php namespace Herbert\Framework;

use Closure;
/*
* This is wrapper for WordPress transient functions
* It uses anonymous functions so
*
* For Example turn this ugly code:
*
if (false === ( $events = get_transient( 'my-events' ) ) ) {
$events = Event::all($start,$end);
set_transient( 'my-events', $result );
}

* Into:
*
$events = Cache::store('my-events',function() {
return Event::all($start,$end);
});
*/

class Cache {

/**
* Returns whatever is cached into $key.
* Runs $closure and stores it into $key when $closure returns non-empty response
*
* @param String $key - Key to store the result of the operation
*
* @param Closure $closure - Anonymous function which is run only when needed
*
* @return mixed - Returns the result of $closure from cache
*/
public static function store($key,Closure $closure)
{
if ( self::bypassCache() || false === ( $result = get_transient( $key ) ) ) {
$result = $closure();
if (!empty($result)) {
set_transient( $key, $result );
}
}
return $result;
}

/**
* Delete something from the cache
*
* @param String $key - Key to delete the from cache
*
* @return mixed - Returns the result of $closure from cache
*/
public static function delete($key)
{
return delete_transient($key);
}

/**
* Check if the request has PRAGMA header set to no-cache
*
* @return bool
*/
public static function bypassCache()
{
return (isset($_SERVER['HTTP_PRAGMA']) && $_SERVER['HTTP_PRAGMA'] === 'no-cache');
}
}
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"description": "Herbert",
"license": "MIT",
"require": {
"php": ">=5.4.0",
"twig/twig": "~1.16",
"illuminate/database": "~5.0",
"vierbergenlars/php-semver": "~3.0",
Expand Down