filestorage

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

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

Go to latest
Published: Aug 28, 2019 License: MIT Imports: 8 Imported by: 0

README

filestorage

see GoDoc.

Installation

go get github.com/RyoNkmr/filestorage

Example

type Hoge struct {
	Hoge int
	Fuga *Fuga
}

type Fuga struct {
	Hoge int
}

func newHoge(i int) *Hoge {
	return &Hoge{
		Hoge: i,
		Fuga: &Fuga{
			Hoge: i,
		},
	}
}

func main() {
	fs := filestorage.NewFileStorage(".")
	a := newHoge(1)

	if err := fs.Set("1", &a, nil); err != nil {
		panic(err)
	}
	fmt.Printf("%#v\n", a) // &main.Hoge{Hoge:1, Fuga:(*main.Fuga)(0xc0000a0000)}

	expired := time.Now().Add(1 * time.Second)
	if err := fs.Set("2", &a, &expired); err != nil {
		panic(err)
	}
	fmt.Printf("%#v\n", a) // &main.Hoge{Hoge:1, Fuga:(*main.Fuga)(0xc0000a0000)}

	time.Sleep(3 * time.Second)

	var h Hoge
	if err := fs.Get("1", &h); err != nil {
		panic(err)
	}
	fmt.Printf("%#v\n", h) // main.Hoge{Hoge:1, Fuga:(*main.Fuga)(0xc0000a0028)}

	var h2 Hoge
	if err := fs.Get("2", &h2); err != nil {
		fmt.Println(err) // filetray: data has already expired
	}
	fmt.Printf("%#v\n", h2) // main.Hoge{Hoge:0, Fuga:(*main.Fuga)(nil)}

	if err := fs.Delete("1"); err != nil {
		panic(err)
	}
	if err := fs.Get("1", &h); err != nil {
		fmt.Println(err) // FileStorage: no data
	}
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidDir    = errors.New("FileStorage: invalid dirpath given")
	ErrNoData        = errors.New("FileStorage: no data")              // file accessing methods returns this error when the given key has no data
	ErrExpired       = errors.New("FileStorage: expired")              // file accessing methods returns this error when the data is expired
	ErrLiveForever   = errors.New("FileStorage: expiredAt is not set") // IsExpired method on FileStorage returns this error when expiredAt is nil for the key
	ErrIndexNotFound = errors.New("FileStorage: indexfile is not found")
)

Functions

func NewOrLoadIndexFile

func NewOrLoadIndexFile(dirpath string) (*indexFile, error)

Types

type FileStorage

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

func NewFileStorage

func NewFileStorage(dirpath string) (storage *FileStorage, err error)

FileStorage creates the storage directory at dirpath to place the file into. NewFileStorage returns an error if it fails to prepare directory for the one.

func (*FileStorage) Delete

func (f *FileStorage) Delete(key string) (err error)

Delete removes the file and the data from the cache

func (*FileStorage) Get

func (f *FileStorage) Get(key string, dest interface{}) (err error)

Get scans the stored value from FileStorage into the destination pointer. An error is returned if the data is not stored or has already expired.

func (*FileStorage) IsExpired

func (f *FileStorage) IsExpired(key string) (isExpired bool, err error)

IsExpired checks whether the stored data is expired. ErrLiveForever is returns if expiredAt is not set.

func (*FileStorage) NoCacheGet

func (f *FileStorage) NoCacheGet(key string, dest interface{}) (err error)

Get scans the stored value from the stored file into the destination pointer. An error is returned if the data is not stored or has already expired.

func (*FileStorage) Set

func (f *FileStorage) Set(key string, value interface{}, expiredAt *time.Time) (err error)

Set stores value into FileStorage. FileStorage creates file each key at "dirpath/key"

type Storage

type Storage interface {
	Get(key string, dest interface{}) error
	NoCacheGet(key string, dest interface{}) error
	IsExpired(key string) (isExpired bool, err error)
	Set(key string, value interface{}, expiredAt *time.Time) error
	Delete(key string) error
}

A storage with cache implementation

Jump to

Keyboard shortcuts

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