Handling compressions

This commit is contained in:
2016-12-21 18:34:55 +08:00
parent a7edb55d4c
commit 3a3a22b37a
3 changed files with 69 additions and 13 deletions

View File

@@ -5,11 +5,20 @@ const cl = global.botanLoader;
const Cookie = cl.load( "botanss.net.components.Cookie" );
const STORE_RAW = 0;
const STORE_GZ = 1;
const STORE_DEFLATE = 2;
class PageCache
{
constructor()
{
this.cache = {};
this.cache = [];
this.cache[ STORE_RAW ] = {};
this.cache[ STORE_GZ ] = {};
this.cache[ STORE_DEFLATE ] = {};
this.excepts = {};
setInterval( () => {
@@ -49,7 +58,10 @@ class PageCache
if( res.headers[ "Location" ] )
c.headers[ "Location" ] = res.headers[ "Location" ];
this.cache[ key ] = c;
if( res.headers[ "Content-Encoding" ] )
c.headers[ "Content-Encoding" ] = res.headers[ "Content-Encoding" ];
this.cache[ this.__encRes( res ) ][ key ] = c;
Dragonfly.Debug( "StoreCache: \"" + key + "\", expire " + new Date( expires ) );
}
@@ -70,7 +82,9 @@ class PageCache
if( cookie.get( "sid" ) in this.excepts ) return false;
var url = req.url;
if( url in this.cache )
var store = this.cache[ this.__encReq( req ) ];
if( url in store )
{
Dragonfly.Info(
"[C] "
@@ -80,7 +94,7 @@ class PageCache
, Dragonfly.Visibility.VISIBLE
);
var c = this.cache[ url ];
var c = store[ url ];
res.writeHead( c.statusCode, c.headers );
res.end( c.data );
@@ -89,6 +103,20 @@ class PageCache
return false;
}
__encRes( res )
{
return { "deflate": STORE_DEFLATE, "gzip": STORE_GZ }[ res.headers[ "Content-Encoding" ] ] || STORE_RAW;
}
__encReq( req )
{
var enc = req.headers[ "accept-encoding" ] || "";
if( ~enc.indexOf( "deflate" ) ) return STORE_DEFLATE;
else if( ~enc.indexOf( "gzip" ) ) return STORE_GZ;
return STORE_RAW;
}
}
module.exports = PageCache;