-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCachedStorage.php
47 lines (40 loc) · 1.47 KB
/
CachedStorage.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
<?php
/**
* Do the dirty work.
*/
class CachedStorage
{
static function fetchIdByTitle($title) {
global $wgRevisionCacheExpiry, $wgMemc;
if ($wgRevisionCacheExpiry) { // TODO a better test for caching
$titlekey = wfMemcKey( 'textid', 'titlehash', md5($title) );
$textid = $wgMemc->get($titlekey);
if (is_int($textid) && $textid > 0)
return $textid;
}
return false;
}
static function set($title, $content) {
global $wgRevisionCacheExpiry, $wgMemc;
if (!$wgRevisionCacheExpiry) // caching is not possible
return false;
// we need to assign a sequence to revision text, because
// Article::loadContent expects page.text_id to be an integer.
$seq_key = wfMemcKey('offline', 'textid_seq');
if (!$wgMemc->get($seq_key))
$wgMemc->set($seq_key, 1); // and clear the cache??
$textid = $wgMemc->incr($seq_key);
// cache a lookup from title to fake textid
$titlekey = wfMemcKey( 'textid', 'titlehash', md5($title) );
$wgMemc->set( $titlekey, $textid, $wgRevisionCacheExpiry );
// TODO interfering with the cache is necessary to avoid a
// second query on Revision::newFromId. It would be much
// smarter to directly retrieve article markup, and optionally
// cache in the usual way.
$textkey = wfMemcKey( 'revisiontext', 'textid', $textid );
$wgMemc->delete( $textkey );
$wgMemc->set( $textkey, $content, $wgRevisionCacheExpiry );
//wfDebug('Stuffing the cache with '.strlen($content).' bytes, at id='.$textid."\n");
return $textid;
}
}