Back to godoc.org

Package querycache

v0.0.0-...-9e8acb2
Latest Go to latest

The latest major version is .

Published: Jun 23, 2020 | License: MIT | Module: github.com/cga1123/bissy-api

Index

type CachedExecutor

type CachedExecutor struct {
	Cache    QueryCache
	Executor Executor
	Store    QueryStore
	Clock    utils.Clock
}

CachedExecutor implements Executor that caches query results for the given Lifetime of a Query

func NewCachedExecutor

func NewCachedExecutor(cache QueryCache, store QueryStore, clock utils.Clock, executor Executor) *CachedExecutor

NewCachedExecutor sets up a new CachedExecutor

func (*CachedExecutor) Execute

func (cache *CachedExecutor) Execute(query *Query) (string, error)

Execute checks the cache for the given query cache, fallsback to the the configured executor if no results are found and stores the new results.

type Config

type Config struct {
	QueryStore      QueryStore
	DatasourceStore DatasourceStore
	Executor        Executor
	Cache           QueryCache
	Clock           utils.Clock
}

Config contains everything external required to setup querycache

func (*Config) SetupHandlers

func (c *Config) SetupHandlers(router *mux.Router)

SetupHandlers mounts the querycache handlers onto the given mux

type CreateDatasource

type CreateDatasource struct {
	Name    string `json:"name"`
	Type    string `json:"type"`
	Options string `json:"options"`
}

CreateDatasource describes the required paramater to create a new Datasource

type CreateQuery

type CreateQuery struct {
	Query        string   `json:"query"`
	Lifetime     Duration `json:"lifetime"`
	DatasourceID string   `json:"datasourceId"`
}

CreateQuery describes the required parameter to create a new Query

type Datasource

type Datasource struct {
	ID        string    `json:"id" db:"id"`
	UserID    string    `json:"userId" db:"user_id"`
	Name      string    `json:"name"`
	Type      string    `json:"type"`
	Options   string    `json:"options"`
	CreatedAt time.Time `json:"createdAt" db:"created_at"`
	UpdatedAt time.Time `json:"updatedAt" db:"updated_at"`
}

Datasource describes a database that Queries may be related to and executed against

func (*Datasource) NewExecutor

func (a *Datasource) NewExecutor() (Executor, error)

NewExecutor returns a new SQLExecutor configured against this Datasource Will return a TestExecutor if the datasources "Type" is "test"

type DatasourceStore

type DatasourceStore interface {
	Get(string, string) (*Datasource, error)
	Create(string, *CreateDatasource) (*Datasource, error)
	List(string, int, int) ([]*Datasource, error)
	Delete(string, string) (*Datasource, error)
	Update(string, string, *UpdateDatasource) (*Datasource, error)
}

DatasourceStore describes a generic Store for Datasources

type Duration

type Duration time.Duration

Duration is an alias to time.Duration to allow for defining JSON marshalling and unmarshalling

func (Duration) MarshalJSON

func (d Duration) MarshalJSON() ([]byte, error)

MarshalJSON marshals a Duration into JSON

func (*Duration) UnmarshalJSON

func (d *Duration) UnmarshalJSON(b []byte) error

UnmarshalJSON unmarshals JSON into a Duration

type Executor

type Executor interface {
	Execute(*Query) (string, error)
}

Executor defines the interface to execute a query

type InMemoryCache

type InMemoryCache struct {
	Cache map[string]string
	// contains filtered or unexported fields
}

InMemoryCache is an in-memory implementation of QueryCache

func NewInMemoryCache

func NewInMemoryCache() *InMemoryCache

NewInMemoryCache sets up a new InMemoryCache

func (*InMemoryCache) Get

func (cache *InMemoryCache) Get(query *Query) (string, bool)

Get returns the cached results for a given query

func (*InMemoryCache) Set

func (cache *InMemoryCache) Set(query *Query, result string) error

Set caches the results for a given query

type Query

type Query struct {
	ID           string    `json:"id"`
	UserID       string    `json:"userId" db:"user_id"`
	Query        string    `json:"query"`
	DatasourceID string    `json:"datasourceId" db:"datasource_id"`
	Lifetime     Duration  `json:"lifetime"`
	CreatedAt    time.Time `json:"createdAt" db:"created_at"`
	UpdatedAt    time.Time `json:"updatedAt" db:"updated_at"`
	LastRefresh  time.Time `json:"lastRefresh" db:"last_refresh"`
}

Query describes an SQL query on a given datasource that should be cached for a given Lifetime value

func (*Query) Fresh

func (query *Query) Fresh(now time.Time) bool

Fresh determines whether a query was last refreshed within Lifetime of the given time parameter

type QueryCache

type QueryCache interface {
	Get(*Query) (string, bool)
	Set(*Query, string) error
}

QueryCache defines the interface for a cache of query results

type QueryStore

type QueryStore interface {
	Get(string, string) (*Query, error)
	Create(string, *CreateQuery) (*Query, error)
	List(string, int, int) ([]*Query, error)
	Delete(string, string) (*Query, error)
	Update(string, string, *UpdateQuery) (*Query, error)
}

QueryStore describes a generic Store for Queries

type RedisCache

type RedisCache struct {
	Client *redis.Client
}

RedisCache is a redis-backed implementation of QueryCache

func (*RedisCache) Get

func (cache *RedisCache) Get(query *Query) (string, bool)

Get returns the cached results for a given query

func (*RedisCache) Set

func (cache *RedisCache) Set(query *Query, result string) error

Set caches the results for a given query

type SQLDatasourceStore

type SQLDatasourceStore struct {
	// contains filtered or unexported fields
}

SQLDatasourceStore describes an SQL implementation of DatasourceStore

func NewSQLDatasourceStore

func NewSQLDatasourceStore(db *hnysqlx.DB, clock utils.Clock, generator utils.IDGenerator) *SQLDatasourceStore

NewSQLDatasourceStore retunes a new SQLDatasourceStore

func (*SQLDatasourceStore) Create

func (s *SQLDatasourceStore) Create(userID string, ca *CreateDatasource) (*Datasource, error)

Create creates and persists a new Datasource to the Store

func (*SQLDatasourceStore) Delete

func (s *SQLDatasourceStore) Delete(userID, id string) (*Datasource, error)

Delete removes the Datasource with given id from the Store

func (*SQLDatasourceStore) Get

func (s *SQLDatasourceStore) Get(userID, id string) (*Datasource, error)

Get returns the Datasource with associated id from the store

func (*SQLDatasourceStore) List

func (s *SQLDatasourceStore) List(userID string, page, per int) ([]*Datasource, error)

List returns the requests Datasources from the Store, ordered by createdAt

func (*SQLDatasourceStore) Update

func (s *SQLDatasourceStore) Update(userID, id string, ua *UpdateDatasource) (*Datasource, error)

Update updates the Datasource with associated id from the store

type SQLExecutor

type SQLExecutor struct {
	// contains filtered or unexported fields
}

SQLExecutor implements Executor against an *sql.DB

func NewSQLExecutor

func NewSQLExecutor(driver, conn string) (*SQLExecutor, error)

NewSQLExecutor builds a new SQLExecutor, parameters are passed to sql.Open

func (*SQLExecutor) Execute

func (sql *SQLExecutor) Execute(query *Query) (string, error)

Execute runs the query against the configured database returns the results as a CSV string

type SQLQueryStore

type SQLQueryStore struct {
	// contains filtered or unexported fields
}

SQLQueryStore defines an SQL implementation of a QueryStore

func NewSQLQueryStore

func NewSQLQueryStore(db *hnysqlx.DB, clock utils.Clock, generator utils.IDGenerator) *SQLQueryStore

NewSQLQueryStore builds a new SQLQueryStore

func (*SQLQueryStore) Create

func (s *SQLQueryStore) Create(userID string, ca *CreateQuery) (*Query, error)

Create creates and persist to memory a Query from a CreateQuery struct

func (*SQLQueryStore) Delete

func (s *SQLQueryStore) Delete(userID, id string) (*Query, error)

Delete removes the Query with associated id from the store

func (*SQLQueryStore) Get

func (s *SQLQueryStore) Get(userID, id string) (*Query, error)

Get returns the Query with associated id from the store

func (*SQLQueryStore) List

func (s *SQLQueryStore) List(userID string, page, per int) ([]*Query, error)

List returns the requests Queries from the Store, ordered by createdAt

func (*SQLQueryStore) Update

func (s *SQLQueryStore) Update(userID, id string, uq *UpdateQuery) (*Query, error)

Update updates the Query with associated id from the store

type TestExecutor

type TestExecutor struct{}

TestExecutor implements an Executor that echoes the passed query

func (*TestExecutor) Execute

func (t *TestExecutor) Execute(query *Query) (string, error)

Execute echoes the given query

type UpdateDatasource

type UpdateDatasource struct {
	Name    *string `json:"name"`
	Type    *string `json:"type"`
	Options *string `json:"options"`
}

UpdateDatasource describes the paramater which may be updated on a Datasource

type UpdateQuery

type UpdateQuery struct {
	Lifetime    *Duration `json:"lifetime"`
	LastRefresh time.Time `json:"lastRefresh"`
}

UpdateQuery describes the paramater which may be updated on a Query

Package Files

Documentation was rendered with GOOS=linux and GOARCH=amd64.

Jump to identifier

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to identifier