kc: change to use redis for storage
This commit is contained in:
@@ -24,74 +24,40 @@
|
|||||||
*/
|
*/
|
||||||
package net.runelite.http.service.kc;
|
package net.runelite.http.service.kc;
|
||||||
|
|
||||||
|
import java.time.Duration;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.beans.factory.annotation.Qualifier;
|
|
||||||
import org.springframework.scheduling.annotation.Scheduled;
|
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.sql2o.Connection;
|
import redis.clients.jedis.Jedis;
|
||||||
import org.sql2o.Sql2o;
|
import redis.clients.jedis.JedisPool;
|
||||||
|
|
||||||
@Service
|
@Service
|
||||||
public class KillCountService
|
public class KillCountService
|
||||||
{
|
{
|
||||||
private static final String CREATE = "CREATE TABLE IF NOT EXISTS `kc` (\n" +
|
private static final Duration KC_EXPIRE = Duration.ofMinutes(2);
|
||||||
" `name` varchar(32) NOT NULL,\n" +
|
|
||||||
" `boss` varchar(32) NOT NULL,\n" +
|
|
||||||
" `kc` int(11) NOT NULL,\n" +
|
|
||||||
" `time` timestamp NOT NULL DEFAULT current_timestamp() ON UPDATE current_timestamp(),\n" +
|
|
||||||
" UNIQUE KEY `name` (`name`, `boss`),\n" +
|
|
||||||
" KEY `time` (`time`)\n" +
|
|
||||||
") ENGINE=MEMORY;";
|
|
||||||
|
|
||||||
private final Sql2o sql2o;
|
private final JedisPool jedisPool;
|
||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
public KillCountService(@Qualifier("Runelite SQL2O") Sql2o sql2o)
|
public KillCountService(JedisPool jedisPool)
|
||||||
{
|
{
|
||||||
this.sql2o = sql2o;
|
this.jedisPool = jedisPool;
|
||||||
|
|
||||||
try (Connection con = sql2o.open())
|
|
||||||
{
|
|
||||||
con.createQuery(CREATE)
|
|
||||||
.executeUpdate();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public Integer getKc(String name, String boss)
|
public Integer getKc(String name, String boss)
|
||||||
{
|
{
|
||||||
try (Connection con = sql2o.open())
|
String value;
|
||||||
|
try (Jedis jedis = jedisPool.getResource())
|
||||||
{
|
{
|
||||||
return con.createQuery("select kc from kc where name = :name and boss = :boss")
|
value = jedis.get("kc." + name + "." + boss);
|
||||||
.addParameter("name", name)
|
|
||||||
.addParameter("boss", boss)
|
|
||||||
.executeScalar(Integer.class);
|
|
||||||
}
|
}
|
||||||
|
return value == null ? null : Integer.parseInt(value);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setKc(String name, String boss, int kc)
|
public void setKc(String name, String boss, int kc)
|
||||||
{
|
{
|
||||||
try (Connection con = sql2o.open())
|
try (Jedis jedis = jedisPool.getResource())
|
||||||
{
|
{
|
||||||
con.createQuery("insert into kc (name, boss, kc) values (:name, :boss, :kc) on duplicate key update kc = VALUES(kc)")
|
jedis.setex("kc." + name + "." + boss, (int) KC_EXPIRE.getSeconds(), Integer.toString(kc));
|
||||||
.addParameter("name", name)
|
|
||||||
.addParameter("boss", boss)
|
|
||||||
.addParameter("kc", kc)
|
|
||||||
.executeUpdate();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public void purge()
|
|
||||||
{
|
|
||||||
try (Connection con = sql2o.open())
|
|
||||||
{
|
|
||||||
con.createQuery("delete from kc where time < (now() - interval 2 minute);")
|
|
||||||
.executeUpdate();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Scheduled(fixedDelay = 60_000)
|
|
||||||
public void schedPurge()
|
|
||||||
{
|
|
||||||
purge();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user