persistcache

package
v0.0.0-...-70dee08 Latest Latest
Warning

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

Go to latest
Published: May 14, 2024 License: Apache-2.0 Imports: 9 Imported by: 0

README

Persisted Cache (persistcache)

Use-case

This package implements a persistence cache. The main use-case is to store data on filesystem and provide access to it via API. Limitation of current implementation is that data should be represented as string. This was developed for patch envelope feature in EVE. Since there is an option for user to provide object to store via inline query, EVE has to store it even after reboot.

From library user perspective this library works as map[string]string with respective files created for each key in root folder.

API & Usage

Create persistCache structure by calling New

rootFilePath := "/my/root/file/path"
pc := persistcache.New(rootFilePath)

In case there are any objects (files) in specified rootFilePath, they will lazily initialized, that means, they will actually be loaded only when Get function is called

Add objects to store by calling Put

pc.Put("myValidKey", []byte("myValidValue"))

check isValidKey and isValidValue to see limitations on valid keys and values. Objects will be stored both in-memory and on filesystem.

Retrieve objects by calling Get

pc.Get("myValidKey")

Remove object from filesystem and from in-memory cache by calling Delete

pc.Delete("myValidKey")

Design decisions

Why we are storing separate files and not saving whole structure as file?
  • If objects stored are large it takes less time to save/update them and less code
  • Access to cache file is easier
  • Avoids a Put of one key from affecting the storage of another key, which could happen if it is a single file and the device is powered off before everything has been sync'ed to disk.
Why store []byte and not interface{} or string

This way library user bears responsibility of marshalling and unmarshalling object on its side, keeping persistcache library simple. In case of string it would imply a valid UTF-8 which is not required for []byte.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type InvalidKeyError

type InvalidKeyError struct{}

InvalidKeyError returned when Get or Put are called with key which cannot be written or read

func (*InvalidKeyError) Error

func (e *InvalidKeyError) Error() string

Error returns error description string

type InvalidValueError

type InvalidValueError struct{}

InvalidValueError returned when Put is called with empty value

func (*InvalidValueError) Error

func (e *InvalidValueError) Error() string

Error returns error description string

type PersistCache

type PersistCache struct {
	sync.Mutex
	// contains filtered or unexported fields
}

PersistCache is main structure storing objects both in-memory and on file system

func New

func New(path string) (*PersistCache, error)

New loads values from cache or creates path if there's none

func (*PersistCache) Delete

func (pc *PersistCache) Delete(key string) error

Delete removes element from cache and filesystem

func (*PersistCache) Get

func (pc *PersistCache) Get(key string) ([]byte, error)

Get value from cache

func (*PersistCache) Objects

func (pc *PersistCache) Objects() []string

Objects returns list objects stored in PersistCache

func (*PersistCache) Put

func (pc *PersistCache) Put(key string, val []byte) (string, error)

Put creates or updates value in in-memory cache and filesystem

Jump to

Keyboard shortcuts

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