[44] | 1 | <?php
|
---|
| 2 | /**
|
---|
| 3 | * Copyright 2006, 2007, 2008, 2009 Eric D. Hough (http://ehough.com)
|
---|
| 4 | *
|
---|
| 5 | * This file is part of TubePress (http://tubepress.org)
|
---|
| 6 | *
|
---|
| 7 | * TubePress is free software: you can redistribute it and/or modify
|
---|
| 8 | * it under the terms of the GNU General Public License as published by
|
---|
| 9 | * the Free Software Foundation, either version 3 of the License, or
|
---|
| 10 | * (at your option) any later version.
|
---|
| 11 | *
|
---|
| 12 | * TubePress is distributed in the hope that it will be useful,
|
---|
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
---|
| 15 | * GNU General Public License for more details.
|
---|
| 16 | *
|
---|
| 17 | * You should have received a copy of the GNU General Public License
|
---|
| 18 | * along with TubePress. If not, see <http://www.gnu.org/licenses/>.
|
---|
| 19 | *
|
---|
| 20 | */
|
---|
| 21 |
|
---|
| 22 | function_exists('tubepress_load_classes')
|
---|
| 23 | || require(dirname(__FILE__) . '/../../../tubepress_classloader.php');
|
---|
| 24 | tubepress_load_classes(array('org_tubepress_cache_CacheService',
|
---|
| 25 | 'net_php_pear_Cache_Lite'));
|
---|
| 26 |
|
---|
| 27 | /**
|
---|
| 28 | * General purpose cache for TubePress
|
---|
| 29 | */
|
---|
| 30 | class org_tubepress_cache_SimpleCacheService implements org_tubepress_cache_CacheService
|
---|
| 31 | {
|
---|
| 32 | private $_cache;
|
---|
| 33 |
|
---|
| 34 | /**
|
---|
| 35 | * Simple constructor
|
---|
| 36 | *
|
---|
| 37 | */
|
---|
| 38 | public function __construct()
|
---|
| 39 | {
|
---|
| 40 | /*
|
---|
| 41 | * thanks to shickm for this...
|
---|
| 42 | * http://code.google.com/p/tubepress/issues/detail?id=27
|
---|
| 43 | */
|
---|
| 44 | if (!function_exists("sys_get_temp_dir")) {
|
---|
| 45 |
|
---|
| 46 | // Based on http://www.phpit.net/
|
---|
| 47 | // article/creating-zip-tar-archives-dynamically-php/2/
|
---|
| 48 | function sys_get_temp_dir()
|
---|
| 49 | {
|
---|
| 50 | // Try to get from environment variable
|
---|
| 51 | if ( !empty($_ENV['TMP']) )
|
---|
| 52 | {
|
---|
| 53 | return realpath( $_ENV['TMP'] );
|
---|
| 54 | }
|
---|
| 55 | else if ( !empty($_ENV['TMPDIR']) )
|
---|
| 56 | {
|
---|
| 57 | return realpath( $_ENV['TMPDIR'] );
|
---|
| 58 | }
|
---|
| 59 | else if ( !empty($_ENV['TEMP']) )
|
---|
| 60 | {
|
---|
| 61 | return realpath( $_ENV['TEMP'] );
|
---|
| 62 | }
|
---|
| 63 |
|
---|
| 64 | // Detect by creating a temporary file
|
---|
| 65 | else
|
---|
| 66 | {
|
---|
| 67 | // Try to use system's temporary directory
|
---|
| 68 | // as random name shouldn't exist
|
---|
| 69 | $temp_file = tempnam( md5(uniqid(rand(), TRUE)), '' );
|
---|
| 70 | if ( $temp_file )
|
---|
| 71 | {
|
---|
| 72 | $temp_dir = realpath( dirname($temp_file) );
|
---|
| 73 | unlink( $temp_file );
|
---|
| 74 | return $temp_dir;
|
---|
| 75 | }
|
---|
| 76 | else
|
---|
| 77 | {
|
---|
| 78 | return FALSE;
|
---|
| 79 | }
|
---|
| 80 | }
|
---|
| 81 | }
|
---|
| 82 | }
|
---|
| 83 |
|
---|
| 84 | $this->_cache = new net_php_pear_Cache_Lite(array("cacheDir" => sys_get_temp_dir()));
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | /**
|
---|
| 88 | * @see org_tubepress_cache_CacheService::get($key)
|
---|
| 89 | */
|
---|
| 90 | public function get($key)
|
---|
| 91 | {
|
---|
| 92 | return $this->_cache->get($key);
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | /**
|
---|
| 96 | * @see org_tubepress_cache_CacheService::has($key)
|
---|
| 97 | */
|
---|
| 98 | public function has($key)
|
---|
| 99 | {
|
---|
| 100 | return $this->_cache->get($key) !== false;
|
---|
| 101 | }
|
---|
| 102 |
|
---|
| 103 | /**
|
---|
| 104 | * @see org_tubepress_cache_CacheService::save($key, $data)
|
---|
| 105 | */
|
---|
| 106 | public function save($key, $data)
|
---|
| 107 | {
|
---|
| 108 | if (!is_string($data)) {
|
---|
| 109 | throw new Exception("Cache can only save string data");
|
---|
| 110 | }
|
---|
| 111 | $this->_cache->save($data, $key);
|
---|
| 112 | }
|
---|
| 113 |
|
---|
| 114 |
|
---|
| 115 | }
|
---|