summaryrefslogtreecommitdiff
path: root/include/MyCache.class.php
blob: e074a3ba5c54600541c355428e94e62364d82a0b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<?php
/**
 * Simple on-disk variable caching class. Implements file caching logic.
 */
class MyCache {

    private function __construct() {}

    private function __clone() {}


    /**
     * Sets given cache line to given value
     *
     * @param id string cache line name. It should follow file naming conventions.
     * @param value object - any serializable variable
     *
     * @return its second argument
     */
    static function set($id, $value) {
        file_put_contents(BASEPATH.'/caches/MyCache/'.$id, serialize($value));
        return $value;
    }


    /**
     * fetches cached value from disk
     *
     * @param id string - cache line identifier to fetch. It should follow file naming conventions.
     * @param valid int - if exists and non zero, method throws ExceptionCacheLineExpired if cache line
     * is older than given time in seconds
     *
     * @return restored variable contents
     */
    static function get($id, $valid=0, $callback=null, $passexception=true) {
        $isValid = self::valid($id, $valid);
        if ($valid != 0 && !$isValid && $callback===null) {
            throw new CacheLineExpiredException();
        }
        if ($isValid) {
            return unserialize(file_get_contents(BASEPATH.'/caches/MyCache/'.$id));
        }
        elseif ($callback===null) {
            if (file_exists(BASEPATH.'/caches/MyCache/'.$id)) {
                throw new CacheLineExpiredException();
            }
            else {
                throw new CacheLineNotFoundException($id);
            }
        }
        else {
            try {
                $value = call_user_func($callback);
                self::set($id,$value);
                return $value;
            }
            catch (Exception $e) {
                if ($passexception) {
                    throw $e;
                }
                return self::get($id);
            }
        }
    }


    /**
     * Removes given cache line from disk
     *
     * @param id string cache line name
     *
     * @return void
     */
    static function delete($id) {
        if (file_exists(BASEPATH.'/caches/MyCache/'.$id)) {
            unlink(BASEPATH.'/caches/MyCache/'.$id);
        }
    }

    /**
     * validates if cache line is not older than given time in seconds
     *
     * @param id string cache line name.
     * @param cacheLifeTime int cache line age limit (default 300 seconds)
     *
     * @return true if cache is stil valid, false otherwise
     */
    static function valid($id, $cacheLifeTime=null) {

        $cacheLifeTime = (int)$cacheLifeTime; //seconds

        if ($cacheLifeTime==null) {
            return is_readable(BASEPATH.'/caches/MyCache/'.$id);
        }

        if (!is_readable(BASEPATH.'/caches/MyCache/'.$id)) {
            return false;
        }
        return (time()-filemtime(BASEPATH.'/caches/MyCache/'.$id)) <= $cacheLifeTime;
    }


    /**
     * checks if cache line exists without validating its age
     *
     * @param id sting cache line name
     *
     * @return boolean true if cache line is found
     */
    static function exist($id) {
        return is_readable(BASEPATH .'/caches/MyCache/'.$id);
    }
}