hashcache

package module
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Aug 3, 2020 License: MIT Imports: 5 Imported by: 0

README

PkgGoDev

hashcache

hashcache is an experiment in creating a hash table / KV store from first principles.

It uses SipHash for hashing (which may be a terrible choice!)

To install:


$ go get github.com/Intermernet/hashcache

To use:

package main

import (
	"fmt"
	"log"

	"github.com/Intermernet/hashcache"
)

type kv struct {
	key   []byte
	value []byte
}

var kvs = []kv{
	{[]byte("data1"), []byte("some large data")},
	{[]byte("data2"), []byte("some more large data")},
	{[]byte("data3"), []byte("even more large data")},
}

func main() {

	c := hashcache.NewCache("Some unique 128 bit key")
	// Set the TTL
	if err := c.SetTTL(5000); err != nil {
		log.Fatalf("%v\n", err)
	}
	// Set the scavenge time
	if err := c.SetScavengeTime(1000); err != nil {
		log.Fatalf("%v\n", err)
	}
	// Add some data
	for _, kv := range kvs {
		c.Write(kv.key, kv.value)
	}
	// Read some data
	for _, kv := range kvs {
		value, ok := c.Read(kv.key)
		switch {
		case ok:
			fmt.Printf("\"%s\" found in cache: \"%s\"\n", kv.key, value)
		default:
			fmt.Printf("\"%s\" not found in cache\n", kv.key)
		}
	}
	// Delete some data
	for _, kv := range kvs {
		ok := c.Delete(kv.key)
		fmt.Printf("%s was deleted: %t\n", kv.key, ok)
	}

}

Todo:

  • Tests!
  • Benchmarks
  • A whole lot more

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cache

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

Cache is a hash tree of keys which have been hashed using SipHash. It stores pointers to the values associated with the keys. It supports customisable key Time To Live and scavenge time.

func NewCache

func NewCache(hashKey string) *Cache

NewCache will return a pointer to a newly instantiated Cache. It requires a 128 bit hash key in string form to initialise. If the key is longer or shorter than 128 bits it will be truncated or padded respectively. The cache TTL and scavenge time are set to 10 seconds and 1 second respectively. These values can be changed at any time by calling the SetTTL and SetScavengeTime methods.

func (*Cache) Count

func (c *Cache) Count() int

Count returns the number of keys in the cache.

func (*Cache) Delete

func (c *Cache) Delete(key []byte) bool

Delete will remove an entry from the cache.

func (*Cache) Iterate added in v0.0.2

func (c *Cache) Iterate() chan Row

Iterate returns all keys and values in the cache.

func (*Cache) Read

func (c *Cache) Read(key []byte) ([]byte, bool)

Read will try to read the value of a given key from the cache. It will return the data as []byte and true if the key is found, otherwise it will return false if the key isn't found.

func (*Cache) SetScavengeTime

func (c *Cache) SetScavengeTime(st uint64) error

SetScavengeTime sets the frequency (in milliseconds) that the cache will check for entries that are older than their TTL. It must be greater than 0 milliseconds, and less than or equal to the cache TTL.

func (*Cache) SetTTL

func (c *Cache) SetTTL(ttl uint64) error

SetTTL Sets the Time-To-Live value for cache entries. It must be greater than or equal to the scavenge time for the cache.

func (*Cache) Write

func (c *Cache) Write(r Row)

Write will add the key and value to the cache. It will overwrite the key if it already exists.

type Row added in v0.0.2

type Row struct {
	K []byte
	V []byte
}

Row is a key, value pair representing a row in the cache.

Jump to

Keyboard shortcuts

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