docstorecache

package module
v0.0.0-...-eb5e99f Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Feb 8, 2020 License: Apache-2.0 Imports: 3 Imported by: 0

README

docstorecache

GoDoc Go Report Card GitHub

A docstore based cache for use with Greg Jones' httpcache. It supports any document store implemented by the Go Cloud project including Google Cloud Firestore, Amazon DynamoDB, Azure Cosmos DB, and others.

Documentation

Overview

Package docstorecache provides an httpclient Cache based on docstore.

Google Cloud Firestore:

const docURL = "firestore://projects/my-project/databases/(default)/documents/httpcache?name_field=Key"
coll, err := docstore.OpenCollection(context.Background(), docURL)
if err != nil {
	log.Fatalf("Error opening collection (%s): %v", docURL, err)
}
defer coll.Close()

cache := New(coll)

cachingTransport := httpcache.NewTransport(cache)

client := cachingTransport.Client()
resp, err := client.Get("http://www.google.com/robots.txt")
if err != nil {
	// handle error
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
fmt.Printf("%s", string(body))

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DocstoreCache

type DocstoreCache struct {
	Collection *docstore.Collection
}

DocstoreCache is an implementation of a Cache that stores responses in a Go Cloud Docstore (Google Cloud Firestore, Amazon DynamoDB, Azure Cosmos DB , etc).

func New

func New(collection *docstore.Collection) *DocstoreCache

New returns a new Cache that uses a docstore collection. When creating the collection you must indicate that "Key" is the primary field (partition key, name, id).

func (*DocstoreCache) Delete

func (c *DocstoreCache) Delete(k string)

Delete attempts to remove an item from docstore.

func (*DocstoreCache) Get

func (c *DocstoreCache) Get(k string) (resp []byte, ok bool)

Get attempts to retrive a cached item from docstore.

func (*DocstoreCache) Set

func (c *DocstoreCache) Set(k string, resp []byte)

Set attempts to save an item in docstore. Errors are silently ignored.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL