Exemplo n.º 1
0
	def __init__(self, DB_Object=None):
		if DB_Object is None:
			self.dbf = vfs.join(CACHE, 'cached.db')
			self.db_lock = FileLock(self.dbf + '.lock')
			self.dbc = False
			self.dbh = False
			self.connect()
		else:
			self.DB=DB_Object
			def cache_response(url, response, cache_limit):
				if cache_limit == 0:
					return False
				self.DB.execute("REPLACE INTO request_cache(url, response) VALUES(?,?)", [url, response])
				self.DB.commit()
			self.cache_response = cache_response
			def get_cached_response(url, cache_limit):
				if cache_limit == 0:
					return False
				else:
					cache_limit = float(cache_limit) * 3600
				if cache_limit == -1:
					results = self.DB.query("SELECT response FROM cached_requests WHERE url=?", [url], force_double_array=False)
				elif callable(cache_limit):
					if cache_limit():
						results = self.DB.query("SELECT response FROM cached_requests WHERE url=?", [url], force_double_array=False)
					else: return False
				else:
					results = self.DB.query("SELECT response FROM cached_requests WHERE age < ? AND url=?", [cache_limit, url], force_double_array=False)
				if results:
					return results[0]
				return False
			self.get_cached_response = get_cached_response
Exemplo n.º 2
0
	def __init__(self, DB_Object=None):
		if DB_Object is None:
			self.dbf = vfs.join(CACHE, 'cached.db')
			self.db_lock = FileLock(self.dbf + '.lock')
			self.dbc = False
			self.dbh = False
			self.connect()
		else:
			self.DB=DB_Object
			def cache_response(url, response, cache_limit):
				if cache_limit == 0:
					return False
				self.DB.execute("REPLACE INTO request_cache(url, response) VALUES(?,?)", [url, response])
				self.DB.commit()
			self.cache_response = cache_response
			def get_cached_response(url, cache_limit):
				if cache_limit == 0:
					return False
				else:
					cache_limit = float(cache_limit) * 3600
				if cache_limit == -1:
					results = self.DB.query("SELECT response FROM cached_requests WHERE url=?", [url], force_double_array=False)
				elif callable(cache_limit):
					if cache_limit():
						results = self.DB.query("SELECT response FROM cached_requests WHERE url=?", [url], force_double_array=False)
					else: return False
				else:
					results = self.DB.query("SELECT response FROM cached_requests WHERE age < ? AND url=?", [cache_limit, url], force_double_array=False)
				if results:
					return results[0]
				return False
			self.get_cached_response = get_cached_response
Exemplo n.º 3
0
class DB_CACHABLE_API(CACHABLE_API):
	custom_tables = False
	DB = False
	connected = False
	create_statements = [
		"""CREATE TABLE IF NOT EXISTS "version" (
			"db_version" INTEGER DEFAULT 1 UNIQUE,
			PRIMARY KEY(db_version));
		""",
		"""CREATE TABLE IF NOT EXISTS "request_cache" (
			"request_id" INTEGER PRIMARY KEY AUTOINCREMENT,
			"url" TEXT UNIQUE,
			"response" TEXT,
			"headers" TEXT,
			"ts" TIMESTAMP DEFAULT CURRENT_TIMESTAMP);
		""",
		"""CREATE VIEW IF NOT EXISTS "cached_requests" AS 
			SELECT request_cache.*, (strftime('%s','now') -  strftime('%s',ts))  AS age 
			FROM request_cache;
		""",
		"""INSERT INTO version(db_version) VALUES(1);"""
	]
	def __init__(self, DB_Object=None):
		if DB_Object is None:
			self.dbf = vfs.join(CACHE, 'cached.db')
			self.db_lock = FileLock(self.dbf + '.lock')
			self.dbc = False
			self.dbh = False
			self.connect()
		else:
			self.DB=DB_Object
			def cache_response(url, response, cache_limit):
				if cache_limit == 0:
					return False
				self.DB.execute("REPLACE INTO request_cache(url, response) VALUES(?,?)", [url, response])
				self.DB.commit()
			self.cache_response = cache_response
			def get_cached_response(url, cache_limit):
				if cache_limit == 0:
					return False
				else:
					cache_limit = float(cache_limit) * 3600
				if cache_limit == -1:
					results = self.DB.query("SELECT response FROM cached_requests WHERE url=?", [url], force_double_array=False)
				elif callable(cache_limit):
					if cache_limit():
						results = self.DB.query("SELECT response FROM cached_requests WHERE url=?", [url], force_double_array=False)
					else: return False
				else:
					results = self.DB.query("SELECT response FROM cached_requests WHERE age < ? AND url=?", [cache_limit, url], force_double_array=False)
				if results:
					return results[0]
				return False
			self.get_cached_response = get_cached_response
		
	def connect(self):
		self.dbh = database.connect(self.dbf, check_same_thread=False)
		self.dbc = self.dbh.cursor()
		with self.db_lock:
			try:
				self.query("SELECT db_version FROM version")
			except:
				if self.custom_tables:
					statements = self.create_statements + self.custom_tables
				else:
					statements = self.create_statements
				for SQL in statements:
					self.execute(SQL)
				self.commit()
		self.connected = True
	
	def commit(self):
		self.dbh.commit()
	
	def prepaire_sql(self, SQL):
		if SQL.upper().startswith('REPLACE INTO'): SQL = 'INSERT OR ' + SQL
		return SQL
		
	def query(self, SQL, data=[]):
		SQL = self.prepaire_sql(SQL)
		self.dbc.execute(SQL, data)
		return self.dbc.fetchall()

	def execute(self, SQL, data=[]):
		SQL = self.prepaire_sql(SQL)
		self.dbc.execute(SQL, data)
			
	def get_cached_response(self, url, cache_limit):
		if cache_limit == 0:
			return False
		else:
			cache_limit = float(cache_limit) * 3600
		with self.db_lock:
			if cache_limit == -1:
				self.execute("SELECT response FROM cached_requests WHERE url=?", [url])
			elif cache_limit == -2:
				self.execute("DELETE FROM cached_requests WHERE url=?", [url])
				self.commit()
				self.db_lock.release()
				return False
			elif callable(cache_limit):
				if cache_limit():
					self.execute("SELECT response FROM cached_requests WHERE url=?", [url])
					self.db_lock.release()
				else: return False
			else:
				self.execute("SELECT response FROM cached_requests WHERE age < ? AND url=?", [cache_limit, url])
			results = self.dbc.fetchone()
			if results:
				self.db_lock.release()
				return results[0]
		return False
	
	def cache_response(self, url, response, cache_limit):
		if cache_limit == 0:
			return False
		with self.db_lock:
			self.execute("REPLACE INTO request_cache(url, response) VALUES(?,?)", [url, response])
			self.commit()
Exemplo n.º 4
0
class DB_CACHABLE_API(CACHABLE_API):
	custom_tables = False
	DB = False
	connected = False
	create_statements = [
		"""CREATE TABLE IF NOT EXISTS "version" (
			"db_version" INTEGER DEFAULT 1 UNIQUE,
			PRIMARY KEY(db_version));
		""",
		"""CREATE TABLE IF NOT EXISTS "request_cache" (
			"request_id" INTEGER PRIMARY KEY AUTOINCREMENT,
			"url" TEXT UNIQUE,
			"response" TEXT,
			"headers" TEXT,
			"ts" TIMESTAMP DEFAULT CURRENT_TIMESTAMP);
		""",
		"""CREATE VIEW IF NOT EXISTS "cached_requests" AS 
			SELECT request_cache.*, (strftime('%s','now') -  strftime('%s',ts))  AS age 
			FROM request_cache;
		""",
		"""INSERT INTO version(db_version) VALUES(1);"""
	]
	def __init__(self, DB_Object=None):
		if DB_Object is None:
			self.dbf = vfs.join(CACHE, 'cached.db')
			self.db_lock = FileLock(self.dbf + '.lock')
			self.dbc = False
			self.dbh = False
			self.connect()
		else:
			self.DB=DB_Object
			def cache_response(url, response, cache_limit):
				if cache_limit == 0:
					return False
				self.DB.execute("REPLACE INTO request_cache(url, response) VALUES(?,?)", [url, response])
				self.DB.commit()
			self.cache_response = cache_response
			def get_cached_response(url, cache_limit):
				if cache_limit == 0:
					return False
				else:
					cache_limit = float(cache_limit) * 3600
				if cache_limit == -1:
					results = self.DB.query("SELECT response FROM cached_requests WHERE url=?", [url], force_double_array=False)
				elif callable(cache_limit):
					if cache_limit():
						results = self.DB.query("SELECT response FROM cached_requests WHERE url=?", [url], force_double_array=False)
					else: return False
				else:
					results = self.DB.query("SELECT response FROM cached_requests WHERE age < ? AND url=?", [cache_limit, url], force_double_array=False)
				if results:
					return results[0]
				return False
			self.get_cached_response = get_cached_response
		
	def connect(self):
		self.dbh = database.connect(self.dbf, check_same_thread=False)
		self.dbc = self.dbh.cursor()
		with self.db_lock:
			try:
				self.query("SELECT db_version FROM version")
			except:
				if self.custom_tables:
					statements = self.create_statements + self.custom_tables
				else:
					statements = self.create_statements
				for SQL in statements:
					self.execute(SQL)
				self.commit()
		self.connected = True
	
	def commit(self):
		self.dbh.commit()
	
	def prepaire_sql(self, SQL):
		if SQL.upper().startswith('REPLACE INTO'): SQL = 'INSERT OR ' + SQL
		return SQL
		
	def query(self, SQL, data=[]):
		SQL = self.prepaire_sql(SQL)
		self.dbc.execute(SQL, data)
		return self.dbc.fetchall()

	def execute(self, SQL, data=[]):
		SQL = self.prepaire_sql(SQL)
		self.dbc.execute(SQL, data)
			
	def get_cached_response(self, url, cache_limit):
		if cache_limit == 0:
			return False
		else:
			cache_limit = float(cache_limit) * 3600
		with self.db_lock:
			if cache_limit == -1:
				self.execute("SELECT response FROM cached_requests WHERE url=?", [url])
			elif cache_limit == -2:
				self.execute("DELETE FROM cached_requests WHERE url=?", [url])
				self.commit()
				self.db_lock.release()
				return False
			elif callable(cache_limit):
				if cache_limit():
					self.execute("SELECT response FROM cached_requests WHERE url=?", [url])
					self.db_lock.release()
				else: return False
			else:
				self.execute("SELECT response FROM cached_requests WHERE age < ? AND url=?", [cache_limit, url])
			results = self.dbc.fetchone()
			if results:
				self.db_lock.release()
				return results[0]
		return False
	
	def cache_response(self, url, response, cache_limit):
		if cache_limit == 0:
			return False
		with self.db_lock:
			self.execute("REPLACE INTO request_cache(url, response) VALUES(?,?)", [url, response])
			self.commit()