RAG / sqlite_cache.py
bakhil-aissa's picture
Upload 12 files
61fc96a verified
raw
history blame contribute delete
937 Bytes
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()