forked from makesites/rss-merger
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathrssFileCache.php
104 lines (96 loc) · 2.97 KB
/
rssFileCache.php
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
<?php
namespace Taophp;
/**
* This file defile the rssFileCache class
*
* This is a file cache to use with the rssMerger class
* @package Rss-merger
* @license GPLv2
* @license http://www.gnu.org/licenses/old-licenses/gpl-2.0.html GNU Public License version 2
* @copyright 2014 Stéphane Mourey <[email protected]>
* @author Stéphane Mourey <[email protected]>
* @copyright 2009-2011 Makis Tracend <[email protected]>
* @author Makis Tracend
* @version 2.3.2-beta Time Limited Edition
* */
class rssFileCache implements rssCacheInt {
/** @type string the directory used to store files*/
protected $dir;
/** @type int the maximum age of a usable cache in milliseconds */
public $maxAge = 1; /** default: one second */
/**
* The constructor
*
* @param string $dir the directory where to store the files
* */
public function __construct($dir){
if (!is_dir($dir) || !is_writable($dir))
throw new \Exception('The directory used to store data must be a writable directory.');
$this->dir = $dir;
}
/**
* Store a string (RSS XML) in the cache for a feed
*
* @param string $feedId a unique id for the feed to store
* @param string $toStore the string to store in the cache (RSS XML)
*
* @return bool
* */
public function feedRSSCache($feedId,$toStore){
return file_put_contents($this->getFileFullNameFromFeedId($feedId),$toStore);
}
/**
* Retrive a string (RSS XML) from the cache for a feed
*
* @param string $feedId a unique id for the feed to store
*
* @return string the stored string (RSS XML)
* */
public function getRSSCache($feedId){
return file_get_contents($this->getFileFullNameFromFeedId($feedId));
}
/**
* Check if the cache is usable (not too old)
*
* @param string $feedId a unique id for the feed to store
*
* @return bool true if usable, false if not
* */
public function checkRSSCache($feedId){
$filename = $this->getFileFullNameFromFeedId($feedId);
return $this->checkRSSCacheExists($feedId)
&& filesize($filename)
&& (time()-filemtime($filename) < $this->maxAge);
}
/**
* Return the complete filename with path of a cached file for a feed Id
*
* @param string $feedId the feed id
*
* @return string the complete filename
* */
protected function getFileFullNameFromFeedId($feedId){
return $this->dir.'/'.$this->getFilenameFromFeedId($feedId);
}
/**
* Return the basename of the file used to store a cached feed from its Id
*
* @param string $feedId the feed id
*
* @return string the base filename
* */
protected function getFilenameFromFeedId($feedId){
return $feedId.'.rss';
}
/**
* Check if data exist in cache (even too old)
*
* @param string $feedId a unique id for the feed to store
*
* @return bool true if data exist, false if not
* */
public function checkRSSCacheExists($feedId){
$filename = $this->getFileFullNameFromFeedId($feedId);
return file_exists($filename) && is_readable($this->getFileFullNameFromFeedId($feedId));
}
}