diff options
author | Alon Zakai <alonzakai@gmail.com> | 2012-11-18 09:37:46 -0800 |
---|---|---|
committer | Alon Zakai <alonzakai@gmail.com> | 2012-11-21 20:47:01 +0100 |
commit | 8fce64dd18afaa0c49fb59cf161fa2fadeac6e7a (patch) | |
tree | 8d445e5894c056ec1e8c271d8083f4479fbc62a4 /tools | |
parent | 6af60b71376b8bf0c9a6fa41f2d4f2a0ec9ded8b (diff) |
basic jcache on pre with files+cPickle
Diffstat (limited to 'tools')
-rw-r--r-- | tools/shared.py | 27 |
1 files changed, 24 insertions, 3 deletions
diff --git a/tools/shared.py b/tools/shared.py index b445051d..18c3cea7 100644 --- a/tools/shared.py +++ b/tools/shared.py @@ -1,4 +1,4 @@ -import shutil, time, os, sys, json, tempfile, copy, shlex, atexit, subprocess +import shutil, time, os, sys, json, tempfile, copy, shlex, atexit, subprocess, md5, cPickle from subprocess import Popen, PIPE, STDOUT from tempfile import mkstemp @@ -1168,18 +1168,39 @@ class JCache: keys = [keys] ret = '' for key in keys: + assert type(key) == str ret += md5.md5(key).hexdigest() return ret + @staticmethod + def get_cachename(shortkey): + return os.path.join(JCache.dirname, shortkey) + # Returns a cached value, if it exists. Make sure the full key matches @staticmethod def get(shortkey, keys): - return None + cachename = JCache.get_cachename(shortkey) + if not os.path.exists(cachename): return + data = cPickle.Unpickler(open(cachename, 'rb')).load() + if len(data) != 2: + if DEBUG: print >> sys.stderr, 'jcache error in get' + return + oldkeys = data[0] + if len(oldkeys) != len(keys): + if DEBUG: print >> sys.stderr, 'jcache collision (a)' + return + for i in range(len(oldkeys)): + if oldkeys[i] != keys[i]: + if DEBUG: print >> sys.stderr, 'jcache collision (b)' + return + if DEBUG: print >> sys.stderr, 'jcache win' + return data[1] # Sets the cached value for a key (from get_key) @staticmethod def set(shortkey, keys, value): - pass + cachename = JCache.get_cachename(shortkey) + cPickle.Pickler(open(cachename, 'wb')).dump([keys, value]) # Compression of code and data for smaller downloads class Compression: |