src/hdalab/utils.py
author cavaliet
Tue, 17 Jun 2014 10:25:33 +0200
changeset 271 8f77cf71ab02
parent 156 web/hdalab/utils.py@3d70078fe90a
child 290 fb86765b4c54
permissions -rw-r--r--
commit the venv update (django and dependancies) in the good head
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
154
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents:
diff changeset
     1
# -*- coding: utf-8 -*-
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents:
diff changeset
     2
'''
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents:
diff changeset
     3
Created on Mar 13, 2012
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents:
diff changeset
     4
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents:
diff changeset
     5
@author: ymh
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents:
diff changeset
     6
'''
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents:
diff changeset
     7
from django.core.cache import cache
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents:
diff changeset
     8
from django.utils.encoding import smart_str
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents:
diff changeset
     9
import md5
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents:
diff changeset
    10
import re
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents:
diff changeset
    11
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents:
diff changeset
    12
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents:
diff changeset
    13
def fix_cache_key(key):
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents:
diff changeset
    14
    cache_key = re.sub(r'\s+', '-', key)
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents:
diff changeset
    15
    cache_key = smart_str(cache_key)
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents:
diff changeset
    16
    if len(cache_key) > (250-(2+len(cache.key_prefix)+len(str(cache.version)))-33):
8527c5a3ddb7 add cache management to filter
ymh <ymh.work@gmail.com>
parents:
diff changeset
    17
        cache_key = cache_key[:(250-(2+len(cache.key_prefix)+len(str(cache.version)))-33)] + '-' + md5.new(cache_key).hexdigest()
156
3d70078fe90a correct stupid bug in cache key
ymh <ymh.work@gmail.com>
parents: 154
diff changeset
    18
    return cache_key