Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 107 lines (86 sloc) 2.084 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
<?php

class HttpCache {
##protected $rootDir = '/tmp/http-cache/';
protected $rootDir;

public function __construct($dir=false) {
if ($dir) {
$this->setRootDir($dir);
} else {
$this->rootDir = HttpUtilsConstants::getBaseCacheDir();
}
}

public function setRootDir($dir) {
if (file_exists($dir) && is_dir($dir)) {
$this->rootDir = $dir;
}
}

public function getRootDir() {
return $this->rootDir;
}


public function cache($url, $text) {
if (!is_string($text) && (strlen($text)>0) ) {
return NULL;
}

$file = $this->_getFileName($url);

//echo "Url: {$url}\nFile: {$file}\n";
$ret = file_put_contents($file, $text);
return (bool)$ret;
}

public function uncache($url) {
$file = $this->_getFileName($url);
if (file_exists($file)) {
return unlink($file);
}
return false;
}

public function isCached($url) {
$file = $this->_getFileName($url);
//echo "Url: {$url}\nFile: {$file}\n";
//echo '#';
return file_exists($file);
}

public function get($url) {
$file = $this->_getFileName($url);
//echo "Cache-file: {$file}\n";
if (file_exists($file)) {
$body = file_get_contents($file);
//echo "Cache Body Length: ", strlen($body), "\n";
if (strlen($body) > 0) {
return $body;
} else {
// Delete empty cached entries
$this->uncache($url);
}
}
return NULL;
}

public function isFresh($url, $expire=false) {

}


protected function _getFileName($url) {
$domain = parse_url($url, PHP_URL_HOST);
$key = md5($url);

$filePath = $this->_getFullPath($domain) . $key;
return $filePath;
}

protected function _getFullPath($domain) {
if ($domain === false) {
$domainDir = $this->rootDir;
} else {
$domainDir = $this->rootDir . $domain . '/';
if (!$this->_initDomainDir($domainDir)) {
return NULL;
}
}
return $domainDir;
}

protected function _initDomainDir($domainDir) {
if (!file_exists($domainDir)) {
if (!@mkdir($domainDir)) {
echo "ERROR: Couldn't create $domainDir\n";
return false;
}
}
return true;
}

}

?>
Something went wrong with that request. Please try again.