Open Sencillo  2014.008
 All Data Structures Files Functions Variables Pages
mysql.php
Go to the documentation of this file.
1 <?php
2 /*~ mysql.php
3 .---------------------------------------------------------------------------.
4 | Software: SencilloCache |
5 | Version: 2014.002 |
6 | Contact: ph@mastery.sk |
7 | ------------------------------------------------------------------------- |
8 | Author: Bc. Peter Horváth (original founder) |
9 | Copyright (c) 2014, Bc. Peter Horváth. All Rights Reserved. |
10 | ------------------------------------------------------------------------- |
11 | License: Distributed under the General Public License (GPL) |
12 | http://www.gnu.org/copyleft/gpl.html |
13 | This program is distributed in the hope that it will be useful - WITHOUT |
14 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
15 | FITNESS FOR A PARTICULAR PURPOSE. |
16 '---------------------------------------------------------------------------'
17 ~*/
18 
19 /* quickcache_db_connect()
20  * Makes connection to the database
21  */
23  $GLOBALS["sql_link"] = @mysql_connect($GLOBALS["QUICKCACHE_DB_HOST"],
24  $GLOBALS["QUICKCACHE_DB_USERNAME"],
25  $GLOBALS["QUICKCACHE_DB_PASSWORD"]);
26 }
27 
28 /* quickcache_db_disconnect()
29  * Closes connection to the database
30  */
32  mysql_close($GLOBALS["sql_link"]);
33 }
34 
35 /* quickcache_db_query($query)
36  * Executes a given query
37  */
38 function quickcache_db_query($query) {
39  // quickcache_debug("Executing SQL-query $query");
40  $ret = @mysql_db_query($GLOBALS["QUICKCACHE_DB_DATABASE"],
41  $query,
42  $GLOBALS["sql_link"]);
43  return $ret;
44 }
45 
46 /* quickcache_restore()
47  * Will try to restore the cachedata from the db.
48  */
49 function quickcache_restore() {
50  $res = quickcache_db_query("select GZDATA, DATASIZE, DATACRC from ".
51  $GLOBALS["QUICKCACHE_DB_TABLE"].
52  " where CACHEKEY='".
53  addslashes($GLOBALS["quickcache_key"]).
54  "' and (CACHEEXPIRATION>".
55  time().
56  " or CACHEEXPIRATION=0)"
57  );
58 
59  if ($res && mysql_num_rows($res))
60  {
61  if ($row = mysql_fetch_array($res))
62  {
63  // restore data into global scope from found row
64  $GLOBALS["quickcachedata_gzdata"] = $row["GZDATA"];
65  $GLOBALS["quickcachedata_datasize"] = $row["DATASIZE"];
66  $GLOBALS["quickcachedata_datacrc"] = $row["DATACRC"];
67  return true;
68  }
69  }
70  return false;
71 }
72 
73 /* quickcache_write()
74  * Will (try to) write out the cachedata to the db
75  */
76 function quickcache_write($gzdata, $datasize, $datacrc) {
77  $dbtable = $GLOBALS["QUICKCACHE_DB_TABLE"];
78 
79  // XXX: Later on, maybe implement locking mechanism inhere.
80 
81  // Check if it already exists
82  $res = quickcache_db_query("select CACHEEXPIRATION from $dbtable".
83  " where CACHEKEY='".
84  addslashes($GLOBALS["quickcache_key"]).
85  "'"
86  );
87 
88 
89  if (!$res || mysql_num_rows($res) < 1) {
90  // Key not found, so insert
91  $res = quickcache_db_query("insert into $dbtable".
92  " (CACHEKEY, CACHEEXPIRATION, GZDATA,".
93  " DATASIZE, DATACRC) values ('".
94  addslashes($GLOBALS["quickcache_key"]).
95  "',".
96  (($GLOBALS["QUICKCACHE_TIME"] != 0) ?
97  (time()+$GLOBALS["QUICKCACHE_TIME"]) : 0).
98  ",'".
99  addslashes($gzdata).
100  "', $datasize, $datacrc)"
101  );
102  // This fails with unique-key violation when another thread has just
103  // inserted the same key. Just continue, as the result is (almost)
104  // the same.
105  } else {
106  // Key found, so update
107  $res = quickcache_db_query("update $dbtable set CACHEEXPIRATION=".
108  (($GLOBALS["QUICKCACHE_TIME"] != 0) ?
109  (time()+$GLOBALS["QUICKCACHE_TIME"]) : 0).
110  ", GZDATA='".
111  addslashes($gzdata).
112  "', DATASIZE=$datasize, DATACRC=$datacrc where".
113  " CACHEKEY='".
114  addslashes($GLOBALS["quickcache_key"]).
115  "'"
116  );
117  // This might be an update too much, but it shouldn't matter
118  }
119 }
120 
121 /* quickcache_do_gc()
122  * Performs the actual garbagecollection
123  */
124 function quickcache_do_gc() {
125  quickcache_db_query("delete from ".
126  $GLOBALS["QUICKCACHE_DB_TABLE"].
127  " where CACHEEXPIRATION<=".
128  time().
129  " and CACHEEXPIRATION!=0"
130  );
131 
132  // Are we allowed to do an optimize table-call?
133  // As noted, first check if this works on your mysql-installation!
134  if ($GLOBALS["QUICKCACHE_OPTIMIZE"]) {
135  quickcache_db_query("OPTIMIZE TABLE ".$GLOBALS["QUICKCACHE_DB_TABLE"]);
136  }
137 }
138 
139 /* quickcache_do_start()
140  * Additional code that is executed before main quickcache-code kicks in.
141  */
143  // Connect to db
145 }
146 
147 /* quickcache_do_end()
148  * Additional code that is executed after caching has been performed,
149  * but just before output is returned. No new output can be added!
150  */
151 function quickcache_do_end() {
152  // Disconnect from db
154 }
155 
156 ?>
quickcache_do_end()
Definition: mysql.php:151
quickcache_do_gc()
Definition: mysql.php:124
quickcache_do_start()
Definition: mysql.php:142
quickcache_db_query($query)
Definition: mysql.php:38
quickcache_db_connect()
Definition: mysql.php:22
quickcache_write($gzdata, $datasize, $datacrc)
Definition: mysql.php:76
quickcache_db_disconnect()
Definition: mysql.php:31
quickcache_restore()
Definition: mysql.php:49