hcache

package module
v0.0.0-...-0753658 Latest Latest
Warning

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

Go to latest
Published: Jul 31, 2016 License: MIT Imports: 0 Imported by: 1

README

hcache

Build Status Coverage Status Go Report Card GoDoc MIT License

package main

import (
	"fmt"

	"github.com/s2gatev/hcache"
)

type data struct {
  value int
}

func main() {
  cache := hcache.New()
  cache.Insert(&data{value: 42}, "important", "answer")
  
  // ...
  
  if v, ok := cache.Get("important", "answer"); ok {
    d := v.(*data)
    fmt.Println(d.value) // 42
  }
}

By @s2gatev, @catiepg

Documentation

Overview

Package hcache is a library providing hierarchical value store.

New value stores are created using the New function of the package, each keeping separate hierarchy of values.

cache := New()

A new value is inserted using the Insert function.

cache.Insert(42, "key1", "key2", "key3")

The value could be retrieved using the Get function.

value := cache.Get("key1", "key2", "key3")

A value could be removed from the store using the Erase function.

cache.Erase("key1", "key2", "key3")

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 store that keeps values in a hierarchical fashion.

func New

func New() *Cache

New creates a hierarchical cache object.

func (*Cache) Erase

func (c *Cache) Erase(keys ...Key) Value

Erase removes an entry from the cache following keys and returns it.

If the entry is not present in the cache Erase returns nil.

func (*Cache) Get

func (c *Cache) Get(keys ...Key) (Value, bool)

Get retrieves an entry, if present, from the cache following keys.

func (*Cache) GetOrInsert

func (c *Cache) GetOrInsert(value Value, keys ...Key) Value

GetOrInsert retrieves an entry from the cache following keys.

If the entry is not present it is created with the specified value.

func (*Cache) Has

func (c *Cache) Has(keys ...Key) bool

Has returns true if an entry is present in the cache following keys and false otherwise.

func (*Cache) Insert

func (c *Cache) Insert(value Value, keys ...Key) Value

Insert adds an entry in the cache following keys.

If there's already a value in the specific location it will be replaced and returned as a result. If the value is new the result of Insert will be nil.

type Key

type Key interface{}

Key represents a single key used in the hirearchical cache.

type Value

type Value interface{}

Value represents a value stored in the hirearchical cache.

Jump to

Keyboard shortcuts

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