Caching Support!


PULL 700


Going to use cachecore.


This pull request [fails](http://travis-ci.org/kennethreitz/requests/builds/1734725) (merged dc0cd95d into d983cfab).


Get an ID for a request: `sha1('{method}-{url}')'?


key: `SHA256(type-method-url)`


Plan is for `cache=True` (on session) to be shorthand for:

```
cache=Cache(backend=SimpleCache(), conditional=True, content=False)
```


Going to implement conditional gets first, content will come at a later date.

/c @dstufft


Going to implement conditional gets first, content will come at a later date.

/c @dstufft


example:

``` python
cache=Cache(backend=SimpleCache(), conditional=True, content=False)
```

`backend=` sets the default for conditional and content if `True`

you could also do

``` python
cache=Cache(conditional=BlahCache(), content=BlahCache())
```


:sparkles: :beer: :sparkles:


https://bitbucket.org/elarson/httpcache


httpcache looks perfect.


This pull request [fails](http://travis-ci.org/kennethreitz/requests/builds/1756672) (merged 7f19fc59 into d983cfab).


This pull request [fails](http://travis-ci.org/kennethreitz/requests/builds/1792303) (merged 706f88fe into d983cfab).


Are you going to implement a HTTP cache here? 


That's the plan :)


Cool.

When you get to it, FYI this is about to go into IETF Last Call as the specification of HTTP/1.1 caching - 
  https://svn.tools.ietf.org/svn/wg/httpbis/draft-ietf-httpbis/latest/p6-cache.html

It should be significantly easier to understand / implement than RFC2616 section 13. Feedback appreciated, of course.


https://github.com/django/django/blob/master/django/middleware/cache.py#L46


@kennethreitz : Are you abandoning plans to add caching support, or is this work continuing elsewhere?


@inactivist I didn't intentionally close the ticket! I am doing some cleanup and switched the default branch from 'develop' to 'master'. That apparently closed all pending pull requests :(


I'll reopen soon.


@kennethreitz : Thanks.  Actually, I was interested in helping with the effort, as I'd like to take advantage of caching.  What can I do to assist?


+1 to this; I could use it right now.

this looks like it was a nice solution using event hooks (and Kenneth agreed), but support for the `pre_request` event hook has since been removed. 


To be perfectly honest, this is why I still use httplib2 in new projects. I'm already used to its API and I don't have to reinvent caching.


@ssokolow your comment seems entirely baffling to me given that there are several options to use when you want to use caching in requests. The last comment on this issue is over a year old and the issue is closed. If you're looking for those resources, I suggest you look elsewhere.


Take a look at Cache-Control: https://pypi.python.org/pypi/CacheControl


@sigmavirus24

Ahh. Sorry about that.

I went looking but only managed to find a cache implementation which was purely timer based (no ETags, no `If-Modified-Since`) so I assumed they didn't exist and this was the most promising avenue.

It's quite likely I just screwed up my search keywords somehow but it'd still be a good idea to to add some kind of "Related Projects" list to the wiki.

@Lukasa Thanks! That couldn't be more perfect.


Yes, thanks, @Lukasa, I searched recently and missed Cache-Control as well.  If it's not there now, why not mention it in the Requests docs?  (Hmm.... pull request, anyone?)


The main reason we don't mention it is because, by and large, we don't mention third-party plugins in our documentation. We explicitly mention a few of our authentication providers, but that's because they're managed by the Requests project but live outside of the core repository.

I would also be against explicitly referring to third-party projects in our documentation, because it provides the suggestion that we believe that project to be superior to any of its competitors. It also puts us on the hook for ensuring that the project remains compatible and maintained, neither of which are things we want to do for third-party projects.

Interestingly, if I google "python requests caching" I find [my own (deprecated) project](http://httpcache.readthedocs.org/en/latest/) towards the top, and that page explicitly mentions CacheControl.


What about on the wiki then?

It does match the wiki's "either hasn't made into the documentation yet, or doesn't belong there" criterion and given the following points, it's pretty intuitive that wiki contents are community-supplied and neither endorsed nor supported by the project:
- The project doesn't use the wiki for its website or documentation.
- The project's documentation is much more polished than the wiki
- It's pretty well understood that, when a project has both Sphinx documentation **and** a wiki, the wiki is there to take advantage of the "anyone can edit" aspect of wiki-ness.
- The Wiki's "Home" page specifically encourages people to add things.

(I'd add it myself, but I don't know all of the "several options" that @sigmavirus24 mentioned existing. I'd rather have all known caching extensions listed, both to let people make their own decisions and to further drive home the "this is not advice" point.)