xtea service: add cache for recently tried keys
This commit is contained in:
@@ -0,0 +1,39 @@
|
|||||||
|
/*
|
||||||
|
* Copyright (c) 2018, Adam <Adam@sigterm.info>
|
||||||
|
* 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;
|
||||||
|
}
|
||||||
@@ -24,6 +24,8 @@
|
|||||||
*/
|
*/
|
||||||
package net.runelite.http.service.xtea;
|
package net.runelite.http.service.xtea;
|
||||||
|
|
||||||
|
import com.google.common.cache.Cache;
|
||||||
|
import com.google.common.cache.CacheBuilder;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
@@ -72,6 +74,10 @@ public class XteaService
|
|||||||
private final Sql2o sql2o;
|
private final Sql2o sql2o;
|
||||||
private final CacheService cacheService;
|
private final CacheService cacheService;
|
||||||
|
|
||||||
|
private final Cache<Integer, XteaCache> keyCache = CacheBuilder.newBuilder()
|
||||||
|
.maximumSize(1024)
|
||||||
|
.build();
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
public XteaService(
|
public XteaService(
|
||||||
@Qualifier("Runelite SQL2O") Sql2o sql2o,
|
@Qualifier("Runelite SQL2O") Sql2o sql2o,
|
||||||
@@ -101,6 +107,29 @@ public class XteaService
|
|||||||
@RequestMapping(method = POST)
|
@RequestMapping(method = POST)
|
||||||
public void submit(@RequestBody XteaRequest xteaRequest)
|
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())
|
try (Connection con = sql2o.beginTransaction())
|
||||||
{
|
{
|
||||||
CacheEntry cache = cacheService.findMostRecent();
|
CacheEntry cache = cacheService.findMostRecent();
|
||||||
@@ -110,8 +139,7 @@ public class XteaService
|
|||||||
throw new InternalServerErrorException("No most recent cache");
|
throw new InternalServerErrorException("No most recent cache");
|
||||||
}
|
}
|
||||||
|
|
||||||
Query query = con.createQuery("insert into xtea (region, rev, key1, key2, key3, key4) "
|
Query query = null;
|
||||||
+ "values (:region, :rev, :key1, :key2, :key3, :key4)");
|
|
||||||
|
|
||||||
for (XteaKey key : xteaRequest.getKeys())
|
for (XteaKey key : xteaRequest.getKeys())
|
||||||
{
|
{
|
||||||
@@ -140,6 +168,12 @@ public class XteaService
|
|||||||
continue;
|
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)
|
query.addParameter("region", region)
|
||||||
.addParameter("rev", xteaRequest.getRevision())
|
.addParameter("rev", xteaRequest.getRevision())
|
||||||
.addParameter("key1", keys[0])
|
.addParameter("key1", keys[0])
|
||||||
@@ -149,8 +183,11 @@ public class XteaService
|
|||||||
.addToBatch();
|
.addToBatch();
|
||||||
}
|
}
|
||||||
|
|
||||||
query.executeBatch();
|
if (query != null)
|
||||||
con.commit(false);
|
{
|
||||||
|
query.executeBatch();
|
||||||
|
con.commit(false);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user