memory

package
v0.0.0-...-360b30e Latest Latest
Warning

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

Go to latest
Published: Jun 3, 2018 License: MIT Imports: 2 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

func New() storage.Storage

New creates a new Storage that lives in memory.

This type of storage is intended for testing and demonstration purposes only. Although the implementation is safe for concurrent use, it is not persisted.

Example
mem := New()

mem.LoadOrStore("foo", "bar")
key, _ := mem.Resolve("bar")

fmt.Printf("Key for 'bar' is '%s'\n", key)
Output:

Key for 'bar' is 'foo'
Example (Other)
mem := New()

mem.LoadOrStore("foo", "bar")

value, loaded := mem.LoadOrStore("foo", "42")
key, _ := mem.Resolve("bar")

fmt.Printf("Value of 'foo' is '%s'\n", value)
fmt.Printf("Value of 'foo' was loaded from storage: %t\n", loaded)
fmt.Printf("Key for 'bar' is '%s'\n", key)

_, ok := mem.Resolve("42")

fmt.Printf("Key for '42' could be resolved: %t", ok)
Output:

Value of 'foo' is 'bar'
Value of 'foo' was loaded from storage: true
Key for 'bar' is 'foo'
Key for '42' could be resolved: false
Example (With_metrics)
loadOps := expvar.NewInt("loadOps")
storeOps := expvar.NewInt("storeOps")
resolveOps := expvar.NewInt("resolveOps")

memory := New()
memory.WithMetrics(&storage.Metrics{LoadOps: loadOps, StoreOps: storeOps, ResolveOps: resolveOps})

memory.LoadOrStore("foo", "bar")
fmt.Printf("Load: %d, Store: %d, Resolve: %d\n", loadOps.Value(), storeOps.Value(), resolveOps.Value())

memory.LoadOrStore("foo", "bar")
fmt.Printf("Load: %d, Store: %d, Resolve: %d\n", loadOps.Value(), storeOps.Value(), resolveOps.Value())

memory.Resolve("bar")
fmt.Printf("Load: %d, Store: %d, Resolve: %d\n", loadOps.Value(), storeOps.Value(), resolveOps.Value())
Output:

Load: 0, Store: 1, Resolve: 0
Load: 1, Store: 1, Resolve: 0
Load: 1, Store: 1, Resolve: 1

Types

This section is empty.

Jump to

Keyboard shortcuts

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