cache

package
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Jun 11, 2020 License: Apache-2.0 Imports: 9 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type DB

type DB interface {
	Get(string) ([]byte, error)
	Put(string, []byte) error
	Delete(string) error
	Path() string
	Close() error
}

DB is an interface that represents a database wrapper.

type DataBase

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

DataBase is a wrapper struct for boltdb key-value pair database.

Example
package main

import (
	"fmt"
	"log"

	"github.com/harrybrwn/apizza/pkg/cache"
	"github.com/harrybrwn/apizza/pkg/tests"
)

func main() {
	// open the database
	db, err := cache.GetDB(tests.TempFile())
	if err != nil {
		log.Fatal(err)
	}
	defer func() {
		if err = db.Close(); err != nil {
			log.Fatal(err)
		}
	}()

	if err = db.Put("key", []byte("some string of values")); err != nil {
		log.Fatal(err)
	}

	data, err := db.Get("key")
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println(string(data))

	if err = db.Destroy(); err != nil {
		panic(err)
	}
}
Output:

some string of values

func GetDB

func GetDB(dbfile string) (db *DataBase, err error)

GetDB returns an initialized DataBase. Will either create a brand new boltdb or open existing one.

func (DataBase) Close

func (idb DataBase) Close() error

Close will close the DataBase's inner bolt.DB

func (DataBase) Delete

func (idb DataBase) Delete(key string) error

Delete removes the data for a specific key.

func (*DataBase) DeleteBucket

func (db *DataBase) DeleteBucket(name string) error

DeleteBucket will delete the bucket given.

Will panic if the name argument is the same as the database's default bucket.

func (*DataBase) Destroy

func (db *DataBase) Destroy() error

Destroy will close the database and completely delete the database file.

func (*DataBase) Exists

func (db *DataBase) Exists(key string) bool

Exists will return true if the key supplied has data associated with it.

func (DataBase) Get

func (idb DataBase) Get(key string) (raw []byte, err error)

Get will retrieve the value given a key

func (*DataBase) Map

func (db *DataBase) Map() (all map[string][]byte, err error)

Map returns a map of all the key-value pairs in the database.

func (DataBase) Path

func (idb DataBase) Path() string

func (DataBase) Put

func (idb DataBase) Put(key string, val []byte) error

Put stores bytes the database

func (*DataBase) ResetTimeStamp

func (db *DataBase) ResetTimeStamp(key string) error

ResetTimeStamp stores a new timestamp for the given key.

func (*DataBase) SetBucket

func (db *DataBase) SetBucket(name string)

SetBucket will set the bucket used in all database transactions.

func (*DataBase) TimeStamp

func (db *DataBase) TimeStamp(key string) (time.Time, error)

TimeStamp gets the timestamp for a given key and will also create one if it does not already exist.

func (*DataBase) UpdateTS

func (db *DataBase) UpdateTS(key string, updater Updater) error

UpdateTS or "UpdateTimeStamp" will execute an Updater's methods in correspondence with the database's timestamp at the key given

func (*DataBase) WithBucket

func (db *DataBase) WithBucket(bucket string) *DataBase

WithBucket temporarily sets the bucket to the string given and returns the database with the new bucket.

The default bucket will be reset when the database calls Put, Get, Exists, Map, TimeStamp, and UpdateTS (any method that calls view or update internally).

type Deleter

type Deleter interface {
	Delete(string) error
}

Deleter is an interface that defines objects that delete.

type FullDB

type FullDB interface {
	MapDB
	Exists(string) bool
	Destroy() error
}

FullDB defines a fully featured database.

type Getter

type Getter interface {
	Get(string) ([]byte, error)
}

Getter is an object that gets.

type MapDB

type MapDB interface {
	DB
	Map() (map[string][]byte, error)
}

MapDB defines a database that can be converted to a map.

type Putter

type Putter interface {
	Put(string, []byte) error
}

Putter is an object that puts.

type Storage

type Storage interface {
	Getter
	Putter
}

Storage is a combination of Putter and Getter.

type TimeStamper

type TimeStamper interface {
	TimeStamp(string) (time.Time, error)
	ResetTimeStamp(string) error
}

TimeStamper defines objects that support named timestamps.

type TimerDB

type TimerDB interface {
	DB
	TimeStamper
}

TimerDB is an interface that defines a database that can store timestamps.

type Updater

type Updater interface {
	OnUpdate() error
	NotUpdate() error
	Decay() time.Duration
}

Updater defines an interface for objects used while updating cached timestamps.

func NewUpdater

func NewUpdater(decay time.Duration, update, notUpdate func() error) Updater

NewUpdater returns an updater from a decay time and two functions.

Jump to

Keyboard shortcuts

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