Documentation ¶
Overview ¶
Copyright (C) 2022 NHR@FAU, University Erlangen-Nuremberg. All rights reserved. Use of this source code is governed by a MIT-style license that can be found in the LICENSE file.
Copyright (C) 2022 NHR@FAU, University Erlangen-Nuremberg. All rights reserved. Use of this source code is governed by a MIT-style license that can be found in the LICENSE file.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Cache ¶
type Cache struct {
// contains filtered or unexported fields
}
func New ¶
Return a new instance of a LRU In-Memory Cache. Read [the README](./README.md) for more information on what is going on with `maxmemory`.
func (*Cache) Del ¶
Remove the value at key `key` from the cache. Return true if the key was in the cache and false otherwise. It is possible that true is returned even though the value already expired. It is possible that false is returned even though the value will show up in the cache if this function is called on a key while that key is beeing computed.
func (*Cache) Get ¶
func (c *Cache) Get(key string, computeValue ComputeValue) interface{}
Return the cached value for key `key` or call `computeValue` and store its return value in the cache. If called, the closure will be called synchronous and __shall not call methods on the same cache__ or a deadlock might ocure. If `computeValue` is nil, the cache is checked and if no entry was found, nil is returned. If another goroutine is currently computing that value, the result is waited for.
type ComputeValue ¶
Type of the closure that must be passed to `Get` to compute the value in case it is not cached.
returned values are the computed value to be stored in the cache, the duration until this value will expire and a size estimate.
type HttpHandler ¶
type HttpHandler struct { // Allows overriding the way the cache key is extracted // from the http request. The defailt is to use the RequestURI. CacheKey func(*http.Request) string // contains filtered or unexported fields }
HttpHandler is can be used as HTTP Middleware in order to cache requests, for example static assets. By default, the request's raw URI is used as key and nothing else. Results with a status code other than 200 are cached with a TTL of zero seconds, so basically re-fetched as soon as the current fetch is done and a new request for that URI is done.
func NewHttpHandler ¶
Returns a new caching HttpHandler. If no entry in the cache is found or it was too old, `fetcher` is called with a modified http.ResponseWriter and the response is stored in the cache. If `fetcher` sets the "Expires" header, the ttl is set appropriately (otherwise, the default ttl passed as argument here is used). `maxmemory` should be in the unit bytes.
func (*HttpHandler) ServeHTTP ¶
func (h *HttpHandler) ServeHTTP(rw http.ResponseWriter, r *http.Request)
Tries to serve a response to r from cache or calls next and stores the response to the cache for the next time.