diff --git a/http-service/src/main/java/net/runelite/http/service/xtea/XteaCache.java b/http-service/src/main/java/net/runelite/http/service/xtea/XteaCache.java new file mode 100644 index 0000000000..7c5f2bb5b0 --- /dev/null +++ b/http-service/src/main/java/net/runelite/http/service/xtea/XteaCache.java @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2018, Adam + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * 1. Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package net.runelite.http.service.xtea; + +import lombok.AllArgsConstructor; +import lombok.Data; + +@Data +@AllArgsConstructor +class XteaCache +{ + private int region; + private int key1; + private int key2; + private int key3; + private int key4; +} diff --git a/http-service/src/main/java/net/runelite/http/service/xtea/XteaService.java b/http-service/src/main/java/net/runelite/http/service/xtea/XteaService.java index ddc476a1fd..b74c146dee 100644 --- a/http-service/src/main/java/net/runelite/http/service/xtea/XteaService.java +++ b/http-service/src/main/java/net/runelite/http/service/xtea/XteaService.java @@ -24,6 +24,8 @@ */ package net.runelite.http.service.xtea; +import com.google.common.cache.Cache; +import com.google.common.cache.CacheBuilder; import java.io.IOException; import java.util.List; import java.util.stream.Collectors; @@ -72,6 +74,10 @@ public class XteaService private final Sql2o sql2o; private final CacheService cacheService; + private final Cache keyCache = CacheBuilder.newBuilder() + .maximumSize(1024) + .build(); + @Autowired public XteaService( @Qualifier("Runelite SQL2O") Sql2o sql2o, @@ -101,6 +107,29 @@ public class XteaService @RequestMapping(method = POST) public void submit(@RequestBody XteaRequest xteaRequest) { + boolean cached = true; + for (XteaKey key : xteaRequest.getKeys()) + { + int region = key.getRegion(); + int[] keys = key.getKeys(); + + XteaCache xteaCache = keyCache.getIfPresent(region); + if (xteaCache == null + || xteaCache.getKey1() != keys[0] + || xteaCache.getKey2() != keys[1] + || xteaCache.getKey3() != keys[2] + || xteaCache.getKey4() != keys[3]) + { + cached = false; + keyCache.put(region, new XteaCache(region, keys[0], keys[1], keys[2], keys[3])); + } + } + + if (cached) + { + return; + } + try (Connection con = sql2o.beginTransaction()) { CacheEntry cache = cacheService.findMostRecent(); @@ -110,8 +139,7 @@ public class XteaService throw new InternalServerErrorException("No most recent cache"); } - Query query = con.createQuery("insert into xtea (region, rev, key1, key2, key3, key4) " - + "values (:region, :rev, :key1, :key2, :key3, :key4)"); + Query query = null; for (XteaKey key : xteaRequest.getKeys()) { @@ -140,6 +168,12 @@ public class XteaService continue; } + if (query == null) + { + query = con.createQuery("insert into xtea (region, rev, key1, key2, key3, key4) " + + "values (:region, :rev, :key1, :key2, :key3, :key4)"); + } + query.addParameter("region", region) .addParameter("rev", xteaRequest.getRevision()) .addParameter("key1", keys[0]) @@ -149,8 +183,11 @@ public class XteaService .addToBatch(); } - query.executeBatch(); - con.commit(false); + if (query != null) + { + query.executeBatch(); + con.commit(false); + } } }