Page Cache
This commit is contained in:
parent
7b8348be37
commit
96c9d608c2
@ -1,10 +1,11 @@
|
|||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
var cl = global.botanLoader;
|
const cl = global.botanLoader;
|
||||||
var Dragonfly = global.Dragonfly;
|
const Dragonfly = global.Dragonfly;
|
||||||
var CondStream = cl.load( "botanss.utils.CondStream" );
|
const PageCache = global.botanPageCache;
|
||||||
|
|
||||||
var FatalError = cl.load( "botanss.errors.FatalError" );
|
const CondStream = cl.load( "botanss.utils.CondStream" );
|
||||||
|
const FatalError = cl.load( "botanss.errors.FatalError" );
|
||||||
|
|
||||||
class WebFrame
|
class WebFrame
|
||||||
{
|
{
|
||||||
@ -108,7 +109,7 @@ class WebFrame
|
|||||||
this.handlers[ name ] = method.bind( this );
|
this.handlers[ name ] = method.bind( this );
|
||||||
}
|
}
|
||||||
|
|
||||||
plantResult()
|
plantResult( cache, ttl )
|
||||||
{
|
{
|
||||||
if( this.planted ) return;
|
if( this.planted ) return;
|
||||||
|
|
||||||
@ -124,6 +125,8 @@ class WebFrame
|
|||||||
this.HTTP.response.write( this.result );
|
this.HTTP.response.write( this.result );
|
||||||
this.HTTP.response.end();
|
this.HTTP.response.end();
|
||||||
Dragonfly.Debug( "Result Planted" );
|
Dragonfly.Debug( "Result Planted" );
|
||||||
|
|
||||||
|
this.__storeCache( cache, ttl );
|
||||||
}
|
}
|
||||||
|
|
||||||
// Release resources
|
// Release resources
|
||||||
@ -133,6 +136,20 @@ class WebFrame
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
__storeCache( cache, ttl )
|
||||||
|
{
|
||||||
|
if( cache && PageCache )
|
||||||
|
{
|
||||||
|
if( ttl == undefined ) ttl = 30;
|
||||||
|
PageCache.store(
|
||||||
|
this.HTTP.request.raw
|
||||||
|
, this.HTTP.response
|
||||||
|
, this.result
|
||||||
|
, ttl
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// This won't handle path exists
|
// This won't handle path exists
|
||||||
// throwing an error is better than handling it
|
// throwing an error is better than handling it
|
||||||
// Need to handle it somewhere else
|
// Need to handle it somewhere else
|
||||||
|
65
system/PageCache.js
Normal file
65
system/PageCache.js
Normal file
@ -0,0 +1,65 @@
|
|||||||
|
"use strict";
|
||||||
|
|
||||||
|
const Dragonfly = global.Dragonfly;
|
||||||
|
const cl = global.botanLoader;
|
||||||
|
|
||||||
|
class PageCache
|
||||||
|
{
|
||||||
|
constructor()
|
||||||
|
{
|
||||||
|
this.cache = { };
|
||||||
|
setInterval( () => {
|
||||||
|
var d = new Date().getTime();
|
||||||
|
for( var i in this.cache )
|
||||||
|
{
|
||||||
|
var c = this.cache[ i ];
|
||||||
|
if( c.ttl < d ) delete this.cache[ i ];
|
||||||
|
}
|
||||||
|
}, 30000 );
|
||||||
|
}
|
||||||
|
|
||||||
|
store( req, res, data, ttl )
|
||||||
|
{
|
||||||
|
if( !global.pagecache ) return;
|
||||||
|
var key = req.url;
|
||||||
|
|
||||||
|
var expires = new Date().getTime() + 1000 * ttl;
|
||||||
|
|
||||||
|
this.cache[ key ] = {
|
||||||
|
data: data
|
||||||
|
, headers: {
|
||||||
|
"Content-Length": res.headers[ "Content-Length" ]
|
||||||
|
, "Content-Type": res.headers[ "Content-Type" ]
|
||||||
|
, "X-Cache-Expires": new Date( expires )
|
||||||
|
}
|
||||||
|
, ttl: expires
|
||||||
|
};
|
||||||
|
|
||||||
|
Dragonfly.Debug( "StoreCache: \"" + key + "\", expire " + new Date( expires ) );
|
||||||
|
}
|
||||||
|
|
||||||
|
process( req, res )
|
||||||
|
{
|
||||||
|
var url = req.url;
|
||||||
|
if( url in this.cache )
|
||||||
|
{
|
||||||
|
Dragonfly.Info(
|
||||||
|
"[C] "
|
||||||
|
+ ( req.headers[ "x-forwarded-for" ] || req.connection.remoteAddress ) + " "
|
||||||
|
+ req.method + ": " + encodeURI( url )
|
||||||
|
+ " - " + req.headers["user-agent"]
|
||||||
|
, Dragonfly.Visibility.VISIBLE
|
||||||
|
);
|
||||||
|
|
||||||
|
var c = this.cache[ url ];
|
||||||
|
res.headers = c.headers;
|
||||||
|
res.write( c.data );
|
||||||
|
res.end();
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = PageCache;
|
Loading…
Reference in New Issue
Block a user