From f29a107a092fcf614d1831c9902ef1867fc7953c Mon Sep 17 00:00:00 2001 From: pictuga Date: Sat, 4 Nov 2017 14:48:00 +0100 Subject: [PATCH] crawler: make SQLiteCache inherit from BaseCache Saves some time for other cache backends --- morss/crawler.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/morss/crawler.py b/morss/crawler.py index 1c918cf..1b37ef9 100644 --- a/morss/crawler.py +++ b/morss/crawler.py @@ -472,10 +472,22 @@ class CacheHandler(BaseHandler): https_response = http_response +class BaseCache: + def __contains__(self, url): + try: + self[url] + + except KeyError: + return False + + else: + return True + + import sqlite3 -class SQLiteCache: +class SQLiteCache(BaseCache): def __init__(self, filename=':memory:'): self.con = sqlite3.connect(filename or sqlite_default, detect_types=sqlite3.PARSE_DECLTYPES, check_same_thread=False) @@ -495,7 +507,7 @@ class SQLiteCache: return row[1:] def __setitem__(self, url, value): # value = (code, msg, headers, data, timestamp) - if self.con.execute('SELECT code FROM data WHERE url=?', (url,)).fetchone(): + if url in self: with self.con: self.con.execute('UPDATE data SET code=?, msg=?, headers=?, data=?, timestamp=? WHERE url=?', value + (url,))