5555import org .jsoup .nodes .Element ;
5656import org .jsoup .safety .Whitelist ;
5757import org .jsoup .select .Elements ;
58+ import pers .adlered .simplecurrentlimiter .cache .pair .CachePair ;
5859import pers .adlered .simplecurrentlimiter .main .SimpleCurrentLimiter ;
5960
6061import java .math .BigDecimal ;
@@ -194,8 +195,6 @@ public class ChatroomProcessor {
194195
195196 public static String barragerUnit = "积分" ;
196197
197-
198-
199198 /**
200199 * Register request handlers.
201200 */
@@ -222,22 +221,35 @@ public static void register() {
222221 Dispatcher .get ("/gen" , chatroomProcessor ::genMetal , loginCheck ::handle );
223222 }
224223
224+ public static Map <String , String > metalCache = Collections .synchronizedMap (new LinkedHashMap <String , String >() {
225+ @ Override
226+ protected boolean removeEldestEntry (Map .Entry eldest ) {
227+ return size () > 2000 ;
228+ }
229+ });
225230 public void genMetal (final RequestContext context ) {
226231 Set <String > params = context .getRequest ().getParameterNames ();
227232 String paramString = "" ;
228233 for (String param : params ) {
229234 paramString += param + "=" + context .getRequest ().getParameter (param ) + "&" ;
230235 }
231236 paramString = "?" + paramString .substring (0 , paramString .length () - 1 );
232- String genUrl = Symphonys .get ("gen.metal.url" ) + paramString ;
233- final HttpRequest req = HttpRequest .get (genUrl ).header (Common .USER_AGENT , Symphonys .USER_AGENT_BOT );
234- final HttpResponse res = req .connectionTimeout (3000 ).timeout (5000 ).send ();
235- res .close ();
236- if (200 != res .statusCode ()) {
237- context .sendError (500 );
238- return ;
237+ String body = "" ;
238+ if (!metalCache .containsKey (paramString )) {
239+ String genUrl = Symphonys .get ("gen.metal.url" ) + paramString ;
240+ final HttpRequest req = HttpRequest .get (genUrl ).header (Common .USER_AGENT , Symphonys .USER_AGENT_BOT );
241+ final HttpResponse res = req .connectionTimeout (3000 ).timeout (5000 ).send ();
242+ res .close ();
243+ if (200 != res .statusCode ()) {
244+ context .sendError (500 );
245+ return ;
246+ }
247+ body = res .charset ("utf-8" ).bodyText ();
248+ metalCache .put (paramString , body );
249+ } else {
250+ body = metalCache .get (paramString );
239251 }
240- String body = res . charset ( "utf-8" ). bodyText ();
252+
241253 context .getResponse ().setContentType ("image/svg+xml" );
242254 context .getResponse ().sendBytes (body .getBytes ());
243255 }
0 commit comments