кусок коду php getter function read_cache fileName fileName path to ca

 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
////1 кусок коду
<?php
//getter
function read_cache($fileName) {
$fileName = '/path/to/cache/folder'.$fileName;
if (file_exists($fileName)) {
$handle = fopen($fileName, 'rb');
$variable = fread($handle, filesize($fileName));
fclose($handle);
return unserialize($variable);
} else {
return null;
}
}
//setter
function write_cache($fileName,$variable) {
$fileName = '/path/to/cache/folder'.$fileName;
$handle = fopen($fileName, 'a');
fwrite($handle, serialize($variable));
fclose($handle);
}
//destroy
function delete_cache($fileName) {
$fileName = '/path/to/cache/folder'.$fileName;
@unlink($fileName);
}
?>
//// 2 кусок коду
<?
// include class
require_once('cache.php');
// checking the existence of the cache
$data = read_cache('test_17.tmp'); //17 this it example id of article
// if cache doesn't exist get article from database
if (empty($data)) {
$sql = "SELECT image, user_id, content FROM table WHERE id = 17";
$data = $db->select_list($sql); // just for example
write_cache('test_17.tmp', $data);
}
// next use data what always
foreach($data as $row) {
........
}
?>
//// 3 кусок коду
<?
require_once('cache.php');
// update article
$data = 'some array for updating';
$db->update_record('table', $data);
delete_cache('test_17.tmp');
?>