filecache

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Feb 23, 2020 License: MIT Imports: 11 Imported by: 0

README

logo

go-filecache

Release version Project language Build Status Coverage Go Report License

This package provides file-based cache implementation with entries expiration, checksum validation and another useful features.

Methods that interacts with file system uses mutexes, so, this cache implementation thread-safe, but performance in this case can be less than you might want.

Installation and usage

The import path for the package is github.com/tarampampam/go-filecache.

To install it, run:

go get github.com/tarampampam/go-filecache

API documentation can be found here.

Usage example
package main

import (
    "bytes"
    "fmt"
    "time"

    filecache "github.com/tarampampam/go-filecache"
)

func main() {
    // Create new cache items pool
	// Note: Do NOT create new pool instance in goroutine - SHARE it instead
    pool := filecache.NewPool("/tmp")
    
    // Put data into cache pool with expiration time
    expiresAt := time.Now().Add(time.Minute * 10)
    if _, err := pool.Put("foo", bytes.NewBuffer([]byte("foo data")), expiresAt); err != nil {
        panic(err)
    }
    
    // Put data without expiration time
    if _, err := pool.PutForever("bar", bytes.NewBuffer([]byte("bar data"))); err != nil {
        panic(err)
    }

    // Define buffer for cached data reading
    buf := bytes.NewBuffer([]byte{})

    // Read data using reader
    if err := pool.GetItem("foo").Get(buf); err != nil {
        panic(err)
    }

    fmt.Println(buf) // "foo data"
}

Simple benchmark (put value, get value, read into buffer and set new value) results:

goos: linux
goarch: amd64
pkg: github.com/tarampampam/go-filecache
BenchmarkSetAndGet-8       10000            611470 ns/op           40836 B/op         84 allocs/op
Testing

For application testing we use built-in golang testing feature and docker-ce + docker-compose as develop environment. So, just write into your terminal after repository cloning:

$ make test

Or execute benchmarks:

$ make gobench

Changelog

Release date Commits since latest release

Changes log can be found here.

Support

Issues Issues

If you will find any package errors, please, make an issue in current repository.

License

This is open-sourced software licensed under the MIT License.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultItemFilePerms os.FileMode = 0664

DefaultItemFilePerms is default permissions for file, associated with cache item

View Source
var DefaultItemFileSignature file.FSignature = nil

DefaultItemFileSignature is default signature for cache files

Functions

This section is empty.

Types

type CacheItem

type CacheItem interface {
	// Returns path to the associated file.
	GetFilePath() string

	// Returns the key for the current cache item.
	GetKey() string

	// Retrieves the value of the item from the cache associated with this object's key.
	Get(to io.Writer) error

	// Confirms if the cache item lookup resulted in a cache hit.
	IsHit() bool

	// Sets the value represented by this cache item.
	Set(from io.Reader) error

	// Returns the expiration time for this cache item. If expiration doesn't set - nil will be returned.
	ExpiresAt() *time.Time

	// Sets the expiration time for this cache item.
	SetExpiresAt(when time.Time) error
}

Item defines an interface for interacting with objects inside a cache

type CachePool

type CachePool interface {
	// Returns cache directory path.
	GetDirPath() string

	// Returns a Cache Item representing the specified key.
	GetItem(key string) CacheItem

	// Confirms if the cache contains specified cache item.
	HasItem(key string) bool

	// Deletes all items in the pool.
	Clear() (bool, error)

	// Removes the item from the pool.
	DeleteItem(key string) (bool, error)

	// Put a cache item with expiring time.
	Put(key string, from io.Reader, expiresAt time.Time) (CacheItem, error)

	// Put a cache item without expiring time.
	PutForever(key string, from io.Reader) (CacheItem, error)
}

Pool generates CacheItemInterface objects

type Error

type Error struct {
	// The type of error
	Type ErrorType

	// The error message
	Message string
	// contains filtered or unexported fields
}

func (*Error) Error

func (e *Error) Error() string

Error returns the error's message.

func (*Error) Unwrap

func (e *Error) Unwrap() error

Unwrap returns previous error. If previous error does not exits, Unwrap returns nil.

type ErrorType

type ErrorType uint8

Cache error types

const (
	ErrUnknown ErrorType = iota
	ErrFileOpening
	ErrFileReading
	ErrFileWriting
	ErrExpirationDataNotAvailable
)

func (ErrorType) String

func (e ErrorType) String() string

String for representing error message.

type Item

type Item struct {
	Pool CachePool
	// contains filtered or unexported fields
}

func (*Item) ExpiresAt

func (item *Item) ExpiresAt() *time.Time

ExpiresAt returns the expiration time for this cache item. If expiration doesn't set - nil will be returned. Important notice: returned time will be WITHOUT nanoseconds (just milliseconds).

func (*Item) Get

func (item *Item) Get(to io.Writer) error

Get retrieves the value of the item from the cache associated with this object's key.

func (*Item) GetFilePath

func (item *Item) GetFilePath() string

GetFilePath returns path to the associated file.

func (*Item) GetKey

func (item *Item) GetKey() string

GetKey returns the key for the current cache item.

func (*Item) IsExpired

func (item *Item) IsExpired() (bool, error)

Indicates if cache item expiration time is exceeded. If expiration data was not set - error will be returned.

func (*Item) IsHit

func (item *Item) IsHit() bool

IsHit confirms if the cache item lookup resulted in a cache hit.

func (*Item) Set

func (item *Item) Set(from io.Reader) error

Set the value represented by this cache item.

func (*Item) SetExpiresAt

func (item *Item) SetExpiresAt(when time.Time) error

SetExpiresAt sets the expiration time for this cache item. Important notice: time will set WITHOUT nanoseconds (just milliseconds).

type Pool

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

func NewPool

func NewPool(dirPath string) *Pool

NewPool creates new cache items pool.

func (*Pool) Clear

func (pool *Pool) Clear() (bool, error)

Clear deletes all items in the pool.

func (*Pool) DeleteItem

func (pool *Pool) DeleteItem(key string) (bool, error)

DeleteItem removes the item from the pool.

func (*Pool) GetDirPath

func (pool *Pool) GetDirPath() string

GetDirPath returns cache directory path.

func (*Pool) GetItem

func (pool *Pool) GetItem(key string) CacheItem

GetItem returns a Cache Item representing the specified key.

func (*Pool) HasItem

func (pool *Pool) HasItem(key string) bool

HasItem confirms if the cache contains specified cache item.

func (*Pool) Put

func (pool *Pool) Put(key string, from io.Reader, expiresAt time.Time) (CacheItem, error)

Put a cache item with expiring time.

func (*Pool) PutForever

func (pool *Pool) PutForever(key string, from io.Reader) (CacheItem, error)

Put a cache item without expiring time.

Directories

Path Synopsis
File implementation for using as a cache entry.
File implementation for using as a cache entry.

Jump to

Keyboard shortcuts

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