package org.lodder.subtools.sublibrary.cache;

import java.io.File;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.lodder.subtools.sublibrary.logging.Logger;

public class DiskCache<K, T> extends InMemoryCache<K, T> {

  private Connection conn;

  public DiskCache(long crunchifyTimeToLive, long crunchifyTimerInterval, int maxItems,
      String username, String password) {
    super(crunchifyTimeToLive, crunchifyTimerInterval, maxItems);
    File path = new File(System.getProperty("user.home"), ".MultiSubDownloader");
    if (!path.exists()) {
      if (!path.mkdir()) {
        throw new RuntimeException("Could not create folder " + path);
      }
    }
    PreparedStatement prep = null;
    try {
      Class.forName("org.hsqldb.jdbcDriver");
      conn =
          DriverManager.getConnection("jdbc:hsqldb:file:" + path.toString()
              + "/diskcache.hsqldb;hsqldb.write_delay=false;shutdown=true", username, password);

      prep =
          conn.prepareStatement("create table IF NOT EXISTS cacheobjects (key OTHER, cacheobject OTHER);");
      prep.execute();
      prep.close();
      fillCacheMap();
    } catch (ClassNotFoundException e) {
      throw new RuntimeException("Unable to load jdbcdriver for diskcache");
    } catch (SQLException e) {
      throw new RuntimeException(e);
    } finally {
      try {
        if (prep != null) prep.close();
      } catch (SQLException e) {
        throw new RuntimeException(e);
      }
    }
  }

  @SuppressWarnings("unchecked")
  private void fillCacheMap() {
    try (Statement stmt = conn.createStatement()) {
      ResultSet rs = stmt.executeQuery("SELECT key, cacheobject FROM cacheobjects");
      while (rs.next()) {
        synchronized (cacheMap) {
          cacheMap.put(rs.getObject("key"), rs.getObject("cacheobject"));
        }
      }
      rs.close();
    } catch (SQLException e) {
      Logger.instance.error("Unable to insert object in disk cache!");
    } finally {

    }
  }

  public void remove(K key) {
    super.remove(key);
    try (PreparedStatement prep = conn.prepareCall("delete from cacheobjects where key = ?")) {
      prep.clearParameters();
      prep.setObject(1, key);
      prep.execute();
    } catch (SQLException e) {
      Logger.instance.error("Unable to delete object from disk cache!");
    }
  }

  public void put(K key, T value) {
    super.put(key, value);
    try (PreparedStatement prep =
        conn.prepareCall("INSERT INTO cacheobjects (key,cacheobject) VALUES (?,?)")) {
      prep.clearParameters();
      prep.setObject(1, key);
      synchronized (cacheMap) {
        CacheObject<?, ?> cacheObject = (CacheObject<?, ?>) cacheMap.get(key);
        prep.setObject(2, cacheObject);
      }
      prep.execute();
    } catch (SQLException e) {
      Logger.instance.error("Unable to insert object in disk cache!");
    }
  }
}

@@ -255,49 +255,52 @@
 org.
-lodder.subtools.sublibrary.logging.Logger
+slf4j.Logger;%0Aimport org.slf4j.LoggerFactory
 ;%0A%0Ap
@@ -358,16 +358,16 @@
 , T%3E %7B%0A%0A
-
   privat
@@ -384,16 +384,100 @@
 on conn;
+%0A  %0A  private static final Logger LOGGER = LoggerFactory.getLogger(DiskCache.class);
 %0A%0A  publ
@@ -2100,38 +2100,29 @@
 e) %7B%0A      L
-ogger.instance
+OGGER
 .error(%22Unab
@@ -2148,32 +2148,35 @@
  in disk cache!%22
+, e
 );%0A    %7D finally
@@ -2455,38 +2455,29 @@
 e) %7B%0A      L
-ogger.instance
+OGGER
 .error(%22Unab
@@ -2505,32 +2505,35 @@
 rom disk cache!%22
+, e
 );%0A    %7D%0A  %7D%0A%0A  
@@ -2997,22 +2997,13 @@
    L
-ogger.instance
+OGGER
 .err
@@ -3045,16 +3045,19 @@
  cache!%22
+, e
 );%0A    %7D

Convert DiskCache from old logger to new LOGGER slf4j