Have the --clear-cache option only clear the cache once. Fixes #9
authorJan-Pascal van Best <janpascal@vanbest.org>
Wed, 25 Apr 2012 18:31:45 +0000 (20:31 +0200)
committerJan-Pascal van Best <janpascal@vanbest.org>
Wed, 25 Apr 2012 18:31:45 +0000 (20:31 +0200)
src/main/java/org/vanbest/xmltv/AbstractEPGSource.java
src/main/java/org/vanbest/xmltv/Main.java
src/main/java/org/vanbest/xmltv/ProgrammeCache.java

index b24e27aebb9148a9fd7f8951456bc8ed7f9d1ba0..57769ac99c9ff42c33ff3fbfc47ee1b4dfa41bc6 100644 (file)
@@ -76,8 +76,6 @@ public abstract class AbstractEPGSource implements EPGSource {
        }
        
        public void clearCache() {
-               ProgrammeCache cache = new ProgrammeCache(config);
-               cache.clear();
-               cache.close();
+               cache.clear(sourceId);
        }
 }
\ No newline at end of file
index 843b9dc562459bb38f4547771cd26d4a2f6ee2d0..60c0d2536d95c574ccf49dbb4950f80a687f9dae 100644 (file)
@@ -79,7 +79,11 @@ public class Main {
                        System.out.println("... from " + enabledCount + " channels");
                        System.out.println("... using cache at " + config.cacheDbHandle);
                }
-               
+               if (clearCache) {
+                       ProgrammeCache cache = new ProgrammeCache(config);
+                       cache.clear();
+                       cache.close();
+               }
                Map<Integer,EPGSource> guides = new HashMap<Integer,EPGSource>();
                EPGSourceFactory factory = EPGSourceFactory.newInstance();
                //EPGSource gids = new TvGids(config);
@@ -106,7 +110,6 @@ public class Main {
                                if (!config.quiet) System.out.print(".");
                                if(!guides.containsKey(c.source)) {
                                        guides.put(c.source, factory.createEPGSource(c.source, config));
-                                       if (clearCache) guides.get(c.source).clearCache();
                                }
                                List<Programme> programmes = guides.get(c.source).getProgrammes(c, day);
                                for (Programme p: programmes) p.serialize(writer);
index 55272bb57a933ac9dc7d2075ff44d1d4eb93172a..c48f16516fc9e0276938ae7aa612c03d22d85aec 100644 (file)
@@ -45,6 +45,7 @@ public class ProgrammeCache {
        private PreparedStatement putStatement;
        private PreparedStatement removeStatement;
        private PreparedStatement clearStatement;
+       private PreparedStatement clearSourceStatement;
        
        private final static Integer SCHEMA_VERSION=1;
        private final static String SCHEMA_KEY="TV_GRAB_NL_JAVA_SCHEMA_VERSION";
@@ -133,6 +134,7 @@ public class ProgrammeCache {
                                putStatement = db.prepareStatement("INSERT INTO cache VALUES (?,?,?,?)");
                                removeStatement = db.prepareStatement("DELETE FROM cache WHERE source=? AND id=?");
                                clearStatement = db.prepareStatement("DELETE FROM cache");
+                               clearSourceStatement = db.prepareStatement("DELETE FROM cache WHERE source=?");
                        } catch (SQLException e) {
                                if (!config.quiet) {
                                        System.out.println("Unable to prepare statements, proceeding without cache");
@@ -226,6 +228,19 @@ public class ProgrammeCache {
                }
        }
 
+       public void clear(int source) {
+               if (db==null) return;
+               try {
+                       clearSourceStatement.setInt(1, source);
+                       int count = clearSourceStatement.executeUpdate();
+                       if (!config.quiet && count>0) {
+                               System.out.println("Cleared " + count + " entries from cache for source " + source);
+                       }
+               } catch (SQLException e) {
+                       if (config.logLevel>=Config.LOG_DEBUG) e.printStackTrace();
+               }
+       }
+
        public void close() {
                cleanup();
                if (db != null) {
@@ -234,6 +249,7 @@ public class ProgrammeCache {
                                putStatement.close();
                                removeStatement.close();
                                clearStatement.close();
+                               clearSourceStatement.close();
                                db.close();
                        } catch (SQLException e) {
                                // TODO Auto-generated catch block
@@ -241,4 +257,5 @@ public class ProgrammeCache {
                        }
                }
        }
+
 }