From 598480dfe41b902e75ab297d576874a8c2e65d8f Mon Sep 17 00:00:00 2001 From: Adam Date: Sat, 5 Jan 2019 09:57:31 -0500 Subject: [PATCH] kc: change to use redis for storage --- .../http/service/kc/KillCountService.java | 60 ++++--------------- 1 file changed, 13 insertions(+), 47 deletions(-) diff --git a/http-service/src/main/java/net/runelite/http/service/kc/KillCountService.java b/http-service/src/main/java/net/runelite/http/service/kc/KillCountService.java index 4b7d49ad90..045a5c548a 100644 --- a/http-service/src/main/java/net/runelite/http/service/kc/KillCountService.java +++ b/http-service/src/main/java/net/runelite/http/service/kc/KillCountService.java @@ -24,74 +24,40 @@ */ package net.runelite.http.service.kc; +import java.time.Duration; 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.sql2o.Connection; -import org.sql2o.Sql2o; +import redis.clients.jedis.Jedis; +import redis.clients.jedis.JedisPool; @Service public class KillCountService { - private static final String CREATE = "CREATE TABLE IF NOT EXISTS `kc` (\n" + - " `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 static final Duration KC_EXPIRE = Duration.ofMinutes(2); - private final Sql2o sql2o; + private final JedisPool jedisPool; @Autowired - public KillCountService(@Qualifier("Runelite SQL2O") Sql2o sql2o) + public KillCountService(JedisPool jedisPool) { - this.sql2o = sql2o; - - try (Connection con = sql2o.open()) - { - con.createQuery(CREATE) - .executeUpdate(); - } + this.jedisPool = jedisPool; } 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") - .addParameter("name", name) - .addParameter("boss", boss) - .executeScalar(Integer.class); + value = jedis.get("kc." + name + "." + boss); } + return value == null ? null : Integer.parseInt(value); } 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)") - .addParameter("name", name) - .addParameter("boss", boss) - .addParameter("kc", kc) - .executeUpdate(); + jedis.setex("kc." + name + "." + boss, (int) KC_EXPIRE.getSeconds(), Integer.toString(kc)); } } - - 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(); - } }