source: Dev/trunk/rdfapi/util/magpie/rss_cache.inc @ 12

Last change on this file since 12 was 12, checked in by basvannuland, 14 years ago

Added RAP RDF API
Added RDF reader writer for save and load survey

File size: 6.4 KB
Line 
1<?php
2/*
3 * Project:     MagpieRSS: a simple RSS integration tool
4 * File:        rss_cache.inc, a simple, rolling(no GC), cache
5 *              for RSS objects, keyed on URL.
6 * Author:      Kellan Elliott-McCrea <kellan@protest.net>
7 * Version:     0.51
8 * License:     GPL
9 *
10 * The lastest version of MagpieRSS can be obtained from:
11 * http://magpierss.sourceforge.net
12 *
13 * For questions, help, comments, discussion, etc., please join the
14 * Magpie mailing list:
15 * http://lists.sourceforge.net/lists/listinfo/magpierss-general
16 *
17 */
18
19class RSSCache {
20    var $BASE_CACHE = './cache';    // where the cache files are stored
21    var $MAX_AGE    = 3600;         // when are files stale, default one hour
22    var $ERROR      = "";           // accumulate error messages
23   
24    function RSSCache ($base='', $age='') {
25        if ( $base ) {
26            $this->BASE_CACHE = $base;
27        }
28        if ( $age ) {
29            $this->MAX_AGE = $age;
30        }
31       
32        // attempt to make the cache directory
33        if ( ! file_exists( $this->BASE_CACHE ) ) {
34            $status = @mkdir( $this->BASE_CACHE, 0755 );
35           
36            // if make failed
37            if ( ! $status ) {
38                $this->error(
39                    "Cache couldn't make dir '" . $this->BASE_CACHE . "'."
40                );
41            }
42        }
43    }
44   
45/*=======================================================================*\
46    Function:   set
47    Purpose:    add an item to the cache, keyed on url
48    Input:      url from wich the rss file was fetched
49    Output:     true on sucess 
50\*=======================================================================*/
51    function set ($url, $rss) {
52        $this->ERROR = "";
53        $cache_file = $this->file_name( $url );
54        $fp = @fopen( $cache_file, 'w' );
55       
56        if ( ! $fp ) {
57            $this->error(
58                "Cache unable to open file for writing: $cache_file"
59            );
60            return 0;
61        }
62       
63       
64        $data = $this->serialize( $rss );
65        fwrite( $fp, $data );
66        fclose( $fp );
67       
68        return $cache_file;
69    }
70   
71/*=======================================================================*\
72    Function:   get
73    Purpose:    fetch an item from the cache
74    Input:      url from wich the rss file was fetched
75    Output:     cached object on HIT, false on MISS
76\*=======================================================================*/
77    function get ($url) {
78        $this->ERROR = "";
79        $cache_file = $this->file_name( $url );
80       
81        if ( ! file_exists( $cache_file ) ) {
82            $this->debug(
83                "Cache doesn't contain: $url (cache file: $cache_file)"
84            );
85            return 0;
86        }
87       
88        $fp = @fopen($cache_file, 'r');
89        if ( ! $fp ) {
90            $this->error(
91                "Failed to open cache file for reading: $cache_file"
92            );
93            return 0;
94        }
95       
96        if ($filesize = filesize($cache_file) ) {
97                $data = fread( $fp, filesize($cache_file) );
98                $rss = $this->unserialize( $data );
99       
100                return $rss;
101        }
102       
103        return 0;
104    }
105
106/*=======================================================================*\
107    Function:   check_cache
108    Purpose:    check a url for membership in the cache
109                and whether the object is older then MAX_AGE (ie. STALE)
110    Input:      url from wich the rss file was fetched
111    Output:     cached object on HIT, false on MISS
112\*=======================================================================*/     
113    function check_cache ( $url ) {
114        $this->ERROR = "";
115        $filename = $this->file_name( $url );
116       
117        if ( file_exists( $filename ) ) {
118            // find how long ago the file was added to the cache
119            // and whether that is longer then MAX_AGE
120            $mtime = filemtime( $filename );
121            $age = time() - $mtime;
122            if ( $this->MAX_AGE > $age ) {
123                // object exists and is current
124                return 'HIT';
125            }
126            else {
127                // object exists but is old
128                return 'STALE';
129            }
130        }
131        else {
132            // object does not exist
133            return 'MISS';
134        }
135    }
136
137        function cache_age( $cache_key ) {
138                $filename = $this->file_name( $url );
139                if ( file_exists( $filename ) ) {
140                        $mtime = filemtime( $filename );
141            $age = time() - $mtime;
142                        return $age;
143                }
144                else {
145                        return -1;     
146                }
147        }
148       
149/*=======================================================================*\
150    Function:   serialize
151\*=======================================================================*/     
152    function serialize ( $rss ) {
153        return serialize( $rss );
154    }
155
156/*=======================================================================*\
157    Function:   unserialize
158\*=======================================================================*/     
159    function unserialize ( $data ) {
160        return unserialize( $data );
161    }
162   
163/*=======================================================================*\
164    Function:   file_name
165    Purpose:    map url to location in cache
166    Input:      url from wich the rss file was fetched
167    Output:     a file name
168\*=======================================================================*/     
169    function file_name ($url) {
170        $filename = md5( $url );
171        return join( DIRECTORY_SEPARATOR, array( $this->BASE_CACHE, $filename ) );
172    }
173
174/*=======================================================================*\
175    Function:   error
176    Purpose:    register error
177\*=======================================================================*/         
178    function error ($errormsg, $lvl=E_USER_WARNING) {
179        // append PHP's error message if track_errors enabled
180        if ( isset($php_errormsg) ) {
181            $errormsg .= " ($php_errormsg)";
182        }
183        $this->ERROR = $errormsg;
184        if ( MAGPIE_DEBUG ) {
185            trigger_error( $errormsg, $lvl);
186        }
187        else {
188            error_log( $errormsg, 0);
189        }
190    }
191   
192    function debug ($debugmsg, $lvl=E_USER_NOTICE) {
193        if ( MAGPIE_DEBUG ) {
194            $this->error("MagpieRSS [debug] $debugmsg", $lvl);
195        }
196    }
197
198}
199
200?>
Note: See TracBrowser for help on using the repository browser.