Commit dea0d28a by Calen Pennington

Url encode memcache keys, rather than writing a new encoding scheme. Also use…

Url encode memcache keys, rather than writing a new encoding scheme. Also use fasthash, rather than md5
parent 88ff84dd
...@@ -7,7 +7,6 @@ Does some caching (to be explained). ...@@ -7,7 +7,6 @@ Does some caching (to be explained).
''' '''
import hashlib
import logging import logging
import os import os
import re import re
...@@ -16,6 +15,7 @@ import urllib ...@@ -16,6 +15,7 @@ import urllib
from datetime import timedelta from datetime import timedelta
from lxml import etree from lxml import etree
from util.memcache import fasthash
try: # This lets us do __name__ == ='__main__' try: # This lets us do __name__ == ='__main__'
from django.conf import settings from django.conf import settings
...@@ -57,10 +57,6 @@ def parse_timedelta(time_str): ...@@ -57,10 +57,6 @@ def parse_timedelta(time_str):
time_params[name] = int(param) time_params[name] = int(param)
return timedelta(**time_params) return timedelta(**time_params)
def fasthash(string):
m = hashlib.new("md4")
m.update(string)
return "id"+m.hexdigest()
def xpath(xml, query_string, **args): def xpath(xml, query_string, **args):
''' Safe xpath query into an xml tree: ''' Safe xpath query into an xml tree:
...@@ -160,11 +156,11 @@ def user_groups(user): ...@@ -160,11 +156,11 @@ def user_groups(user):
cache_expiration = 60 * 60 # one hour cache_expiration = 60 * 60 # one hour
# Kill caching on dev machines -- we switch groups a lot # Kill caching on dev machines -- we switch groups a lot
group_names = cache.get(fasthash(key)) group_names = cache.get(key)
if group_names is None: if group_names is None:
group_names = [u.name for u in UserTestGroup.objects.filter(users=user)] group_names = [u.name for u in UserTestGroup.objects.filter(users=user)]
cache.set(fasthash(key), group_names, cache_expiration) cache.set(key, group_names, cache_expiration)
return group_names return group_names
...@@ -203,7 +199,7 @@ def course_file(user,coursename=None): ...@@ -203,7 +199,7 @@ def course_file(user,coursename=None):
cache_key = filename + "_processed?dev_content:" + str(options['dev_content']) + "&groups:" + str(sorted(groups)) cache_key = filename + "_processed?dev_content:" + str(options['dev_content']) + "&groups:" + str(sorted(groups))
if "dev" not in settings.DEFAULT_GROUPS: if "dev" not in settings.DEFAULT_GROUPS:
tree_string = cache.get(fasthash(cache_key)) tree_string = cache.get(cache_key)
else: else:
tree_string = None tree_string = None
...@@ -211,7 +207,7 @@ def course_file(user,coursename=None): ...@@ -211,7 +207,7 @@ def course_file(user,coursename=None):
tree = course_xml_process(etree.XML(render_to_string(filename, options, namespace = 'course'))) tree = course_xml_process(etree.XML(render_to_string(filename, options, namespace = 'course')))
tree_string = etree.tostring(tree) tree_string = etree.tostring(tree)
cache.set(fasthash(cache_key), tree_string, 60) cache.set(cache_key, tree_string, 60)
else: else:
tree = etree.XML(tree_string) tree = etree.XML(tree_string)
......
...@@ -2,17 +2,19 @@ ...@@ -2,17 +2,19 @@
This module provides a KEY_FUNCTION suitable for use with a memcache backend This module provides a KEY_FUNCTION suitable for use with a memcache backend
so that we can cache any keys, not just ones that memcache would ordinarily accept so that we can cache any keys, not just ones that memcache would ordinarily accept
""" """
from django.utils.hashcompat import md5_constructor
from django.utils.encoding import smart_str from django.utils.encoding import smart_str
import string import hashlib
import urllib
def fasthash(string):
m = hashlib.new("md4")
m.update(string)
return "id"+m.hexdigest()
def safe_key(key, key_prefix, version): def safe_key(key, key_prefix, version):
safe_key = smart_str(key) safe_key = urllib.quote_plus(smart_str(key))
for char in safe_key:
if ord(char) < 33 or ord(char) == 127:
safe_key = safe_key.replace(char, '_')
if len(safe_key) > 250: if len(safe_key) > 250:
safe_key = md5_constructor(safe_key) safe_key = fasthash(safe_key)
return ":".join([key_prefix, str(version), safe_key]) return ":".join([key_prefix, str(version), safe_key])
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment