forked from AKSW/site.ontowiki
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FastCacheLoad.php
119 lines (102 loc) · 3.06 KB
/
FastCacheLoad.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
<?php
/**
* @copyright Copyright (c) 2012, {@link http://aksw.org AKSW}
* @license http://opensource.org/licenses/gpl-license.php GNU General Public License (GPL)
*/
/**
* Site Extension Fast Cache Loader
*
* this files parses the config ini generates the cache id and directly
* looks for the object cache and outputs it. put this to your htacess:
*
* <Files "index.php">
* php_value auto_prepend_file 'extensions/site/FastCacheLoad.php'
* </Files>
*
* @category OntoWiki
* @author Sebastian Tramp <[email protected]>
*/
/*
* only mysql supported
*/
function GetCachedContent_ZendDB($cacheId, $config)
{
$dbuser = $config['store.zenddb.username'];
$dbpass = $config['store.zenddb.password'];
$dbname = $config['store.zenddb.dbname'];
$dbhost = 'localhost';
$link = mysql_connect($dbhost, $dbuser, $dbpass);
if (!$link) {
exit('Could not connect: ' . mysql_error());
}
$selectedDatabase = mysql_select_db($dbname, $link);
if (!$selectedDatabase) {
exit('Can\'t use foo : ' . mysql_error());
}
$query = 'SELECT content FROM `ef_cache` WHERE id = "' . $cacheId . '"';
// Perform Query
$result = mysql_query($query);
// Check result
if ($result && (mysql_num_rows($result) == 1)) {
$result = mysql_fetch_row($result);
$content = unserialize($result[0]);
return $content;
}
}
/*
* fetch virtuoso based cache
*/
function GetCachedContent_Virtuoso($cacheId, $config)
{
$virtUser = $config['store.virtuoso.username'];
$virtPass = $config['store.virtuoso.password'];
$virtDsn = $config['store.virtuoso.dsn'];
$connection = @odbc_connect($virtDsn, $virtUser, $virtPass);
if ($connection == false) {
exit('Could not connect to virtuoso');
}
$query = "SELECT content
FROM DB.DBA.ef_cache
WHERE id = '$cacheId'";
$resultId = odbc_exec($connection, $query);
if ($resultId == false) {
// Erroneous query
return;
}
if (odbc_num_rows($resultId) == 1) {
$serialized = '';
while ($segment = odbc_result($resultId, 1)) {
$serialized .= (string) $segment;
}
return unserialize($serialized);
}
}
function GetCacheContent ($id)
{
$config = parse_ini_file('config.ini');
$backend = $config['store.backend'];
$content = null;
switch ($backend) {
case 'zenddb':
$content = GetCachedContent_ZendDB($id, $config);
break;
case 'virtuoso':
$content = GetCachedContent_Virtuoso($id, $config);
break;
default:
// nothing to do
}
return $content;
}
$content = null;
// prepare the cacheid and fetch the cache content
if (preg_match('/\.html$/', $_SERVER['REQUEST_URI'])) {
$siteModuleObjectCacheIdSource = $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI'];
$siteModuleObjectCacheId = 'site_' . md5($siteModuleObjectCacheIdSource);
$content = GetCacheContent($siteModuleObjectCacheId);
}
// Cache hit: send response and exit
if ($content != null) {
echo $content;
exit;
}