Commits

Grigoriy Petukhov committed 475e0ff

Add has_item method to spider mysql cache backend

Comments (0)

Files changed (1)

grab/spider/cache_backend/mysql.py

         self.cursor.execute('BEGIN')
         self.cursor.execute('TRUNCATE cache')
         self.cursor.execute('COMMIT')
+
+    def has_item(self, url, timeout=None):
+        """
+        Test if required item exists in the cache.
+        """
+
+        _hash = self.build_hash(url)
+        with self.spider.save_timer('cache.read.mysql_query'):
+            if timeout is None:
+                query = ""
+            else:
+                ts = int(time.time()) - timeout
+                query = " AND timestamp > %d" % ts
+            res = self.cursor.execute('''
+                SELECT id
+                FROM cache
+                WHERE id = x%%s %(query)s
+                LIMIT 1
+                ''' % {'query': query},
+                (_hash,))
+            row = self.cursor.fetchone()
+        return True if row else False
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.