+import logging
+import os
+import sqlite3
+import time
+
+import misc
+
+class Registry:
+ def __init__(self, db_file):
+ if not db_file: return
+ if not os.path.isfile(db_file):
+ self._conn = sqlite3.connect(db_file, check_same_thread=False)
+ self._create_db()
+ else:
+ self._conn = sqlite3.connect(db_file, check_same_thread=False)
+
+ def is_registered(self, name):
+ if not db_file: return False
+ c = self._conn.cursor()
+ c.execute("SELECT COUNT(*) FROM cache WHERE name=?", (name,))
+ return c.fetchone() != None
+
+ def clean(self):
+ if not db_file: return
+ c = self._conn.cursor()
+ c.execute("DELETE FROM cache WHERE access_time<?", (int(time.time()) - 604800,))
+ self._conn.commit()
+
+
+ def register(self, name):
+ if not db_file: return
+ c = self._conn.cursor()
+ c.execute( '''INSERT INTO cache (name, access_time) VALUES (?, ?)''',
+ (name, int(time.time())) )
+ self._conn.commit()
+
+ def refresh(self, name):
+ if not db_file: return
+ c = self._conn.cursor()
+ c.execute( "UPDATE cache SET access_time=? WHERE name=?",
+ (int(time.time()), name) )
+ self._conn.commit()
+
+ def _create_db(self):
+ c = self._conn.cursor()
+ c.execute('''CREATE TABLE cache (name TEXT PRIMARY KEY, access_time INTEGER)''')
+ self._conn.commit()