Commits

Rune Halvorsen committed beb9137

Added caching to searches

  • Participants
  • Parent commits 3f47495

Comments (0)

Files changed (1)

File spotimeta/__init__.py

 
 class Metadata(object):
 
-    def __init__(self, cache={}, rate=10, timeout=None, user_agent=None):
+    def __init__(self, cache=None, rate=10, timeout=None, user_agent=None):
         self.cache = cache # not implemented yet
         self.rate = rate # not implemented yet
         self.timeout = timeout
 
         if fp: # We got data, sweet
             res = parse_lookup_doc(fp, uri=uri)
-        # else: we did not get data, means nothing has changes, rewarm cache
 
         self._cache_put(key, res, new_modified or modified, new_expires or expires)
         return res
         return self._do_search(url, term, page)
 
     def _do_search(self, url, term, page):
+        key = "%s:%s" % (term, page)
+
+        res, modified, expires = self._cache_get(key)
+        if res and time.time() < expires:
+            return res
+
         query = {"q": term}
 
         if page is not None:
             query["page"] = str(page)
 
-        fp, modified, expires = self._get_url(url, query)
-        res = parse_search_doc(fp)
+        fp, new_modified, new_expires = self._get_url(url, query, modified)
+
+        if fp: # We got data, sweet
+            res = parse_search_doc(fp)
+
+        self._cache_put(key, res, new_modified or modified, new_expires or expires)
+
         return res
 
     def _cache_get(self, key):