source: getweather/trunk/simple-cache.php @ 529

Revision 529, 1.9 KB checked in by chuyskywalker, 7 years ago (diff)
Line 
1<?php
2/*
3Plugin Name: Simple Cache
4Version: 1.0
5Plugin URI:
6Description: Simple set of caching functions. Useful for plugin authors to build off of.
7Author: Jeff Minard
8Author URI: http://creatimation.net/
9*/
10
11
12define(CACHE_DIR, ABSPATH . 'wp-content/simple-cache');
13
14if ( !file_exists(CACHE_DIR) ) {
15        //ack, no cache dir created - try to make one
16        if ( is_writable( dirname(CACHE_DIR) ) ) {
17                // parent folder is writeable, try to make a dir.
18                $dir = @mkdir( CACHE_DIR, 0666);
19                if($dir == false) {
20                        // tried to make cache folder and failed.
21                        die("Your cache directory (<code>" . CACHE_DIR . "</code>) needs and be writable for this plugin to work. Double-check it. <a href='" . get_settings('siteurl') . "/wp-admin/plugins.php?action=deactivate&amp;plugin=simple-cache.php'>Deactivate the Simple Cache plugin</a>.");
22                }
23        } else {
24                // parent is unwrite able, and we have no chache folder.
25                die("The simple-cache plugin cache directory (<code>" . CACHE_DIR . "</code>) needs to exist and be writable for this plugin to work. Double-check it. <a href='" . get_settings('siteurl') . "/wp-admin/plugins.php?action=deactivate&amp;plugin=simple-cache.php'>Deactivate the Simple Cache plugin</a>.");
26        }
27}
28
29
30function cache_recall($func_call, $stale_age=15) {
31
32        $filename = CACHE_DIR . md5($func_call) . '.txt';
33
34        if( !file_exists($filename) ) {
35                // this function call has not been cached before
36                return false;
37        }
38
39        if ( filemtime($filename) < strtotime("$stale_age minutes ago") ) {
40                // file contents are too old!
41                return false;
42        }
43
44        $cached_content = file_get_contents($filename);
45
46        return $cached_content;
47
48}
49
50function cache_store($func_call, $content) {
51
52        $filename = CACHE_DIR . md5($func_call) . '.txt';
53
54        if (!$handle = @fopen($filename, 'w')) {
55                // Can't open file?
56                return false;
57        }
58
59        if (fwrite($handle, $content) === FALSE) {
60                // Write fail?
61                return false;
62        }
63
64        fclose($handle);
65
66        return true;
67
68}
69
70
71?>
Note: See TracBrowser for help on using the repository browser.