Skip to content

Commit

Permalink
textual $cacheExpire
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Jul 2, 2015
1 parent af5d4fb commit 3a96600
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
7 changes: 7 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,12 @@ Download Atom feed from URL:
$atom = Feed::loadAtom($url);
```

You can also enable caching:

```php
Feed::$cacheDir = __DIR__ . '/tmp';
Feed::$cacheExpire = '5 hours';
```

-----
(c) David Grudl, 2008 (http://davidgrudl.com)
5 changes: 3 additions & 2 deletions src/Feed.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
class Feed
{
/** @var int */
public static $cacheExpire = 86400; // 1 day
public static $cacheExpire = '1 day';

/** @var string */
public static $cacheDir;
Expand Down Expand Up @@ -143,7 +143,8 @@ private static function httpRequest($url, $user, $pass)
{
if (self::$cacheDir) {
$cacheFile = self::$cacheDir . '/feed.' . md5($url) . '.xml';
if (@filemtime($cacheFile) + self::$cacheExpire > time()) {
$e = self::$cacheExpire;
if (time() - @filemtime($cacheFile) <= (is_string($e) ? strtotime($e) - time() : $e)) {
return file_get_contents($cacheFile);
}
}
Expand Down

0 comments on commit 3a96600

Please sign in to comment.