Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

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 akaariai authored
14 django/core/cache/backends/db.py
View
@@ -167,17 +167,9 @@ def _cull(self, db, cursor, now):
num = cursor.fetchone()[0]
if num > self._max_entries:
cull_num = num / self._cull_frequency
- if connections[db].vendor == 'oracle':
- # Oracle doesn't support LIMIT + OFFSET
- cursor.execute("""SELECT cache_key FROM
-(SELECT ROW_NUMBER() OVER (ORDER BY cache_key) AS counter, cache_key FROM %s)
-WHERE counter > %%s AND COUNTER <= %%s""" % table, [cull_num, cull_num + 1])
- else:
- # This isn't standard SQL, it's likely to break
- # with some non officially supported databases
- cursor.execute("SELECT cache_key FROM %s "
- "ORDER BY cache_key "
- "LIMIT 1 OFFSET %%s" % table, [cull_num])
+ cursor.execute(
+ connections[db].ops.cache_key_culling_sql() % table,
+ [cull_num])
cursor.execute("DELETE FROM %s "
"WHERE cache_key < %%s" % table,
[cursor.fetchone()[0]])
10 django/db/backends/__init__.py
View
@@ -475,6 +475,16 @@ def autoinc_sql(self, table, column):
"""
return None
+ def cache_key_culling_sql(self):
+ """
+ Returns a SQL query that retrieves the first cache key greater than the
+ n smallest.
+
+ This is used by the 'db' cache backend to determine where to start
+ culling.
+ """
+ return "SELECT cache_key FROM %s ORDER BY cache_key LIMIT 1 OFFSET %%s"
+
def date_extract_sql(self, lookup_type, field_name):
"""
Given a lookup_type of 'year', 'month' or 'day', returns the SQL that
7 django/db/backends/oracle/base.py
View
@@ -118,6 +118,13 @@ def autoinc_sql(self, table, column):
/""" % locals()
return sequence_sql, trigger_sql
+ def cache_key_culling_sql(self):
+ return """
+ SELECT cache_key
+ FROM (SELECT cache_key, rank() OVER (ORDER BY cache_key) AS rank FROM %s)
+ WHERE rank = %%s + 1
+ """
+
def date_extract_sql(self, lookup_type, field_name):
# http://download-east.oracle.com/docs/cd/B10501_01/server.920/a96540/functions42a.htm#1017163
if lookup_type == 'week_day':
Please sign in to comment.
Something went wrong with that request. Please try again.