colly_wyx
2018-04-27 74adf3a72663f151dc2c1b87ecb4ea4b0e080a50
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
<?php
 
/** Loading and saving data, it's only cache so load() does not need to block until save()
*/
interface NotORM_Cache {
    
    /** Load stored data
    * @param string
    * @return mixed or null if not found
    */
    function load($key);
    
    /** Save data
    * @param string
    * @param mixed
    * @return null
    */
    function save($key, $data);
    
}
 
 
 
/** Cache using $_SESSION["NotORM"]
*/
class NotORM_Cache_Session implements NotORM_Cache {
    
    function load($key) {
        if (!isset($_SESSION["NotORM"][$key])) {
            return null;
        }
        return $_SESSION["NotORM"][$key];
    }
    
    function save($key, $data) {
        $_SESSION["NotORM"][$key] = $data;
    }
    
}
 
 
 
/** Cache using file
*/
class NotORM_Cache_File implements NotORM_Cache {
    private $filename, $data = array();
    
    function __construct($filename) {
        $this->filename = $filename;
        $this->data = unserialize(@file_get_contents($filename)); // @ - file may not exist
    }
    
    function load($key) {
        if (!isset($this->data[$key])) {
            return null;
        }
        return $this->data[$key];
    }
    
    function save($key, $data) {
        if (!isset($this->data[$key]) || $this->data[$key] !== $data) {
            $this->data[$key] = $data;
            file_put_contents($this->filename, serialize($this->data), LOCK_EX);
        }
    }
    
}
 
 
 
/** Cache using PHP include
*/
class NotORM_Cache_Include implements NotORM_Cache {
    private $filename, $data = array();
    
    function __construct($filename) {
        $this->filename = $filename;
        $this->data = @include realpath($filename); // @ - file may not exist, realpath() to not include from include_path //! silently falls with syntax error and fails with unreadable file
        if (!is_array($this->data)) { // empty file returns 1
            $this->data = array();
        }
    }
    
    function load($key) {
        if (!isset($this->data[$key])) {
            return null;
        }
        return $this->data[$key];
    }
    
    function save($key, $data) {
        if (!isset($this->data[$key]) || $this->data[$key] !== $data) {
            $this->data[$key] = $data;
            file_put_contents($this->filename, '<?php return ' . var_export($this->data, true) . ';', LOCK_EX);
        }
    }
    
}
 
 
 
/** Cache storing data to the "notorm" table in database
*/
class NotORM_Cache_Database implements NotORM_Cache {
    private $connection;
    
    function __construct(PDO $connection) {
        $this->connection = $connection;
    }
    
    function load($key) {
        $result = $this->connection->prepare("SELECT data FROM notorm WHERE id = ?");
        $result->execute(array($key));
        $return = $result->fetchColumn();
        if (!$return) {
            return null;
        }
        return unserialize($return);
    }
    
    function save($key, $data) {
        // REPLACE is not supported by PostgreSQL and MS SQL
        $parameters = array(serialize($data), $key);
        $result = $this->connection->prepare("UPDATE notorm SET data = ? WHERE id = ?");
        $result->execute($parameters);
        if (!$result->rowCount()) {
            $result = $this->connection->prepare("INSERT INTO notorm (data, id) VALUES (?, ?)");
            try {
                @$result->execute($parameters); // @ - ignore duplicate key error
            } catch (PDOException $e) {
                if ($e->getCode() != "23000") { // "23000" - duplicate key
                    throw $e;
                }
            }
        }
    }
    
}
 
 
 
// eAccelerator - user cache is obsoleted
 
 
 
/** Cache using "NotORM." prefix in Memcache
*/
class NotORM_Cache_Memcache implements NotORM_Cache {
    private $memcache;
    
    function __construct(Memcache $memcache) {
        $this->memcache = $memcache;
    }
    
    function load($key) {
        $return = $this->memcache->get("NotORM.$key");
        if ($return === false) {
            return null;
        }
        return $return;
    }
    
    function save($key, $data) {
        $this->memcache->set("NotORM.$key", $data);
    }
    
}
 
 
 
/** Cache using "NotORM." prefix in APC
*/
class NotORM_Cache_APC implements NotORM_Cache {
    
    function load($key) {
        $success = false;
        $return = apc_fetch("NotORM.$key", $success);
        if (!$success) {
            return null;
        }
        return $return;
    }
    
    function save($key, $data) {
        apc_store("NotORM.$key", $data);
    }
    
}