-
Notifications
You must be signed in to change notification settings - Fork 5
/
CacheDAO.php
executable file
·151 lines (141 loc) · 3.98 KB
/
CacheDAO.php
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
<?php defined('SKYBLUE') or die('Bad file request');
/**
* @version 2.0 2010-07-12 21:18:00 $
* @package SkyBlueCanvas
* @copyright Copyright (C) 2005 - 2010 Scott Edwin Lewis. All rights reserved.
* @license GNU/GPL, see COPYING.txt
* SkyBlueCanvas is free software. This version may have been modified pursuant
* to the GNU General Public License, and as distributed it includes or
* is derivative of works licensed under the GNU General Public License or
* other free or open source software licenses.
* See COPYING.txt for copyright notices and details.
*/
/**
* @author Scott Lewis
* @date July 12, 2010
*/
class CacheDAO extends SqliteDAO {
/**
* Class constructor
* @param string The cache file name
* @param int The cache lifetime in minutes
*/
function __construct() {
parent::__construct(array(
'type' => 'cache',
'bean_class' => 'Cache'
));
if (! $this->tableExists("Cache")) {
try {
$this->query(
"BEGIN; " .
"CREATE TABLE Cache (" .
"id CHAR(255) PRIMARY KEY, " .
"content CHAR(255), " .
"time CHAR(255)" .
");" .
" COMMIT;"
);
}
catch (PDOException $e) {
die($e->getMessage());
}
}
}
/**
* Gets a cached item
* @return array An associative array of the cache item DB row
*/
function getItem($id) {
$Statement = $this->query("SELECT * FROM Cache WHERE id = '{$id}' LIMIT 1");
if ($result = $Statement->fetch(PDO::FETCH_ASSOC)) {
return $result;
}
return null;
}
/**
* Get all cached items
* @return array
*/
function getAllItems($refresh=false) {
$Statement = $this->query("SELECT * FROM Cache");
if ($result = $Statement->fetchAll(PDO::FETCH_ASSOC)) {
return $result;
}
return null;
}
/**
* Saves an item to the cache
* @param string The content to cache
* @return boolean Whether or not the content was cached
*/
function saveCache($content) {
$result = false;
$id = $this->itemName;
$time = time();
if (! $this->getItem($id)) {
$content = base64_encode($content);
try {
$this->query(
"INSERT INTO Cache (id, content, time) "
. " VALUES ('{$id}', '{$content}', '{$time}');"
);
}
catch (PDOException $e) {
die($e->getMessage());
}
}
else {
$result = $this->touchCache();
}
return $result;
}
/**
* Updates the cache file time to the current time
* @return void
*/
function touchCache() {
$result = false;
try {
$result = $this->query(
"UPDATE Cache SET time = '{$time}' WHERE id = '{$id}';"
);
}
catch (PDOException $e) {
die($e->getMessage());
}
return $result;
}
/**
* Deletes the cache file
* @return void
*/
function clearItem($id) {
$result = false;
try {
$result = $this->query(
"DELETE FROM Cache WHERE id = '{$id}'"
);
}
catch (PDOException $e) {
die($e->getMessage());
}
return $result;
}
/**
* Deletes all cahced files
* @return bool Whether or not all cached files were deleted
*/
function clearAll() {
$result = false;
try {
$result = $this->query(
"DELETE FROM Cache"
);
}
catch (PDOException $e) {
die($e->getMessage());
}
return $result;
}
}