cachereader

package module
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: May 12, 2024 License: BSD-3-Clause Imports: 8 Imported by: 9

README

go-reader-cachereader

Go package implementing the whosonfirst/go-reader interface for use with a caching layer.

Documentation

Go Reference

Example

package main

import (
	"context"
	"fmt"
	"github.com/whosonfirst/go-reader"
	"github.com/whosonfirst/go-reader-cachereader"	
	"io/ioutil"
	"log"
)

func main(){

	ctx := context.Background()

	reader_uri := "fs:///usr/local/data"
	cache_uri := "gocache://"

	cr_uri := fmt.Sprintf("cachereader://?reader=%s&cache=%s", reader_uri, cache_uri)
	
	r, _ := reader.NewReader(ctx, cr_uri)

	path := "101736545.geojson"
	
	for i := 0; i < 3; i++ {

		fh, _ := r.Read(ctx, path)
		defer fh.Close()

		io.Copy(ioutil.Discard, fh)

		status, _ := cachereader.GetLastRead(r, path)

		switch i {
		case 0:
			if status != cachereader.CacheMiss {
				log.Printf("Expected cache miss on first read of %s", path)
			}
		default:
			if status != cachereader.CacheHit {
				log.Printf("Expected cache hit after first read of %s", path)
			}
		}
	}
}

Error handling omitted for the sake of brevity.

See also

Documentation

Overview

Package cachereader implements the `whosonfirst/go-reader` interface with a caching layer.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewCacheReader

func NewCacheReader(ctx context.Context, uri string) (reader.Reader, error)

NewCacheReader will return a new `CacheReader` instance configured by 'uri' which is expected to take the form of:

cachereader://?reader={READER_URI}&cache={CACHE_URI}

Where {READER_URI} is expected to be a valid `whosonfirst/go-reader.Reader` URI and {CACHE_URI} is expected to be a valid `whosonfirst/go-cache.Cache` URI. Note that multiple "?reader=" parameter are supported; internally the `CacheReader` implementation uses a `whosonfirst/go-reader.MultiReader` instance for reading documents.

func NewCacheReaderWithOptions added in v1.1.0

func NewCacheReaderWithOptions(ctx context.Context, opts *CacheReaderOptions) (reader.Reader, error)

NewCacheReader will return a new `CacheReader` instance configured by 'opts'.

Types

type CacheReader

type CacheReader struct {
	reader.Reader
	// contains filtered or unexported fields
}

type CacheReader implements the `whosonfirst/go-reader` interface for use with a caching layer for reading documents.

func (*CacheReader) Read

func (cr *CacheReader) Read(ctx context.Context, key string) (io.ReadSeekCloser, error)

Read returns an `io.ReadSeekCloser` instance for the document resolved by `uri`. The document will also be added to the internal cache maintained by 'cr' if it not already present.

func (*CacheReader) ReaderURI added in v0.2.0

func (cr *CacheReader) ReaderURI(ctx context.Context, key string) string

ReaderURI returns final URI resolved by `uri` for this reader.

type CacheReaderOptions added in v1.1.0

type CacheReaderOptions struct {
	Reader reader.Reader
	Cache  cache.Cache
}

type CacheReaderOptions is a struct for use with the `NewCacheReaderWithOptions` method.

type CacheStatus added in v1.0.0

type CacheStatus uint8

type CacheStatus is a constant indicating the cache status for a read action

const (
	// CacheNotFound signals that there are no recorded events for a cache lookup
	CacheNotFound CacheStatus = iota
	// CacheHit signals that the last read event was a cache HIT
	CacheHit
	// CacheHit signals that the last read event was a cache MISS
	CacheMiss
)

func GetLastRead added in v1.0.0

func GetLastRead(cr reader.Reader, key string) (CacheStatus, bool)

GetLastRead returns the `CacheStatus` value for the last event using 'cr' to read 'path'.

Jump to

Keyboard shortcuts

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