You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

569 lines
18 KiB

8 years ago
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1">
  6. <meta name="theme-color" content="#375EAB">
  7. <title>httpcache - The Go Programming Language</title>
  8. <link type="text/css" rel="stylesheet" href="../../../../lib/godoc/style.css">
  9. <link rel="stylesheet" href="../../../../lib/godoc/jquery.treeview.css">
  10. <script type="text/javascript">window.initFuncs = [];</script>
  11. </head>
  12. <body>
  13. <div id='lowframe' style="position: fixed; bottom: 0; left: 0; height: 0; width: 100%; border-top: thin solid grey; background-color: white; overflow: auto;">
  14. ...
  15. </div><!-- #lowframe -->
  16. <div id="topbar" class="wide"><div class="container">
  17. <div class="top-heading" id="heading-wide"><a href="http://localhost:6060/">The Go Programming Language</a></div>
  18. <div class="top-heading" id="heading-narrow"><a href="http://localhost:6060/">Go</a></div>
  19. <a href="index.html#" id="menu-button"><span id="menu-button-arrow">&#9661;</span></a>
  20. <form method="GET" action="http://localhost:6060/search">
  21. <div id="menu">
  22. <a href="http://localhost:6060/doc/">Documents</a>
  23. <a href="http://localhost:6060/pkg/">Packages</a>
  24. <a href="http://localhost:6060/project/">The Project</a>
  25. <a href="http://localhost:6060/help/">Help</a>
  26. <a href="http://localhost:6060/blog/">Blog</a>
  27. <input type="text" id="search" name="q" class="inactive" value="Search" placeholder="Search">
  28. </div>
  29. </form>
  30. </div></div>
  31. <div id="page" class="wide">
  32. <div class="container">
  33. <h1>Package httpcache</h1>
  34. <div id="nav"></div>
  35. <!--
  36. Copyright 2009 The Go Authors. All rights reserved.
  37. Use of this source code is governed by a BSD-style
  38. license that can be found in the LICENSE file.
  39. -->
  40. <!--
  41. Note: Static (i.e., not template-generated) href and id
  42. attributes start with "pkg-" to make it impossible for
  43. them to conflict with generated attributes (some of which
  44. correspond to Go identifiers).
  45. -->
  46. <script type='text/javascript'>
  47. document.ANALYSIS_DATA = null;
  48. document.CALLGRAPH = null;
  49. </script>
  50. <div id="short-nav">
  51. <dl>
  52. <dd><code>import "github.com/gregjones/httpcache"</code></dd>
  53. </dl>
  54. <dl>
  55. <dd><a href="index.html#pkg-overview" class="overviewLink">Overview</a></dd>
  56. <dd><a href="index.html#pkg-index" class="indexLink">Index</a></dd>
  57. <dd><a href="index.html#pkg-subdirectories">Subdirectories</a></dd>
  58. </dl>
  59. </div>
  60. <!-- The package's Name is printed as title by the top-level template -->
  61. <div id="pkg-overview" class="toggleVisible">
  62. <div class="collapsed">
  63. <h2 class="toggleButton" title="Click to show Overview section">Overview ▹</h2>
  64. </div>
  65. <div class="expanded">
  66. <h2 class="toggleButton" title="Click to hide Overview section">Overview ▾</h2>
  67. <p>
  68. Package httpcache provides a http.RoundTripper implementation that works as a
  69. mostly RFC-compliant cache for http responses.
  70. </p>
  71. <p>
  72. It is only suitable for use as a &#39;private&#39; cache (i.e. for a web-browser or an API-client
  73. and not for a shared proxy).
  74. </p>
  75. </div>
  76. </div>
  77. <div id="pkg-index" class="toggleVisible">
  78. <div class="collapsed">
  79. <h2 class="toggleButton" title="Click to show Index section">Index ▹</h2>
  80. </div>
  81. <div class="expanded">
  82. <h2 class="toggleButton" title="Click to hide Index section">Index ▾</h2>
  83. <!-- Table of contents for API; must be named manual-nav to turn off auto nav. -->
  84. <div id="manual-nav">
  85. <dl>
  86. <dd><a href="index.html#pkg-constants">Constants</a></dd>
  87. <dd><a href="index.html#pkg-variables">Variables</a></dd>
  88. <dd><a href="index.html#CachedResponse">func CachedResponse(c Cache, req *http.Request) (resp *http.Response, err error)</a></dd>
  89. <dd><a href="index.html#Date">func Date(respHeaders http.Header) (date time.Time, err error)</a></dd>
  90. <dd><a href="index.html#Cache">type Cache</a></dd>
  91. <dd><a href="index.html#MemoryCache">type MemoryCache</a></dd>
  92. <dd>&nbsp; &nbsp; <a href="index.html#NewMemoryCache">func NewMemoryCache() *MemoryCache</a></dd>
  93. <dd>&nbsp; &nbsp; <a href="index.html#MemoryCache.Delete">func (c *MemoryCache) Delete(key string)</a></dd>
  94. <dd>&nbsp; &nbsp; <a href="index.html#MemoryCache.Get">func (c *MemoryCache) Get(key string) (resp []byte, ok bool)</a></dd>
  95. <dd>&nbsp; &nbsp; <a href="index.html#MemoryCache.Set">func (c *MemoryCache) Set(key string, resp []byte)</a></dd>
  96. <dd><a href="index.html#Transport">type Transport</a></dd>
  97. <dd>&nbsp; &nbsp; <a href="index.html#NewMemoryCacheTransport">func NewMemoryCacheTransport() *Transport</a></dd>
  98. <dd>&nbsp; &nbsp; <a href="index.html#NewTransport">func NewTransport(c Cache) *Transport</a></dd>
  99. <dd>&nbsp; &nbsp; <a href="index.html#Transport.CancelRequest">func (t *Transport) CancelRequest(req *http.Request)</a></dd>
  100. <dd>&nbsp; &nbsp; <a href="index.html#Transport.Client">func (t *Transport) Client() *http.Client</a></dd>
  101. <dd>&nbsp; &nbsp; <a href="index.html#Transport.RoundTrip">func (t *Transport) RoundTrip(req *http.Request) (resp *http.Response, err error)</a></dd>
  102. </dl>
  103. </div><!-- #manual-nav -->
  104. <h4>Package files</h4>
  105. <p>
  106. <span style="font-size:90%">
  107. <a href="http://localhost:6060/src/github.com/gregjones/httpcache/httpcache.go">httpcache.go</a>
  108. </span>
  109. </p>
  110. </div><!-- .expanded -->
  111. </div><!-- #pkg-index -->
  112. <div id="pkg-callgraph" class="toggle" style="display: none">
  113. <div class="collapsed">
  114. <h2 class="toggleButton" title="Click to show Internal Call Graph section">Internal call graph ▹</h2>
  115. </div> <!-- .expanded -->
  116. <div class="expanded">
  117. <h2 class="toggleButton" title="Click to hide Internal Call Graph section">Internal call graph ▾</h2>
  118. <p>
  119. In the call graph viewer below, each node
  120. is a function belonging to this package
  121. and its children are the functions it
  122. calls&mdash;perhaps dynamically.
  123. </p>
  124. <p>
  125. The root nodes are the entry points of the
  126. package: functions that may be called from
  127. outside the package.
  128. There may be non-exported or anonymous
  129. functions among them if they are called
  130. dynamically from another package.
  131. </p>
  132. <p>
  133. Click a node to visit that function's source code.
  134. From there you can visit its callers by
  135. clicking its declaring <code>func</code>
  136. token.
  137. </p>
  138. <p>
  139. Functions may be omitted if they were
  140. determined to be unreachable in the
  141. particular programs or tests that were
  142. analyzed.
  143. </p>
  144. <!-- Zero means show all package entry points. -->
  145. <ul style="margin-left: 0.5in" id="callgraph-0" class="treeview"></ul>
  146. </div>
  147. </div> <!-- #pkg-callgraph -->
  148. <h2 id="pkg-constants">Constants</h2>
  149. <pre>const (
  150. <span class="comment">// XFromCache is the header added to responses that are returned from the cache</span>
  151. <span id="XFromCache">XFromCache</span> = &#34;X-From-Cache&#34;
  152. )</pre>
  153. <h2 id="pkg-variables">Variables</h2>
  154. <pre>var <span id="ErrNoDateHeader">ErrNoDateHeader</span> = <a href="../../../errors/index.html">errors</a>.<a href="../../../errors/index.html#New">New</a>(&#34;no Date header&#34;)</pre>
  155. <p>
  156. ErrNoDateHeader indicates that the HTTP headers contained no Date header.
  157. </p>
  158. <h2 id="CachedResponse">func <a href="http://localhost:6060/src/github.com/gregjones/httpcache/httpcache.go?s=1212:1292#L39">CachedResponse</a></h2>
  159. <pre>func CachedResponse(c <a href="index.html#Cache">Cache</a>, req *<a href="../../../net/http/index.html">http</a>.<a href="../../../net/http/index.html#Request">Request</a>) (resp *<a href="../../../net/http/index.html">http</a>.<a href="../../../net/http/index.html#Response">Response</a>, err <a href="../../../builtin/index.html#error">error</a>)</pre>
  160. <p>
  161. CachedResponse returns the cached http.Response for req if present, and nil
  162. otherwise.
  163. </p>
  164. <h2 id="Date">func <a href="http://localhost:6060/src/github.com/gregjones/httpcache/httpcache.go?s=9487:9549#L322">Date</a></h2>
  165. <pre>func Date(respHeaders <a href="../../../net/http/index.html">http</a>.<a href="../../../net/http/index.html#Header">Header</a>) (date <a href="../../../time/index.html">time</a>.<a href="../../../time/index.html#Time">Time</a>, err <a href="../../../builtin/index.html#error">error</a>)</pre>
  166. <p>
  167. Date parses and returns the value of the Date header.
  168. </p>
  169. <h2 id="Cache">type <a href="http://localhost:6060/src/github.com/gregjones/httpcache/httpcache.go?s=635:1004#L22">Cache</a></h2>
  170. <pre>type Cache interface {
  171. <span class="comment">// Get returns the []byte representation of a cached response and a bool</span>
  172. <span class="comment">// set to true if the value isn&#39;t empty</span>
  173. Get(key <a href="../../../builtin/index.html#string">string</a>) (responseBytes []<a href="../../../builtin/index.html#byte">byte</a>, ok <a href="../../../builtin/index.html#bool">bool</a>)
  174. <span class="comment">// Set stores the []byte representation of a response against a key</span>
  175. Set(key <a href="../../../builtin/index.html#string">string</a>, responseBytes []<a href="../../../builtin/index.html#byte">byte</a>)
  176. <span class="comment">// Delete removes the value associated with the key</span>
  177. Delete(key <a href="../../../builtin/index.html#string">string</a>)
  178. }</pre>
  179. <p>
  180. A Cache interface is used by the Transport to store and retrieve responses.
  181. </p>
  182. <h2 id="MemoryCache">type <a href="http://localhost:6060/src/github.com/gregjones/httpcache/httpcache.go?s=1530:1602#L50">MemoryCache</a></h2>
  183. <pre>type MemoryCache struct {
  184. <span class="comment">// contains filtered or unexported fields</span>
  185. }</pre>
  186. <p>
  187. MemoryCache is an implemtation of Cache that stores responses in an in-memory map.
  188. </p>
  189. <h3 id="NewMemoryCache">func <a href="http://localhost:6060/src/github.com/gregjones/httpcache/httpcache.go?s=2200:2234#L78">NewMemoryCache</a></h3>
  190. <pre>func NewMemoryCache() *<a href="index.html#MemoryCache">MemoryCache</a></pre>
  191. <p>
  192. NewMemoryCache returns a new Cache that will store items in an in-memory map
  193. </p>
  194. <h3 id="MemoryCache.Delete">func (*MemoryCache) <a href="http://localhost:6060/src/github.com/gregjones/httpcache/httpcache.go?s=2024:2064#L71">Delete</a></h3>
  195. <pre>func (c *<a href="index.html#MemoryCache">MemoryCache</a>) Delete(key <a href="../../../builtin/index.html#string">string</a>)</pre>
  196. <p>
  197. Delete removes key from the cache
  198. </p>
  199. <h3 id="MemoryCache.Get">func (*MemoryCache) <a href="http://localhost:6060/src/github.com/gregjones/httpcache/httpcache.go?s=1695:1755#L56">Get</a></h3>
  200. <pre>func (c *<a href="index.html#MemoryCache">MemoryCache</a>) Get(key <a href="../../../builtin/index.html#string">string</a>) (resp []<a href="../../../builtin/index.html#byte">byte</a>, ok <a href="../../../builtin/index.html#bool">bool</a>)</pre>
  201. <p>
  202. Get returns the []byte representation of the response and true if present, false if not
  203. </p>
  204. <h3 id="MemoryCache.Set">func (*MemoryCache) <a href="http://localhost:6060/src/github.com/gregjones/httpcache/httpcache.go?s=1882:1932#L64">Set</a></h3>
  205. <pre>func (c *<a href="index.html#MemoryCache">MemoryCache</a>) Set(key <a href="../../../builtin/index.html#string">string</a>, resp []<a href="../../../builtin/index.html#byte">byte</a>)</pre>
  206. <p>
  207. Set saves response resp to the cache with key
  208. </p>
  209. <h2 id="Transport">type <a href="http://localhost:6060/src/github.com/gregjones/httpcache/httpcache.go?s=2988:3397#L113">Transport</a></h2>
  210. <pre>type Transport struct {
  211. <span class="comment">// The RoundTripper interface actually used to make requests</span>
  212. <span class="comment">// If nil, http.DefaultTransport is used</span>
  213. Transport <a href="../../../net/http/index.html">http</a>.<a href="../../../net/http/index.html#RoundTripper">RoundTripper</a>
  214. Cache <a href="index.html#Cache">Cache</a>
  215. <span class="comment">// If true, responses returned from the cache will be given an extra header, X-From-Cache</span>
  216. MarkCachedResponses <a href="../../../builtin/index.html#bool">bool</a>
  217. <span class="comment">// contains filtered or unexported fields</span>
  218. }</pre>
  219. <p>
  220. Transport is an implementation of http.RoundTripper that will return values from a cache
  221. where possible (avoiding a network request) and will additionally add validators (etag/if-modified-since)
  222. to repeated requests allowing servers to return 304 / Not Modified
  223. </p>
  224. <h3 id="NewMemoryCacheTransport">func <a href="http://localhost:6060/src/github.com/gregjones/httpcache/httpcache.go?s=17096:17137#L580">NewMemoryCacheTransport</a></h3>
  225. <pre>func NewMemoryCacheTransport() *<a href="index.html#Transport">Transport</a></pre>
  226. <p>
  227. NewMemoryCacheTransport returns a new Transport using the in-memory cache implementation
  228. </p>
  229. <h3 id="NewTransport">func <a href="http://localhost:6060/src/github.com/gregjones/httpcache/httpcache.go?s=3517:3554#L128">NewTransport</a></h3>
  230. <pre>func NewTransport(c <a href="index.html#Cache">Cache</a>) *<a href="index.html#Transport">Transport</a></pre>
  231. <p>
  232. NewTransport returns a new Transport with the
  233. provided Cache implementation and MarkCachedResponses set to true
  234. </p>
  235. <h3 id="Transport.CancelRequest">func (*Transport) <a href="http://localhost:6060/src/github.com/gregjones/httpcache/httpcache.go?s=8800:8852#L295">CancelRequest</a></h3>
  236. <pre>func (t *<a href="index.html#Transport">Transport</a>) CancelRequest(req *<a href="../../../net/http/index.html">http</a>.<a href="../../../net/http/index.html#Request">Request</a>)</pre>
  237. <p>
  238. CancelRequest calls CancelRequest on the underlaying transport if implemented or
  239. throw a warning otherwise.
  240. </p>
  241. <h3 id="Transport.Client">func (*Transport) <a href="http://localhost:6060/src/github.com/gregjones/httpcache/httpcache.go?s=3673:3714#L133">Client</a></h3>
  242. <pre>func (t *<a href="index.html#Transport">Transport</a>) Client() *<a href="../../../net/http/index.html">http</a>.<a href="../../../net/http/index.html#Client">Client</a></pre>
  243. <p>
  244. Client returns an *http.Client that caches responses.
  245. </p>
  246. <h3 id="Transport.RoundTrip">func (*Transport) <a href="http://localhost:6060/src/github.com/gregjones/httpcache/httpcache.go?s=4923:5004#L171">RoundTrip</a></h3>
  247. <pre>func (t *<a href="index.html#Transport">Transport</a>) RoundTrip(req *<a href="../../../net/http/index.html">http</a>.<a href="../../../net/http/index.html#Request">Request</a>) (resp *<a href="../../../net/http/index.html">http</a>.<a href="../../../net/http/index.html#Response">Response</a>, err <a href="../../../builtin/index.html#error">error</a>)</pre>
  248. <p>
  249. RoundTrip takes a Request and returns a Response
  250. </p>
  251. <p>
  252. If there is a fresh Response already in cache, then it will be returned without connecting to
  253. the server.
  254. </p>
  255. <p>
  256. If there is a stale Response, then any validators it contains will be set on the new request
  257. to give the server a chance to respond with NotModified. If this happens, then the cached Response
  258. will be returned.
  259. </p>
  260. <h2 id="pkg-subdirectories">Subdirectories</h2>
  261. <div class="pkg-dir">
  262. <table>
  263. <tr>
  264. <th class="pkg-name">Name</th>
  265. <th class="pkg-synopsis">Synopsis</th>
  266. </tr>
  267. <tr>
  268. <td colspan="2"><a href="../index.html">..</a></td>
  269. </tr>
  270. <tr>
  271. <td class="pkg-name" style="padding-left: 0px;">
  272. <a href="diskcache/index.html">diskcache</a>
  273. </td>
  274. <td class="pkg-synopsis">
  275. Package diskcache provides an implementation of httpcache.Cache that uses the diskv package to supplement an in-memory map with persistent storage
  276. </td>
  277. </tr>
  278. <tr>
  279. <td class="pkg-name" style="padding-left: 0px;">
  280. <a href="leveldbcache/index.html">leveldbcache</a>
  281. </td>
  282. <td class="pkg-synopsis">
  283. Package leveldbcache provides an implementation of httpcache.Cache that uses github.com/syndtr/goleveldb/leveldb
  284. </td>
  285. </tr>
  286. <tr>
  287. <td class="pkg-name" style="padding-left: 0px;">
  288. <a href="memcache/index.html">memcache</a>
  289. </td>
  290. <td class="pkg-synopsis">
  291. Package memcache provides an implementation of httpcache.Cache that uses App Engine&#39;s memcache package to store cached responses.
  292. </td>
  293. </tr>
  294. <tr>
  295. <td class="pkg-name" style="padding-left: 0px;">
  296. <a href="redis/index.html">redis</a>
  297. </td>
  298. <td class="pkg-synopsis">
  299. Package redis provides a redis interface for http caching.
  300. </td>
  301. </tr>
  302. </table>
  303. </div>
  304. <div id="footer">
  305. Build version go1.6.<br>
  306. Except as <a href="https://developers.google.com/site-policies#restrictions">noted</a>,
  307. the content of this page is licensed under the
  308. Creative Commons Attribution 3.0 License,
  309. and code is licensed under a <a href="http://localhost:6060/LICENSE">BSD license</a>.<br>
  310. <a href="http://localhost:6060/doc/tos.html">Terms of Service</a> |
  311. <a href="http://www.google.com/intl/en/policies/privacy/">Privacy Policy</a>
  312. </div>
  313. </div><!-- .container -->
  314. </div><!-- #page -->
  315. <!-- TODO(adonovan): load these from <head> using "defer" attribute? -->
  316. <script type="text/javascript" src="../../../../lib/godoc/jquery.js"></script>
  317. <script type="text/javascript" src="../../../../lib/godoc/jquery.treeview.js"></script>
  318. <script type="text/javascript" src="../../../../lib/godoc/jquery.treeview.edit.js"></script>
  319. <script type="text/javascript" src="../../../../lib/godoc/godocs.js"></script>
  320. </body>
  321. </html>