mirror of
https://github.com/Grasscutters/mitmproxy.git
synced 2024-12-04 04:37:15 +00:00
1ffc273c94
- Move more stuff that belongs in netlib.human - Move some stuff to near the only use - Zap mitmproxy.utils.timestamp(). I see the rationale, but we used it interchangeably with time.time() throughout the project. Since time.time() dominates in the codebase and timestamp() is such low utility, away it goes.
39 lines
1.0 KiB
Python
39 lines
1.0 KiB
Python
from __future__ import absolute_import, print_function, division
|
|
|
|
import netlib.utils
|
|
|
|
|
|
pkg_data = netlib.utils.Data(__name__)
|
|
|
|
|
|
class LRUCache:
|
|
|
|
"""
|
|
A simple LRU cache for generated values.
|
|
"""
|
|
|
|
def __init__(self, size=100):
|
|
self.size = size
|
|
self.cache = {}
|
|
self.cacheList = []
|
|
|
|
def get(self, gen, *args):
|
|
"""
|
|
gen: A (presumably expensive) generator function. The identity of
|
|
gen is NOT taken into account by the cache.
|
|
*args: A list of immutable arguments, used to establish identiy by
|
|
*the cache, and passed to gen to generate values.
|
|
"""
|
|
if args in self.cache:
|
|
self.cacheList.remove(args)
|
|
self.cacheList.insert(0, args)
|
|
return self.cache[args]
|
|
else:
|
|
ret = gen(*args)
|
|
self.cacheList.insert(0, args)
|
|
self.cache[args] = ret
|
|
if len(self.cacheList) > self.size:
|
|
d = self.cacheList.pop()
|
|
self.cache.pop(d)
|
|
return ret
|