exkv

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Feb 26, 2020 License: Apache-2.0 Imports: 2 Imported by: 0

README

exkv Key-Value Cache

exkv.Cache is an in-memory key-value store with expiration handling & automatic cleanups. It also supports index aliases, making it easy to reference a single entry by multiple keys (e.g. a user by its username or its email address).

Basic Usage Example

var myCache = &exkv.Cache{
	DefaultTimeout: 5 * time.Minute,
	CleanupInterval: 30 * time.Minute,
	Indexer: func(name string, value interface{}) map[string]string {
		return map[string]string{
			"email": value.(myCacheItem).Email,
		}
	},
}

type myCacheEntry struct {
	Username string
	Email string
	IsAdmin bool
}


// Writing to the cache:
myCache.Set("example", myCacheEntry{ "example", "mail@example.org", false })
myCache.SetWithTimeout("homer", myCacheEntry{ "homer", "homer@example.org", true }, exkv.Forever)
myCache.SetWithTimeout("linda", myCacheEntry{ "linda", "linda@example.org", true }, 2 * time.Second)

time.Sleep(5 * time.Second)
println(myCache.Get("linda")) // nil


// Retrieve some information either from the cache, or from a complex query:
var username = "example"
if e := myCache.Get(username); e != nil {
	return e.(myCacheEntry)
}
var user myCacheEntry
// TODO: Do very complicated stuff to retrieve the user details ...
myCache.Set(username, user)


// Get a list of all admins:
var users = myCache.Filter(func(name string, value interface{}) bool {
	return value.(myCacheEntry).IsAdmin
})
println(users["homer"].Username) // homer


// Use the index to retrieve a user, and reset the expiration:
var name, user = myCache.GetAndRenewByIndex("email", "homer@example.org")
println(name) // email
println(user.IsAdmin) // true


// Delete an entry manually:
myCache.Set("example", nil)

For advanced documentation, please refer to the GoDoc documentation.

Documentation

Index

Constants

View Source
const Forever time.Duration = -1

Forever can be used as an expiration timeout to make entries valid forever.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

type Cache struct {
	DefaultTimeout  time.Duration // Default expiration timeout for new entries created with cache.Set(...), or a negative numbers to make entries valid forever; defaults to 30 seconds
	CleanupInterval time.Duration // Minimum interval for automatic cleanups (which will be triggered on any operation on the Cache); the default value 0 disables automatic cleanups
	Indexer         CacheIndexer  // Function that maps a name and value of an entry to its index alias values (map[index]value)
	// contains filtered or unexported fields
}

Cache is an in-memory key-value store with expiration handling & automatic cleanups.

func (*Cache) Cleanup

func (cache *Cache) Cleanup()

Cleanup removes expired entries from the cache. It will be called automatically on every cache operation if CleanupInterval is set no a non-zero value.

func (*Cache) Filter

func (cache *Cache) Filter(fn CacheFilter) map[string]interface{}

Filter returns a set of entries from the cache, filtered by a function that returns true if the entry (consisting of name & value) should be included. Note that it can be really slow for large caches!

func (*Cache) Get

func (cache *Cache) Get(name string) interface{}

Get retrieves an entry from the cache if it exists & is valid, otherwise it returns nil.

func (*Cache) GetAndRenew

func (cache *Cache) GetAndRenew(name string) interface{}

GetAndRenew retrieves an entry from the cache if it exists & is valid, otherwise it returns nil. Additionally, it renews the expiration of the entry to the current time + the entry's expiration timeout.

func (*Cache) GetAndRenewByIndex

func (cache *Cache) GetAndRenewByIndex(index string, value string) (string, interface{})

GetAndRenewByIndex retrieves an entry's name and value from the cache if it exists & is valid, using an index alias instead of the entry's name directly. If the index, the index value, or the entry doesn't exist, it returns nil as a value. Additionally, it renews the expiration of the entry to the current time + the entry's expiration timeout (analog to GetAndRenew).

func (*Cache) GetByIndex

func (cache *Cache) GetByIndex(index string, value string) (string, interface{})

GetByIndex retrieves an entry's name and value from the cache if it exists & is valid, using an index alias instead of the entry's name directly. If the index, the index value, or the entry doesn't exist, it returns nil as a value.

func (*Cache) Index

func (cache *Cache) Index(name string, index string, value string)

Index adds an index alias to an entry. If there's e.g. an entry with the name "example", and it has "email" set to "mail@example.org", you can use cache.Index("example","email","mail@example.org) to make it available later with cache.GetByIndex("email","mail@example.org"). Previous index aliases will be overwritten.

func (*Cache) Set

func (cache *Cache) Set(name string, value interface{})

Set stores a value in the Cache, using the default timeout of the Cache. Setting the value to nil deletes the entry.

func (*Cache) SetWithTimeout

func (cache *Cache) SetWithTimeout(name string, value interface{}, timeout time.Duration)

SetWithTimeout stores a value in the Cache, using a custom timeout. Setting the value to nil deletes the entry.

type CacheFilter

type CacheFilter func(string, interface{}) bool

CacheFilter is a function that returns true if the entry (consisting of name & value) should be included by Cache.Filter.

var All CacheFilter = func(string, interface{}) bool {
	return true
}

All is a filter function that includes all entries.

type CacheIndexer

type CacheIndexer func(string, interface{}) map[string]string

CacheIndexer is a function that maps a name and value of an entry to its index alias values (map[index]value).

Jump to

Keyboard shortcuts

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