Spaces:
Sleeping
Sleeping
File size: 937 Bytes
61fc96a | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | import sqlite3
import time
class SQLiteCache:
def __init__(self, db_path):
self.conn = sqlite3.connect(db_path)
self.conn.execute('''CREATE TABLE IF NOT EXISTS cache (id INTEGER PRIMARY KEY AUTOINCREMENT, hash_doc TEXT, timestamp INTEGER)''')
self.conn.commit()
def get(self, key):
cursor = self.conn.execute('SELECT hash_doc FROM cache WHERE hash_doc = ?', (key,))
row = cursor.fetchone()
return row[0] if row else None
def set(self, key):
self.conn.execute('INSERT INTO cache (hash_doc, timestamp) VALUES ( ?, ?)', (key , time.time()))
self.conn.commit()
def close(self):
self.conn.close()
def destroy(self):
self.conn.execute('DROP TABLE IF EXISTS cache')
self.conn.commit()
if __name__ == "__main__":
cache = SQLiteCache("cache.db")
cache.set("test")
print(cache.get("test"))
cache.close()
|