~jstys-z/helioviewer.org/client5

« back to all changes in this revision

Viewing changes to src/php/Helper/Serialize.php

  • Committer: V. Keith Hughitt
  • Date: 2009-04-01 21:08:05 UTC
  • Revision ID: hughitt1@kore-20090401210805-372f7dgih07vxk42
nightly build 04-01-2009

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
<?php
2
 
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
3
 
/**
4
 
 * Serialize Class definition
5
 
 *
6
 
 * PHP version 5
7
 
 *
8
 
 * @category Helper
9
 
 * @package  Helioviewer
10
 
 * @author   Jeff Stys <jeff.stys@nasa.gov>
11
 
 * @license  http://www.mozilla.org/MPL/MPL-1.1.html Mozilla Public License 1.1
12
 
 * @link     http://launchpad.net/helioviewer.org
13
 
 */
14
 
/**
15
 
 * Provides reading, writing, and invalidating Serilized cache files.
16
 
 *
17
 
 * @category Helper
18
 
 * @package  Helioviewer
19
 
 * @author   Jeff Stys <jeff.stys@nasa.gov>
20
 
 * @license  http://www.mozilla.org/MPL/MPL-1.1.html Mozilla Public License 1.1
21
 
 * @link     http://launchpad.net/helioviewer.org
22
 
 */
23
 
class Helper_Serialize {
24
 
 
25
 
    private $_path;
26
 
    private $_filename;
27
 
    private $_maxAgeSec;
28
 
 
29
 
    /**
30
 
     * Constructor method
31
 
     *
32
 
     * @return void
33
 
     */
34
 
    public function __construct($subPath, $filename, $maxAgeSec=86000) {
35
 
        $this->_path      = HV_CACHE_DIR.'/'.$subPath;
36
 
        $this->_filename  = $filename;
37
 
        $this->_maxAgeSec = $maxAgeSec;
38
 
 
39
 
        // Verify that cache directory exists
40
 
        if ( !@file_exists($this->_path) ) {
41
 
            if ( !@mkdir($this->_path, 0777, true) ) {
42
 
               return false;
43
 
            }
44
 
        }
45
 
 
46
 
    }
47
 
 
48
 
    /**
49
 
     * Read cache file from disk and return un-serialized data structure.
50
 
     * Optionally invalidate cache file if older than _maxAgeSec.
51
 
     *
52
 
     * @return array on success, boolean false on error
53
 
     */
54
 
    public function readCache($verifyAge=true) {
55
 
 
56
 
        // Optionally invalidate cache if _maxAgeSec has been exceeded
57
 
        if ( $verifyAge === true ) {
58
 
 
59
 
            clearstatcache(true, $this->_path.'/'.$this->_filename);
60
 
            $timestamp = @filemtime($this->_path.'/'.$this->_filename);
61
 
 
62
 
            if ( $timestamp === false ||
63
 
                 (microtime(true)-(float)$timestamp) > (float)$this->_maxAgeSec ) {
64
 
 
65
 
                @unlink($this->_path.'/'.$this->_filename);
66
 
                return false;
67
 
            }
68
 
        }
69
 
 
70
 
        // Read serialized data from cache file
71
 
        $serialized = @file_get_contents($this->_path.'/'.$this->_filename);
72
 
 
73
 
        if ( $serialized === false ) {
74
 
            @unlink($this->_path.'/'.$this->_filename);
75
 
            return false;
76
 
        }
77
 
 
78
 
        // Un-serialize data
79
 
        $data = unserialize($serialized);
80
 
        if ( $data === null ) {
81
 
            @unlink($this->_path.'/'.$this->_filename);
82
 
            return false;
83
 
        }
84
 
 
85
 
        return $data;
86
 
    }
87
 
 
88
 
    /**
89
 
     * Write serialized data to cache file.
90
 
     * Optionally that caching succeeded by immediately reading in cached data
91
 
     * and attempting to un-serialize it.
92
 
     *
93
 
     * @return boolean
94
 
     */
95
 
    public function writeCache($data, $verify=false) {
96
 
 
97
 
        $serialized = serialize($data);
98
 
        if ( $serialized === false ) {
99
 
 
100
 
            return false;
101
 
        }
102
 
 
103
 
        // Write data to a temporary file first
104
 
        $temp_filename = md5($serialized).'_'.microtime(true);
105
 
 
106
 
        $fh = @fopen($this->_path.'/'.$temp_filename, 'w');
107
 
        if ( $fh === false ) {
108
 
            return false;
109
 
        }
110
 
        if ( !@fwrite($fh, $serialized) ) {
111
 
            @fclose($fh);
112
 
            @unlink($this->_path.'/'.$temp_filename);
113
 
            return false;
114
 
        }
115
 
        @fclose($fh);
116
 
 
117
 
        // Move temporary file into permanent location
118
 
        if ( !@rename($this->_path.'/'.$temp_filename, $this->_path.'/'.$this->_filename) ) {
119
 
 
120
 
            @unlink($this->_path.'/'.$temp_filename);
121
 
            return false;
122
 
        }
123
 
        clearstatcache(true, $this->_path.'/'.$this->_filename);
124
 
 
125
 
        // Optionally verify that cache file was written and installed
126
 
        // successfully by explicitly reading it back from disk and checking
127
 
        // that it the data it contains can be un-serialized.
128
 
        if ( $verify === true ) {
129
 
            if ( $this->readCache($verifyAge=false) === false ) {
130
 
                return false;
131
 
            }
132
 
        }
133
 
 
134
 
        return true;
135
 
    }
136
 
 
137
 
}
138
 
?>
 
 
b'\\ No newline at end of file'