...

Package vault

import "github.com/Diarkis/diarkis/vault"
Overview
Index

Overview ▾

Index ▾

func OnEmpty(cb func())
func Setup()
func Stop(ctx context.Context, next func(error))
type Storage
    func NewVault(name string, interval int64) *Storage
    func (vs *Storage) AsyncGet(key string, operation func(interface{}, func()))
    func (vs *Storage) Clear(vaultName string)
    func (vs *Storage) Del(key string) bool
    func (vs *Storage) DelAndSet(delKey, setKey string, values interface{}, ttlDuration int64) bool
    func (vs *Storage) DelIf(key string, condition func(val interface{}) bool) bool
    func (vs *Storage) DisableStatic(key string) bool
    func (vs *Storage) EnableStatic(key string) bool
    func (vs *Storage) Exists(key string) bool
    func (vs *Storage) Extend(key string, ttl int64) bool
    func (vs *Storage) Get(key string) interface{}
    func (vs *Storage) GetAll() []interface{}
    func (vs *Storage) GetAllKeys() []string
    func (vs *Storage) IsEmpty() bool
    func (vs *Storage) Length() int
    func (vs *Storage) OnDel(callback func(string, interface{}))
    func (vs *Storage) OnDelAndSet(cb func(delKey, setKey string, delValues, setValues interface{}))
    func (vs *Storage) Peek(key string) interface{}
    func (vs *Storage) Set(key string, values interface{}, ttlDuration int64) bool
    func (vs *Storage) SetAsStatic(key string, values interface{}) bool
    func (vs *Storage) SetIfNotExists(key string, ttlDuration int64, cb func() interface{}) bool
    func (vs *Storage) SetOrUpdate(key string, operation func(interface{}) interface{}, ttlDuration int64, isStatic bool) bool
    func (vs *Storage) Update(key string, operation func(interface{})) interface{}
    func (vs *Storage) UpdateWithoutTTLChange(key string, operation func(interface{})) interface{}

func OnEmpty

func OnEmpty(cb func())

OnEmpty registers a callback to be invoked when all vaults are empty before shutting down

func Setup

func Setup()

Setup Sets up vault

func Stop

func Stop(ctx context.Context, next func(error))

Stop Stops vault

type Storage

Storage storage of each vault

type Storage struct {
    sync.RWMutex
    // contains filtered or unexported fields
}

func NewVault

func NewVault(name string, interval int64) *Storage

NewVault Creates a new vault with a name

[NOTE] Uses mutex lock internal.

IMPORTANT: create all required vaults BEFORE process start.
interval is in seconds

func (*Storage) AsyncGet

func (vs *Storage) AsyncGet(key string, operation func(interface{}, func()))

AsyncGet passes the vault data to operation callback function if the key exists.

[NOTE] Uses mutex lock internal.

This function LOCKS the vault data UNTIL operation callback calls the end callback.

func (*Storage) Clear

func (vs *Storage) Clear(vaultName string)

Clear Deletes all keys and values in the vault

[NOTE] Uses mutex lock internal.

Does trigger OnDel

func (*Storage) Del

func (vs *Storage) Del(key string) bool

Del Deletes a key in a vault by name

[NOTE] Uses mutex lock internal.

func (*Storage) DelAndSet

func (vs *Storage) DelAndSet(delKey, setKey string, values interface{}, ttlDuration int64) bool

DelAndSet is used internally.

func (*Storage) DelIf

func (vs *Storage) DelIf(key string, condition func(val interface{}) bool) bool

DelIf deletes the value of the key given if the conditions implemented by condition function are met

[NOTE] Uses mutex lock internal.

func (*Storage) DisableStatic

func (vs *Storage) DisableStatic(key string) bool

DisableStatic makes the value of the given key not static (non-static keys will expire)

[NOTE] Uses mutex lock internal.

func (*Storage) EnableStatic

func (vs *Storage) EnableStatic(key string) bool

EnableStatic makes the value of the given key static (static keys do not expire)

[NOTE] Uses mutex lock internal.

func (*Storage) Exists

func (vs *Storage) Exists(key string) bool

Exists returns true if the given key exists - this does NOT update TTL of the key

[NOTE] Uses mutex lock internal.

func (*Storage) Extend

func (vs *Storage) Extend(key string, ttl int64) bool

Extend Extends TTL of a value data in a vault

[NOTE] Uses mutex lock internal.

it allows expired vault to be extended also. TTL is in seconds.

func (*Storage) Get

func (vs *Storage) Get(key string) interface{}

Get Returns a value of a key by vault name

[NOTE] Uses mutex lock internal.

func (*Storage) GetAll

func (vs *Storage) GetAll() []interface{}

GetAll returns all items in the storage as an array

[NOTE] Uses mutex lock internal.

func (*Storage) GetAllKeys

func (vs *Storage) GetAllKeys() []string

GetAllKeys returns all keys in the storage as an array

[NOTE] Uses mutex lock internal.

func (*Storage) IsEmpty

func (vs *Storage) IsEmpty() bool

IsEmpty returns true if the vault is empty - it checks TTL on each item thus very slow and expensive

[NOTE] Uses mutex lock internal.

func (*Storage) Length

func (vs *Storage) Length() int

Length returns the number of keys in the Storage

[NOTE] Uses mutex lock internal.

func (*Storage) OnDel

func (vs *Storage) OnDel(callback func(string, interface{}))

OnDel Registers a callback function to a vault on delete.

[NOTE] Uses mutex lock internal.

The callback will be passed the key and the value that has been deleted

func (*Storage) OnDelAndSet

func (vs *Storage) OnDelAndSet(cb func(delKey, setKey string, delValues, setValues interface{}))

OnDelAndSet is used internally.

func (*Storage) Peek

func (vs *Storage) Peek(key string) interface{}

Peek returns the stored value of the key if exists.

[NOTE] Uses mutex lock internal.

This does NOT update the key's TTL unlike Get.

func (*Storage) Set

func (vs *Storage) Set(key string, values interface{}, ttlDuration int64) bool

Set Sets a value of a key by vault name - ttlDuration is in seconds

[NOTE] Uses mutex lock internal.

func (*Storage) SetAsStatic

func (vs *Storage) SetAsStatic(key string, values interface{}) bool

SetAsStatic sets a static key that will NEVER be discarded

[NOTE] Uses mutex lock internal.

IMPORTANT A static key will NEVER be discarded from the server memory

IMPORTANT A static key may sill be deleted by using Del()

func (*Storage) SetIfNotExists

func (vs *Storage) SetIfNotExists(key string, ttlDuration int64, cb func() interface{}) bool

SetIfNotExists sets the given key and the return value of the callback as its value to the storage.

The condition for setting the key and the value is to have the same key not exist in the storage, and the callback must return an interface{} as its value.

It returns false if the key and the value are not stored.

func (*Storage) SetOrUpdate

func (vs *Storage) SetOrUpdate(key string, operation func(interface{}) interface{}, ttlDuration int64, isStatic bool) bool

SetOrUpdate checks to see if the key given exists.

[NOTE] Uses mutex lock internal.

If the key does not exist or has expired,
   it creates a new entry by using the returned value interface{} from operation function.
If key exists and it is valid, it allows operation function to update the entry passed to it.
If isStatic is true, the item will NEVER be discarded

func (*Storage) Update

func (vs *Storage) Update(key string, operation func(interface{})) interface{}

Update Updates a value of a key by vault name and returns the updated data

[NOTE] Uses mutex lock internal.

func (*Storage) UpdateWithoutTTLChange

func (vs *Storage) UpdateWithoutTTLChange(key string, operation func(interface{})) interface{}

UpdateWithoutTTLChange updates the value of the key given without updating the key's TTL.

[NOTE] Uses mutex lock internal.