Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #18330 - Made cache culling 3rd party db backend friendly

This is Ian Kelly's patch from #15580 with minor modifications.
  • Loading branch information...
commit d3c2eb103f6682c029a850e60dc4cf85896b6aa2 1 parent 0f49b2b
Anssi Kääriäinen authored July 05, 2012
14  django/core/cache/backends/db.py
@@ -167,17 +167,9 @@ def _cull(self, db, cursor, now):
167 167
             num = cursor.fetchone()[0]
168 168
             if num > self._max_entries:
169 169
                 cull_num = num / self._cull_frequency
170  
-                if connections[db].vendor == 'oracle':
171  
-                    # Oracle doesn't support LIMIT + OFFSET
172  
-                    cursor.execute("""SELECT cache_key FROM
173  
-(SELECT ROW_NUMBER() OVER (ORDER BY cache_key) AS counter, cache_key FROM %s)
174  
-WHERE counter > %%s AND COUNTER <= %%s""" % table, [cull_num, cull_num + 1])
175  
-                else:
176  
-                    # This isn't standard SQL, it's likely to break
177  
-                    # with some non officially supported databases
178  
-                    cursor.execute("SELECT cache_key FROM %s "
179  
-                                   "ORDER BY cache_key "
180  
-                                   "LIMIT 1 OFFSET %%s" % table, [cull_num])
  170
+                cursor.execute(
  171
+                    connections[db].ops.cache_key_culling_sql() % table,
  172
+                    [cull_num])
181 173
                 cursor.execute("DELETE FROM %s "
182 174
                                "WHERE cache_key < %%s" % table,
183 175
                                [cursor.fetchone()[0]])
10  django/db/backends/__init__.py
@@ -475,6 +475,16 @@ def autoinc_sql(self, table, column):
475 475
         """
476 476
         return None
477 477
 
  478
+    def cache_key_culling_sql(self):
  479
+        """
  480
+        Returns a SQL query that retrieves the first cache key greater than the
  481
+        n smallest.
  482
+
  483
+        This is used by the 'db' cache backend to determine where to start
  484
+        culling.
  485
+        """
  486
+        return "SELECT cache_key FROM %s ORDER BY cache_key LIMIT 1 OFFSET %%s"
  487
+
478 488
     def date_extract_sql(self, lookup_type, field_name):
479 489
         """
480 490
         Given a lookup_type of 'year', 'month' or 'day', returns the SQL that
7  django/db/backends/oracle/base.py
@@ -118,6 +118,13 @@ def autoinc_sql(self, table, column):
118 118
 /""" % locals()
119 119
         return sequence_sql, trigger_sql
120 120
 
  121
+    def cache_key_culling_sql(self):
  122
+        return """
  123
+            SELECT cache_key
  124
+              FROM (SELECT cache_key, rank() OVER (ORDER BY cache_key) AS rank FROM %s)
  125
+             WHERE rank = %%s + 1
  126
+        """
  127
+
121 128
     def date_extract_sql(self, lookup_type, field_name):
122 129
         # http://download-east.oracle.com/docs/cd/B10501_01/server.920/a96540/functions42a.htm#1017163
123 130
         if lookup_type == 'week_day':

0 notes on commit d3c2eb1

Please sign in to comment.
Something went wrong with that request. Please try again.