Use TTLCache to cache proxy_bypass.

As mentioned in https://github.com/kennethreitz/requests/issues/2988, we might leverage the LRU cache to speed up the `proxy_bypass`, which might cause unexpected delay.


The bigger problem here is that `functools.lru_cache` is not available in Python 2. It should be fairly trivial to lift the code into a backport module and vendor it though.


For general code review I'm not best placed to do it because I suggested the approach to begin with, so let's tag in @sigmavirus24 and @kennethreitz as well.


OK. I'll add the doc latter. As for the backport for LRU cache, do you think it should reside in `compat.py` or in a separate module?


I think it should go under `packages`.


> The bigger problem here is that functools.lru_cache is not available in Python 2. It should be fairly trivial to lift the code into a backport module and vendor it though.

IMO you should invalidate single items in the cache based on the timestamp added, so you would need a custom cache function anyway...


@Lukasa @schlamar I agree I'd need to make a custom cache function. Do you think it is appropriate to make it a standalone package and vendor it under `packages`?


I do. =)


I just found there is a 3rd party package that can do the cache for us: https://github.com/tkem/cachetools
Specifically, we can use TTL cache, instead of making a timestamp invalid_indicator ourself.
How do you think?


Requests doesn't like bringing in third-party modules as dependencies if we can avoid it.


I thought if I make a standalone package it would also be considered third party, right? Then would it make any difference for using another caching tool?
Thanks


> I thought if I make a standalone package it would also be considered third party, right? Then would it make any difference for using another caching tool?

Sorry, I'm not sure that I understand what you're proposing here.


I think a simple TTL cache can be implemented in 50-80 LOC or even less so there is no need to vendor a new standalone package nor another third-party module. I would just put it into `requests.utils`.


sure, no problem. will do it in utils.
Marc Schlaich notifications@github.com於 2016年3月21日 週一，下午7:24寫道：

> I think a simple TTL cache can be implemented in 50-80 LOC or even less so
> there is no need to vendor a new standalone package nor another third-party
> module. I would just put it into requests.utils.
> 
> —
> You are receiving this because you authored the thread.
> Reply to this email directly or view it on GitHub
> https://github.com/kennethreitz/requests/pull/2992#issuecomment-199232165
> 
> ## 
> 
> Chih-Wei (Bert)


This pull request has been open for over two months now. Let's do what we can to get it merged or closed :)


To remember which functions have been cached, I'd need to store it somewhere.
Which approach, using a scope (under utils.py) variable or using a Class's instance variable, would you prefer?


@kennethreitz @Lukasa Sorry for taking so long. I implemented a naive TTLCache for caching the `proxy_bypass` function call. Please have a look at it and let me know your thoughts.
If there is no big problem with the implementation, I'll add some documentation and testing if necessary later.

Thanks.


I reviewed it. A second opinion would be nice.


Closing due to inactivity (not sure if that's our fault or not, sorry!). Please re-submit if you're still interested in contributing this code :)