📄 mcacheserver.java
字号:
package net.jumperz.app.MGuardian.plugin;
import java.io.*;
import java.util.*;
import net.jumperz.net.*;
import net.jumperz.util.*;
import java.net.*;
public class MCacheServer
extends MGuardianPlugin
{
private static final int DEFAULT_CACHE_SIZE = 100;
private Map cacheMap = new HashMap( DEFAULT_CACHE_SIZE );
//--------------------------------------------------------------------------------
public Map execute( Map sessionInfo )
throws IOException
{
String arg = ( String )sessionInfo.get( "arg" );
String url = getUrl( sessionInfo );
if( arg.equals( "clear" ) )
{
return clear( sessionInfo );
}
else if( arg.equals( "list" ) )
{
return list( sessionInfo );
}
else if( arg.equals( "add" ) )
{
add( sessionInfo, url );
return null;
}
else if( arg.equals( "load" ) )
{
return load( sessionInfo, url );
}
else if( arg.equals( "remove" ) )
{
remove( sessionInfo, url );
return null;
}
return null;
}
//--------------------------------------------------------------------------------
private void remove( Map sessionInfo, String url )
{
cacheMap.remove( url );
}
//--------------------------------------------------------------------------------
private Map clear( Map sessionInfo )
throws IOException
{
if( sessionInfo.get( "response" ) == null )
{
return clearCache();
}
else
{
return null;
}
}
//--------------------------------------------------------------------------------
private Map list( Map sessionInfo )
throws IOException
{
if( sessionInfo.get( "response" ) == null )
{
return listUri();
}
else
{
return null;
}
}
//--------------------------------------------------------------------------------
private String getUrl( Map sessionInfo )
{
MHttpRequest request = ( MHttpRequest )sessionInfo.get( "request" );
String url = null;
String uri = request.getUri();
if( uri.startsWith( "/" ) )
{
String host = null;
if( request.headerExists( "Host" ) )
{
host = request.getHeaderValue( "Host" );
}
else
{
Socket clientSocket = ( Socket )sessionInfo.get( "clientSideSocket" );
host = clientSocket.getLocalAddress().getHostAddress();
}
url = "http://" + host + uri;
}
else
{
url = uri;
}
return url;
}
//--------------------------------------------------------------------------------
private Map listUri()
throws IOException
{
Map pluginResult = new HashMap();
MHttpResponse response = new MHttpResponse();
synchronized( this )
{
Set keySet = cacheMap.keySet();
Iterator p = keySet.iterator();
StringBuffer s = new StringBuffer();
while( p.hasNext() )
{
s.append( p.next() );
s.append( "\n" );
}
response.setBody( s.toString() );
}
pluginResult.put( "response", response );
return pluginResult;
}
//--------------------------------------------------------------------------------
private Map clearCache()
throws IOException
{
Map pluginResult = new HashMap();
MHttpResponse response = new MHttpResponse();
synchronized( this )
{
response.setBody( cacheMap.keySet().toString() );
cacheMap.clear();
}
pluginResult.put( "response", response );
return pluginResult;
}
//--------------------------------------------------------------------------------
private synchronized void add( Map sessionInfo, String url )
{
//System.err.println( url );
// already in cache
if( cacheMap.containsKey( url ) )
{
return;
}
MHttpResponse response = ( MHttpResponse )sessionInfo.get( "response" );
response.removeHeaderValue( "Set-Cookie" );
response.setHeaderValue( "X-GC", "true" );
cacheMap.put( url, response );
}
//--------------------------------------------------------------------------------
private synchronized Map load( Map sessionInfo, String url )
{
if( cacheMap.containsKey( url ) )
{
MHttpResponse response = ( MHttpResponse )cacheMap.get( url );
Map pluginResult = new HashMap();
pluginResult.put( "response", response );
pluginResult.put( "pass", new Boolean( true ) );
return pluginResult;
}
else
{
return null;
}
}
//--------------------------------------------------------------------------------
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -