diff options
-rw-r--r-- | mitmproxy/tools/console/common.py | 7 | ||||
-rw-r--r-- | mitmproxy/tools/console/flowview.py | 22 | ||||
-rw-r--r-- | mitmproxy/tools/main.py | 23 | ||||
-rw-r--r-- | test/mitmproxy/test_utils_lrucache.py | 34 |
4 files changed, 27 insertions, 59 deletions
diff --git a/mitmproxy/tools/console/common.py b/mitmproxy/tools/console/common.py index 90dce5ae..78860702 100644 --- a/mitmproxy/tools/console/common.py +++ b/mitmproxy/tools/console/common.py @@ -7,7 +7,7 @@ import urwid import urwid.util import mitmproxy.net -from mitmproxy.utils import lrucache +from functools import lru_cache from mitmproxy.tools.console import signals from mitmproxy import export from mitmproxy.utils import human @@ -325,9 +325,8 @@ def export_to_clip_or_file(key, scope, flow, writer): else: # other keys writer(exporter(flow)) -flowcache = lrucache.LRUCache(800) - +@lru_cache(maxsize=800) def raw_format_flow(f): f = dict(f) pile = [] @@ -458,4 +457,4 @@ def format_flow(f, focus, extended=False, hostheader=False, max_url_len=False): else: d["resp_ctype"] = "" - return flowcache.get(raw_format_flow, tuple(sorted(d.items()))) + return raw_format_flow(tuple(sorted(d.items()))) diff --git a/mitmproxy/tools/console/flowview.py b/mitmproxy/tools/console/flowview.py index 5ac61bb7..18947327 100644 --- a/mitmproxy/tools/console/flowview.py +++ b/mitmproxy/tools/console/flowview.py @@ -8,7 +8,6 @@ from typing import Optional, Union # noqa from mitmproxy import contentviews from mitmproxy import http -from mitmproxy.utils import lrucache from mitmproxy.tools.console import common from mitmproxy.tools.console import flowdetailview from mitmproxy.tools.console import grideditor @@ -18,6 +17,7 @@ from mitmproxy.tools.console import tabs from mitmproxy import export from mitmproxy.net.http import Headers from mitmproxy.net.http import status_codes +from functools import lru_cache class SearchError(Exception): @@ -120,9 +120,6 @@ class FlowViewHeader(urwid.WidgetWrap): hostheader=self.master.options.showhost ) - -cache = lrucache.LRUCache(200) - TAB_REQ = 0 TAB_RESP = 1 @@ -193,15 +190,14 @@ class FlowView(tabs.Tabs): message.headers.fields, getattr(message, "path", None), )) - return cache.get( - # We move message into this partial function as it is not hashable. - lambda *args: self._get_content_view(message, *args), - viewmode, - limit, - flow_modify_cache_invalidation - ) - - def _get_content_view(self, message, viewmode, max_lines, _): + # we need to pass the message off-band because it's not hashable + self._get_content_view_message = message + return self._get_content_view(viewmode, limit, flow_modify_cache_invalidation) + + @lru_cache(maxsize=200) + def _get_content_view(self, viewmode, max_lines, _): + message = self._get_content_view_message + self._get_content_view_message = None description, lines, error = contentviews.get_message_content_view( viewmode, message ) diff --git a/mitmproxy/tools/main.py b/mitmproxy/tools/main.py index 9fce9234..f88bee6c 100644 --- a/mitmproxy/tools/main.py +++ b/mitmproxy/tools/main.py @@ -1,13 +1,20 @@ -import os -import signal +from __future__ import print_function # this is here for the version check to work on Python 2. import sys -from mitmproxy.tools import cmdline -from mitmproxy import exceptions -from mitmproxy.proxy import config -from mitmproxy.proxy import server -from mitmproxy.utils import version_check -from mitmproxy.utils import debug +if sys.version_info < (3, 5): + print("#" * 49, file=sys.stderr) + print("# mitmproxy only supports Python 3.5 and above! #", file=sys.stderr) + print("#" * 49, file=sys.stderr) + +import os # noqa +import signal # noqa + +from mitmproxy.tools import cmdline # noqa +from mitmproxy import exceptions # noqa +from mitmproxy.proxy import config # noqa +from mitmproxy.proxy import server # noqa +from mitmproxy.utils import version_check # noqa +from mitmproxy.utils import debug # noqa def assert_utf8_env(): diff --git a/test/mitmproxy/test_utils_lrucache.py b/test/mitmproxy/test_utils_lrucache.py deleted file mode 100644 index 07b96b4d..00000000 --- a/test/mitmproxy/test_utils_lrucache.py +++ /dev/null @@ -1,34 +0,0 @@ -from mitmproxy.utils import lrucache - - -def test_LRUCache(): - cache = lrucache.LRUCache(2) - - class Foo: - ran = False - - def gen(self, x): - self.ran = True - return x - f = Foo() - - assert not f.ran - assert cache.get(f.gen, 1) == 1 - assert f.ran - f.ran = False - assert cache.get(f.gen, 1) == 1 - assert not f.ran - - f.ran = False - assert cache.get(f.gen, 1) == 1 - assert not f.ran - assert cache.get(f.gen, 2) == 2 - assert cache.get(f.gen, 3) == 3 - assert f.ran - - f.ran = False - assert cache.get(f.gen, 1) == 1 - assert f.ran - - assert len(cache.cacheList) == 2 - assert len(cache.cache) == 2 |