/[web]/nav/lib.php
ViewVC logotype

Diff of /nav/lib.php

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1257 by rda, Mon May 28 09:13:59 2012 UTC revision 1305 by rda, Mon Jun 11 12:15:13 2012 UTC
# Line 1  Line 1 
1  <?php  <?php
2  // definition  // definition
3    
4    class NCache
5    {
6        function __construct() { }
7    
8        /**
9         * Factory.
10         *
11         * @param string $path where cache file store is located, relative to app path.
12         * @param integer $timeout in seconds
13         *
14         * @return NCache
15        */
16        function build($path, $timeout = 3600)
17        {
18            $path = __DIR__ . '/' . $path;
19    
20            if (!is_dir($path))
21                return null;
22    
23            if ($timeout < 60)
24                $timeout = 60;
25    
26            $i           = new self;
27            $i->_path    = $path;
28            $i->_timeout = $timeout;
29    
30            return $i;
31        }
32    
33        /**
34         * Get value for $key.
35         *
36         * @param mixed $key
37         *
38         * @return mixed
39        */
40        function get($key = null)
41        {
42            if (is_null($key))
43                return false;
44    
45            $filename = $this->_get_filename($key);
46    
47            if ($this->_is_valid_file($filename, $this->_timeout)) {
48                return unserialize(file_get_contents($filename));
49            }
50    
51            return null;
52        }
53    
54        /**
55         * Save $value under $key.
56         *
57         * @param mixed $key
58         * @param mixed $value
59        */
60        function set($key, $value)
61        {
62            if (is_null($key))
63                return false;
64    
65            $filename = $this->_get_filename($key);
66            file_put_contents($filename, serialize($value));
67    
68            return true;
69        }
70    
71        /**
72         * Get cache file from key.
73         *
74         * @param mixed $key
75         *
76         * @return string
77        */
78        private function _get_filename($key)
79        {
80            $key  = hash('sha1', serialize($key));
81    
82            return $this->_path . '/' . $key . '.cache';
83        }
84    
85        /**
86         * Check that the cache file exists and has not expired.
87         *
88         * @param string $filename
89         *
90         * @return boolean
91        */
92        private function _is_valid_file($filename, $timeout)
93        {
94            if (!file_exists($filename)) {
95                //error_log(sprintf('Could not find %s', $filename), 0);
96                return false;
97            }
98    
99            if (filemtime($filename) + $timeout < time()) {
100                //error_log(sprintf('%s timestamp expired (timeout was %ds.).', $filename, $timeout));
101                unlink($filename);
102                return false;
103            }
104    
105            //error_log(sprintf('Found %s', $filename));
106            return true;
107        }
108    }
109    
110  class l10n  class l10n
111  {  {
112      public static $t;      public static $t;

Legend:
Removed from v.1257  
changed lines
  Added in v.1305

  ViewVC Help
Powered by ViewVC 1.1.30