Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- diff --git a/src/whoosh/util/cache.py b/src/whoosh/util/cache.py
- --- a/src/whoosh/util/cache.py
- +++ b/src/whoosh/util/cache.py
- @@ -80,33 +80,37 @@
- stats = [0, 0] # Hits, misses
- data = {}
- lastused = {}
- + lock = Lock()
- @functools.wraps(user_function)
- def wrapper(*args):
- - try:
- - result = data[args]
- - stats[0] += 1 # Hit
- - except KeyError:
- - stats[1] += 1 # Miss
- - if len(data) == maxsize:
- - for k, _ in nsmallest(maxsize // 10 or 1,
- - iteritems(lastused),
- - key=itemgetter(1)):
- - del data[k]
- - del lastused[k]
- - data[args] = user_function(*args)
- - result = data[args]
- - finally:
- - lastused[args] = time()
- - return result
- + with lock:
- + try:
- + result = data[args]
- + stats[0] += 1 # Hit
- + except KeyError:
- + stats[1] += 1 # Miss
- + if len(data) == maxsize:
- + for k, _ in nsmallest(maxsize // 10 or 1,
- + iteritems(lastused),
- + key=itemgetter(1)):
- + del data[k]
- + del lastused[k]
- + data[args] = user_function(*args)
- + result = data[args]
- + finally:
- + lastused[args] = time()
- + return result
- def cache_info():
- - return stats[0], stats[1], maxsize, len(data)
- + with lock:
- + return stats[0], stats[1], maxsize, len(data)
- def cache_clear():
- - data.clear()
- - lastused.clear()
- - stats[0] = stats[1] = 0
- + with lock:
- + data.clear()
- + lastused.clear()
- + stats[0] = stats[1] = 0
- wrapper.cache_info = cache_info
- wrapper.cache_clear = cache_clear
- @@ -182,29 +186,33 @@
- def decorating_function(user_function):
- stats = [0, 0] # hits, misses
- data = {}
- + lock = Lock()
- @functools.wraps(user_function)
- def wrapper(*args):
- - try:
- - result = data[args]
- - stats[0] += 1 # Hit
- - except KeyError:
- - stats[1] += 1 # Miss
- - if len(data) == maxsize:
- - keys = data.keys()
- - for i in xrange(maxsize // 10 or 1):
- - n = random.randint(0, len(keys) - 1)
- - k = keys.pop(n)
- - del data[k]
- - data[args] = user_function(*args)
- - result = data[args]
- - return result
- + with lock:
- + try:
- + result = data[args]
- + stats[0] += 1 # Hit
- + except KeyError:
- + stats[1] += 1 # Miss
- + if len(data) == maxsize:
- + keys = data.keys()
- + for i in xrange(maxsize // 10 or 1):
- + n = random.randint(0, len(keys) - 1)
- + k = keys.pop(n)
- + del data[k]
- + data[args] = user_function(*args)
- + result = data[args]
- + return result
- def cache_info():
- - return stats[0], stats[1], maxsize, len(data)
- + with lock:
- + return stats[0], stats[1], maxsize, len(data)
- def cache_clear():
- - data.clear()
- + with lock:
- + data.clear()
- wrapper.cache_info = cache_info
- wrapper.cache_clear = cache_clear
- @@ -231,37 +239,41 @@
- def decorating_function(user_function):
- # Cache1, Cache2, Pointer, Hits, Misses
- stats = [{}, {}, 0, 0, 0]
- + lock = Lock()
- @functools.wraps(user_function)
- def wrapper(*args):
- - ptr = stats[2]
- - a = stats[ptr]
- - b = stats[not ptr]
- - key = args
- + with lock:
- + ptr = stats[2]
- + a = stats[ptr]
- + b = stats[not ptr]
- + key = args
- - if key in a:
- - stats[3] += 1 # Hit
- - return a[key]
- - elif key in b:
- - stats[3] += 1 # Hit
- - return b[key]
- - else:
- - stats[4] += 1 # Miss
- - result = user_function(*args)
- - a[key] = result
- - if len(a) >= maxsize:
- - stats[2] = not ptr
- - b.clear()
- - return result
- + if key in a:
- + stats[3] += 1 # Hit
- + return a[key]
- + elif key in b:
- + stats[3] += 1 # Hit
- + return b[key]
- + else:
- + stats[4] += 1 # Miss
- + result = user_function(*args)
- + a[key] = result
- + if len(a) >= maxsize:
- + stats[2] = not ptr
- + b.clear()
- + return result
- def cache_info():
- - return stats[3], stats[4], maxsize, len(stats[0]) + len(stats[1])
- + with lock:
- + return stats[3], stats[4], maxsize, len(stats[0]) + len(stats[1])
- def cache_clear():
- """Clear the cache and cache statistics"""
- - stats[0].clear()
- - stats[1].clear()
- - stats[3] = stats[4] = 0
- + with lock:
- + stats[0].clear()
- + stats[1].clear()
- + stats[3] = stats[4] = 0
- wrapper.cache_info = cache_info
- wrapper.cache_clear = cache_clear
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement