Documentation ¶
Index ¶
- Constants
- Variables
- func Hit(err error) (bool, error)
- func Miss(err error) (bool, error)
- func URIFromPath(path string) string
- func URITemp() string
- func Unzip[V any](kv []KV[V]) (keys []string, vals []V)
- func Vacuum(interval time.Duration, max int) func(cache *Cache)
- type Cache
- func (c *Cache) BatchEvict(keys []string) (evicted []KV[[]byte], err error)
- func (c *Cache) BatchGet(keys []string) ([]KV[[]byte], error)
- func (c *Cache) BatchSet(rows []KV[[]byte]) error
- func (c *Cache) Close() error
- func (c *Cache) Evict(key string) (kv KV[[]byte], err error)
- func (c *Cache) EvictAll() (len int, err error)
- func (c *Cache) Get(key string) ([]byte, error)
- func (c *Cache) GetOr(key string, fetch func(k string) ([]byte, error)) ([]byte, error)
- func (c *Cache) ItrKeys(from string, to string) iter.Seq[string]
- func (c *Cache) ItrRange(from string, to string) iter.Seq2[string, []byte]
- func (c *Cache) ItrValues(from string, to string) iter.Seq[[]byte]
- func (c *Cache) Keys(from string, to string) (keys []string, err error)
- func (c *Cache) NS(ns string, ops ...Op) (*Cache, error)
- func (c *Cache) Range(from string, to string) (kv []KV[[]byte], err error)
- func (c *Cache) Set(key string, value []byte) error
- func (c *Cache) SetTTL(key string, value []byte, ttl time.Duration) error
- func (c *Cache) Vacuum(max int) (n int, err error)
- func (c *Cache) Values(from string, to string) (values [][]byte, err error)
- type KV
- type Op
- type TypedCache
- func (t *TypedCache[V]) BatchEvict(keys []string) ([]KV[V], error)
- func (t *TypedCache[V]) BatchGet(keys []string) ([]KV[V], error)
- func (t *TypedCache[V]) BatchSet(ziped []KV[V]) error
- func (t *TypedCache[V]) Evict(key string) (KV[V], error)
- func (t *TypedCache[V]) EvictAll() (int, error)
- func (t *TypedCache[V]) Get(key string) (V, error)
- func (t *TypedCache[V]) GetOr(key string, getter func(key string) (V, error)) (V, error)
- func (t *TypedCache[V]) ItrKeys(from string, to string) iter.Seq[string]
- func (t *TypedCache[V]) ItrRange(from string, to string) iter.Seq2[string, V]
- func (t *TypedCache[V]) ItrValues(from string, to string) iter.Seq[V]
- func (t *TypedCache[V]) Keys(from string, to string) (keys []string, err error)
- func (t *TypedCache[V]) Range(from string, to string) ([]KV[V], error)
- func (t *TypedCache[V]) Raw() *Cache
- func (t *TypedCache[V]) Set(key string, value V) error
- func (t *TypedCache[V]) SetTTL(key string, value V, ttl time.Duration) error
- func (t *TypedCache[V]) Values(from string, to string) (values []V, err error)
Constants ¶
const MAX_BLOB_SIZE = 1_000_000_000 - 100 // 1GB - 100 bytes
const MAX_PARAMS = 999
const NO_TTL = time.Hour * 24 * 365 * 100 // 100 years (effectively forever)
NO_TTL is a constant that represents no ttl, kind of, it is really just a very long time for a cache
const NS_DEFAULT = "default"
const RANGE_MAX = string(byte(255))
const RANGE_MIN = string(byte(0))
Variables ¶
var NotFound = errors.New("not found")
Functions ¶
func URIFromPath ¶
Types ¶
type Cache ¶
type Cache struct {
// contains filtered or unexported fields
}
func (*Cache) BatchEvict ¶
BatchEvict evicts a batch of keys from the cache
if onEvict is set, it will be called for each key the eviction will take place in one transaction, but split up into bacthes of MAX_PARAMS, ie 999, in order to have the eviction be atomic. If one key fails to evict, the whole batch will fail. Prefer batches less then MAX_PARAMS
func (*Cache) BatchGet ¶
BatchGet retrieves a batch of keys from the cache
the BatchGet will take place in one transaction, but split up into sub-batches of MAX_PARAMS size, ie 999, in order to have the BatchGet be atomic. If one key fails to fetched, the whole batch will fail. Prefer batches less then MAX_PARAMS
func (*Cache) BatchSet ¶
BatchSet sets a batch of key/value pairs in the cache
the BatchSet will take place in one transaction, but split up into sub-batches of MAX_PARAMS/3 size, ie 999/3 = 333, in order to have the BatchSet be atomic. If one key fails to set, the whole batch will fail. Prefer batches less then MAX_PARAMS
func (*Cache) Evict ¶
Evict evicts a key from the cache if onEvict is set, it will be called for key
func (*Cache) GetOr ¶
GetOr retrieves a value from the cache, if the key does not exist it will call the setter function and set the result.
If multiple goroutines call GetOr with the same key, only one will call the fetch function the others will wait for the first to finish and retrieve the cached value from the first call. It is useful paradigm to lessen a thundering herd problem. This is done by locking on the provided key in the application layer, not the database layer. meaning, this might work poorly if multiple applications are using the same sqlite cache files.
func (*Cache) ItrKeys ¶
ItrKeys returns an iterator for the range of keys [from, to]
WARNING Since iterators don't really have any way of communication errors the Con is that errors are dropped when using iterators. the Pro is that it is very easy to use, and scan row by row (ie. no need to load all rows into memory)
func (*Cache) ItrRange ¶
ItrRange returns an iterator for the range of keys [from, to]
WARNING Since iterators don't really have any way of communication errors the Con is that errors are dropped when using iterators. the Pro is that it is very easy to use, and scan row by row (ie. no need to load all rows into memory)
func (*Cache) ItrValues ¶
ItrValues returns an iterator for the range of values [from, to]
WARNING Since iterators don't really have any way of communication errors the Con is that errors are dropped when using iterators. the Pro is that it is very easy to use, and scan row by row (ie. no need to load all rows into memory)
func (*Cache) NS ¶
NS creates a new namespace, if the namespace already exists it will return the existing namespace.
onEvict must be set for every new namespace created using WithEvictCallback. NS will create a new table in the database for the namespace in order to isolate it, and the indexes.
type Op ¶
func DBPragma ¶
DBPragma is a helper function to set a pragma on the database
see https://www.sqlite.org/pragma.html
example:
DBPragma("journal_size_limit = 6144000")
func DBRemoveOnClose ¶
func DBRemoveOnClose() Op
DBRemoveOnClose is a helper function to remove the database files on close
func DBSyncOff ¶
func DBSyncOff() Op
DBSyncOff is a helper function to set
synchronous = off
this is useful for write performance but effects read performance and durability
func WithEvictCallback ¶
WithEvictCallback sets the callback function to be called when a key is evicted
func WithVacuum ¶
WithVacuum sets the vacuum function to be called in a go routine
type TypedCache ¶
type TypedCache[V any] struct { // contains filtered or unexported fields }
func Of ¶
func Of[V any](cache *Cache) *TypedCache[V]
Of creates a typed cache facade using generics. encoding/gob is used for serialization and deserialization
Example: cache, err := cove.New(cove.URITemp(), cove.DBRemoveOnClose()) assert.NoError(err) // creates a namespace that is separate from the main cache, // this helps to avoid key collisions and allows for easier management of keys // when using multiple types separateNamespace, err := cache.NS("my-strings") assert.NoError(err) stringCache := cove.Of[string](stringNamespace) stringCache.Set("hello", "typed world") fmt.Println(stringCache.Get("hello")) // Output: typed world <nil>
func (*TypedCache[V]) BatchEvict ¶
func (t *TypedCache[V]) BatchEvict(keys []string) ([]KV[V], error)
BatchEvict evicts a batch of keys from the cache
if onEvict is set, it will be called for each key the eviction will take place in one transaction, but split up into bacthes of MAX_PARAMS, ie 999, in order to have the eviction be atomic. If one key fails to evict, the whole batch will fail. Prefer batches less then MAX_PARAMS
func (*TypedCache[V]) BatchGet ¶
func (t *TypedCache[V]) BatchGet(keys []string) ([]KV[V], error)
BatchGet retrieves a batch of keys from the cache
the BatchGet will take place in one transaction, but split up into sub-batches of MAX_PARAMS size, ie 999, in order to have the BatchGet be atomic. If one key fails to fetched, the whole batch will fail. Prefer batches less then MAX_PARAMS
func (*TypedCache[V]) BatchSet ¶
func (t *TypedCache[V]) BatchSet(ziped []KV[V]) error
BatchSet sets a batch of key/value pairs in the cache
the BatchSet will take place in one transaction, but split up into sub-batches of MAX_PARAMS/3 size, ie 999/3 = 333, in order to have the BatchSet be atomic. If one key fails to set, the whole batch will fail. Prefer batches less then MAX_PARAMS
func (*TypedCache[V]) Evict ¶
func (t *TypedCache[V]) Evict(key string) (KV[V], error)
Evict evicts a key from the cache
if onEvict is set, it will be called for key
func (*TypedCache[V]) EvictAll ¶
func (t *TypedCache[V]) EvictAll() (int, error)
EvictAll evicts all keys in the cache
onEvict will not be called
func (*TypedCache[V]) Get ¶
func (t *TypedCache[V]) Get(key string) (V, error)
Get returns the value for the key
func (*TypedCache[V]) GetOr ¶
func (t *TypedCache[V]) GetOr(key string, getter func(key string) (V, error)) (V, error)
GetOr returns the value for the key, if the key does not exist it will call the getter function to get the value
func (*TypedCache[V]) ItrKeys ¶
ItrKeys returns a key iterator for the range of keys [from, to]
WARNING Since iterators don't really have any way of communication errors the Con is that errors are dropped when using iterators. the Pro is that it is very easy to use, and scan row by row (ie. no need to load all rows into memory)
func (*TypedCache[V]) ItrRange ¶
ItrRange returns a k/v iterator for the range of keys [from, to]
WARNING Since iterators don't really have any way of communication errors the Con is that errors are dropped when using iterators. the Pro is that it is very easy to use, and scan row by row (ie. no need to load all rows into memory)
func (*TypedCache[V]) ItrValues ¶
func (t *TypedCache[V]) ItrValues(from string, to string) iter.Seq[V]
ItrValues returns a value iterator for the range of keys [from, to]
WARNING Since iterators don't really have any way of communication errors the Con is that errors are dropped when using iterators. the Pro is that it is very easy to use, and scan row by row (ie. no need to load all rows into memory)
func (*TypedCache[V]) Keys ¶
func (t *TypedCache[V]) Keys(from string, to string) (keys []string, err error)
Keys returns all keys in the range [from, to]
func (*TypedCache[V]) Range ¶
func (t *TypedCache[V]) Range(from string, to string) ([]KV[V], error)
Range returns all key value pairs in the range [from, to]
func (*TypedCache[V]) Raw ¶
func (t *TypedCache[V]) Raw() *Cache
Raw returns the underlying untyped cache
func (*TypedCache[V]) Set ¶
func (t *TypedCache[V]) Set(key string, value V) error
Set sets the value for the key with the default TTL