cache

package
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Nov 19, 2020 License: MIT Imports: 6 Imported by: 0

Documentation

Index

Constants

View Source
const (

	// Name of the directory to store the caches in
	CacheDirName = ".yamlcrypt.cache"
)

Variables

View Source
var YoungCacheSize int64 = 1024 * 1024 * 100

Max young cache size: 100MiB by default (can be shrunk for tests)

Functions

This section is empty.

Types

type Cache

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

A quick and dirty "LRU-ish" cache. Maintains a read/write "young" cache, and a read-only "old" cache. New values are added to the "young" cache. When looking up a value, if it's present in the "young" cache, retrieve it from there. If it's present in the "old" cache, retrieve it from there, copying it into the "young" cache. When the "young" cache gets too big, the current "old" cache is removed and the current "young" cache takes its place. This only happens on close since the lifecycle of this object is expected to be pretty short in this application, but the benefit of this is: during a session, any values added to the cache are guaranteed to remain present until at least the end of the session (technically, until the end of the next session, due to the "old" cache). Getting and inserting values are protected with a mutex, making this safe for parallel access, if a bit of a drag.

func Setup

func Setup(config config.Config) (Cache, error)

Initialize the cache.

func (*Cache) Add

func (c *Cache) Add(plaintext string, ciphertext []byte) error

Add a (plaintext, ciphertext) pair to the young cache. Protected with a mutex.

func (*Cache) Close

func (c *Cache) Close() error

Close the cache, doing some cleanup as well. Must be called before exiting

func (*Cache) Decrypt

func (c *Cache) Decrypt(ciphertext []byte) (string, bool, error)

Look up the plaintext for a given ciphertext. Protected with a mutex.

func (*Cache) Encrypt

func (c *Cache) Encrypt(plaintext string) ([]byte, bool, error)

Look up the ciphertext for a given plaintext. Protected with a mutex.

Jump to

Keyboard shortcuts

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