cache

command
v0.4.1 Latest Latest
Warning

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

Go to latest
Published: Oct 13, 2023 License: MIT Imports: 5 Imported by: 0

README

Example: Key/Value Cache

This example demonstrates a Key column type that allows you to perform O(1) lookups over that. This can be used in the case where you do not have a specific offset for the entry.

// Cache represents a key-value store
type Cache struct {
	store *column.Collection
}

// New creates a new key-value cache
func New() *Cache {
	db := column.NewCollection()
	db.CreateColumn("key", column.ForKey())
	db.CreateColumn("val", column.ForString())

	return &Cache{
		store: db,
	}
}

// Get attempts to retrieve a value for a key
func (c *Cache) Get(key string) (value string, found bool) {
	c.store.QueryKey(key, func(r column.Row) error {
		value, found = r.String("val")
		return nil
	})
	return
}

// Set updates or inserts a new value
func (c *Cache) Set(key, value string) {
	if err := c.store.QueryKey(key, func(r column.Row) error {
		r.SetString("val", value)
		return nil
	}); err != nil {
		panic(err)
	}
}

Some Results

running insert of 50000 rows...
-> inserted 10000 rows
-> inserted 20000 rows
-> inserted 30000 rows
-> inserted 40000 rows
-> inserted 50000 rows
-> insert took 80.2478ms

running query of user_11255...
Hi, User 11255 true
-> query took 1.271µs

Documentation

The Go Gopher

There is no documentation for this package.

Jump to

Keyboard shortcuts

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