Skip to content

Commit

Permalink
Merge pull request #3246 from peterbe/bug-1257655-remove-memoize-func…
Browse files Browse the repository at this point in the history
…tion-for-crashstats-models

fixes bug 1257655 - Remove memoize function for crashstats models
  • Loading branch information
Peter Bengtsson committed Mar 18, 2016
2 parents 9893023 + d4ed2a3 commit 0e78592
Showing 1 changed file with 0 additions and 94 deletions.
94 changes: 0 additions & 94 deletions webapp-django/crashstats/crashstats/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -195,99 +195,7 @@ def inner(*args, **kwargs):
return inner


def memoize(function):
"""Decorator for model methods to cache in memory or the filesystem
using CACHE_MIDDLEWARE and/or CACHE_MIDDLEWARE_FILES Django config"""

@functools.wraps(function)
def memoizer(instance, *args, **kwargs):

def get_cached_result(key, instance, stringified_args):
result = cache.get(key)
if result is not None:
logger.debug("CACHE HIT %s" % stringified_args)
return result

# Didn't find key in middleware_cache, so try filecache
cache_file = get_cache_filename(key, instance)
if settings.CACHE_MIDDLEWARE_FILES and os.path.isfile(cache_file):
# but is it fresh enough?
age = time.time() - os.stat(cache_file)[stat.ST_MTIME]
if age > instance.cache_seconds:
logger.debug("CACHE FILE TOO OLD")
os.remove(cache_file)
else:
logger.debug("CACHE FILE HIT %s" % stringified_args)
delete_cache_file = False
with open(cache_file) as f:
if instance.expect_json:
try:
return json.load(f)
except ValueError:
logger.warn(
"%s is not a valid JSON file and will "
"be deleted" % (
cache_file,
)
)
delete_cache_file = True
else:
return f.read()
if delete_cache_file:
os.remove(cache_file)

# Didn't find our values in the cache
return None

def get_cache_filename(key, instance):
root = settings.CACHE_MIDDLEWARE_FILES
if isinstance(root, bool):
cache_file = os.path.join(
settings.ROOT,
'models-cache'
)
else:
cache_file = root

cache_file = os.path.join(cache_file, classname, key)
cache_file += instance.expect_json and '.json' or '.dump'
return cache_file

def refresh_caches(key, instance, result):
cache.set(key, result, instance.cache_seconds)
cache_file = get_cache_filename(key, instance)
if cache_file and settings.CACHE_MIDDLEWARE_FILES:
if not os.path.isdir(os.path.dirname(cache_file)):
os.makedirs(os.path.dirname(cache_file))
with open(cache_file, 'w') as f:
if instance.expect_json:
json.dump(result, f, indent=2)
else:
f.write(result)

# Check if item is in the cache and call the decorated method if needed
do_cache = settings.CACHE_MIDDLEWARE and instance.cache_seconds
if do_cache:
classname = instance.__class__.__name__
stringified_args = classname + " " + str(kwargs)
key = hashlib.md5(stringified_args).hexdigest()
result = get_cached_result(key, instance, stringified_args)
if result is not None:
return result

# Didn't find it in the cache or not using a cache, so run our function
result = function(instance, *args, **kwargs)

if do_cache:
refresh_caches(key, instance, result)
return result

return memoizer


class SocorroCommon(object):
""" Soon to be deprecated by classes using socorro dataservice classes
and memoize decorator """

# by default, we don't need username and password
username = password = None
Expand Down Expand Up @@ -549,8 +457,6 @@ def clear_implementations_cache(cls):


class SocorroMiddleware(SocorroCommon):
""" Soon to be deprecated by classes using socorro dataservice classes
and memoize decorator """

# by default, assume the class to not have an implementation reference
implementation = None
Expand Down

0 comments on commit 0e78592

Please sign in to comment.