Advertisement
Guest User

Untitled

a guest
Jun 2nd, 2015
281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Diff 6.17 KB | None | 0 0
  1. diff --git a/src/whoosh/util/cache.py b/src/whoosh/util/cache.py
  2. --- a/src/whoosh/util/cache.py
  3. +++ b/src/whoosh/util/cache.py
  4. @@ -80,33 +80,37 @@
  5.          stats = [0, 0]  # Hits, misses
  6.          data = {}
  7.          lastused = {}
  8. +        lock = Lock()
  9.  
  10.          @functools.wraps(user_function)
  11.          def wrapper(*args):
  12. -            try:
  13. -                result = data[args]
  14. -                stats[0] += 1  # Hit
  15. -            except KeyError:
  16. -                stats[1] += 1  # Miss
  17. -                if len(data) == maxsize:
  18. -                    for k, _ in nsmallest(maxsize // 10 or 1,
  19. -                                          iteritems(lastused),
  20. -                                          key=itemgetter(1)):
  21. -                        del data[k]
  22. -                        del lastused[k]
  23. -                data[args] = user_function(*args)
  24. -                result = data[args]
  25. -            finally:
  26. -                lastused[args] = time()
  27. -            return result
  28. +            with lock:
  29. +                try:
  30. +                    result = data[args]
  31. +                    stats[0] += 1  # Hit
  32. +                except KeyError:
  33. +                    stats[1] += 1  # Miss
  34. +                    if len(data) == maxsize:
  35. +                        for k, _ in nsmallest(maxsize // 10 or 1,
  36. +                                              iteritems(lastused),
  37. +                                              key=itemgetter(1)):
  38. +                            del data[k]
  39. +                            del lastused[k]
  40. +                    data[args] = user_function(*args)
  41. +                    result = data[args]
  42. +                finally:
  43. +                    lastused[args] = time()
  44. +                return result
  45.  
  46.          def cache_info():
  47. -            return stats[0], stats[1], maxsize, len(data)
  48. +            with lock:
  49. +                return stats[0], stats[1], maxsize, len(data)
  50.  
  51.          def cache_clear():
  52. -            data.clear()
  53. -            lastused.clear()
  54. -            stats[0] = stats[1] = 0
  55. +            with lock:
  56. +                data.clear()
  57. +                lastused.clear()
  58. +                stats[0] = stats[1] = 0
  59.  
  60.          wrapper.cache_info = cache_info
  61.          wrapper.cache_clear = cache_clear
  62. @@ -182,29 +186,33 @@
  63.      def decorating_function(user_function):
  64.          stats = [0, 0]  # hits, misses
  65.          data = {}
  66. +        lock = Lock()
  67.  
  68.          @functools.wraps(user_function)
  69.          def wrapper(*args):
  70. -            try:
  71. -                result = data[args]
  72. -                stats[0] += 1  # Hit
  73. -            except KeyError:
  74. -                stats[1] += 1  # Miss
  75. -                if len(data) == maxsize:
  76. -                    keys = data.keys()
  77. -                    for i in xrange(maxsize // 10 or 1):
  78. -                        n = random.randint(0, len(keys) - 1)
  79. -                        k = keys.pop(n)
  80. -                        del data[k]
  81. -                data[args] = user_function(*args)
  82. -                result = data[args]
  83. -            return result
  84. +            with lock:
  85. +                try:
  86. +                    result = data[args]
  87. +                    stats[0] += 1  # Hit
  88. +                except KeyError:
  89. +                    stats[1] += 1  # Miss
  90. +                    if len(data) == maxsize:
  91. +                        keys = data.keys()
  92. +                        for i in xrange(maxsize // 10 or 1):
  93. +                            n = random.randint(0, len(keys) - 1)
  94. +                            k = keys.pop(n)
  95. +                            del data[k]
  96. +                    data[args] = user_function(*args)
  97. +                    result = data[args]
  98. +                return result
  99.  
  100.          def cache_info():
  101. -            return stats[0], stats[1], maxsize, len(data)
  102. +            with lock:
  103. +                return stats[0], stats[1], maxsize, len(data)
  104.  
  105.          def cache_clear():
  106. -            data.clear()
  107. +            with lock:
  108. +                data.clear()
  109.  
  110.          wrapper.cache_info = cache_info
  111.          wrapper.cache_clear = cache_clear
  112. @@ -231,37 +239,41 @@
  113.      def decorating_function(user_function):
  114.          # Cache1, Cache2, Pointer, Hits, Misses
  115.          stats = [{}, {}, 0, 0, 0]
  116. +        lock = Lock()
  117.  
  118.          @functools.wraps(user_function)
  119.          def wrapper(*args):
  120. -            ptr = stats[2]
  121. -            a = stats[ptr]
  122. -            b = stats[not ptr]
  123. -            key = args
  124. +            with lock:
  125. +                ptr = stats[2]
  126. +                a = stats[ptr]
  127. +                b = stats[not ptr]
  128. +                key = args
  129.  
  130. -            if key in a:
  131. -                stats[3] += 1  # Hit
  132. -                return a[key]
  133. -            elif key in b:
  134. -                stats[3] += 1  # Hit
  135. -                return b[key]
  136. -            else:
  137. -                stats[4] += 1  # Miss
  138. -                result = user_function(*args)
  139. -                a[key] = result
  140. -                if len(a) >= maxsize:
  141. -                    stats[2] = not ptr
  142. -                    b.clear()
  143. -                return result
  144. +                if key in a:
  145. +                    stats[3] += 1  # Hit
  146. +                    return a[key]
  147. +                elif key in b:
  148. +                    stats[3] += 1  # Hit
  149. +                    return b[key]
  150. +                else:
  151. +                    stats[4] += 1  # Miss
  152. +                    result = user_function(*args)
  153. +                    a[key] = result
  154. +                    if len(a) >= maxsize:
  155. +                        stats[2] = not ptr
  156. +                        b.clear()
  157. +                    return result
  158.  
  159.          def cache_info():
  160. -            return stats[3], stats[4], maxsize, len(stats[0]) + len(stats[1])
  161. +            with lock:
  162. +                return stats[3], stats[4], maxsize, len(stats[0]) + len(stats[1])
  163.  
  164.          def cache_clear():
  165.              """Clear the cache and cache statistics"""
  166. -            stats[0].clear()
  167. -            stats[1].clear()
  168. -            stats[3] = stats[4] = 0
  169. +            with lock:
  170. +                stats[0].clear()
  171. +                stats[1].clear()
  172. +                stats[3] = stats[4] = 0
  173.  
  174.          wrapper.cache_info = cache_info
  175.          wrapper.cache_clear = cache_clear
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement