Skip to content

Commit

Permalink
Merge pull request #3 from brunomarks/master
Browse files Browse the repository at this point in the history
Possibility of redefine the file extension and append trailing if is missing in cache folder parameter
  • Loading branch information
gilbitron committed Jul 15, 2014
2 parents dc55a03 + a249f90 commit 70793e9
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions simpleCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,19 @@ class SimpleCache {
var $cache_path = 'cache/';
//Length of time to cache a file in seconds
var $cache_time = 3600;
// possibility of define the file extension
var $cache_extension = '.cache';

//This is just a functionality wrapper function
function get_data($label, $url)
{
// append trailing if is missing
$path_size = strlen($this->cache_path);
if (substr($this->cache_path, $path_size - 1) !== '/')
{
$this->cache_path .= '/';
}

if($data = $this->get_cache($label)){
return $data;
} else {
Expand All @@ -30,13 +39,13 @@ function get_data($label, $url)

function set_cache($label, $data)
{
file_put_contents($this->cache_path . $this->safe_filename($label) .'.cache', $data);
file_put_contents($this->cache_path . $this->safe_filename($label) . $this->cache_extension, $data);
}

function get_cache($label)
{
if($this->is_cached($label)){
$filename = $this->cache_path . $this->safe_filename($label) .'.cache';
$filename = $this->cache_path . $this->safe_filename($label) . $this->cache_extension;
return file_get_contents($filename);
}

Expand All @@ -45,7 +54,7 @@ function get_cache($label)

function is_cached($label)
{
$filename = $this->cache_path . $this->safe_filename($label) .'.cache';
$filename = $this->cache_path . $this->safe_filename($label) . $this->cache_extension;

if(file_exists($filename) && (filemtime($filename) + $this->cache_time >= time())) return true;

Expand Down

0 comments on commit 70793e9

Please sign in to comment.