fabric-sdk-go: github.com/hyperledger/fabric-sdk-go/pkg/util/concurrent/lazycache Index | Examples | Files

package lazycache

import "github.com/hyperledger/fabric-sdk-go/pkg/util/concurrent/lazycache"

Index

Examples

Package Files

keys.go lazycache.go refoptcheck.go

type Cache Uses

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

Cache implements a lazy initializing cache. A cache entry is created the first time a value is accessed (via Get or MustGet) by invoking the provided Initializer. If the Initializer returns an error then the entry will not be added.

func New Uses

func New(name string, initializer EntryInitializer, opts ...options.Opt) *Cache

New creates a new lazy cache. - name is the name of the cache and is only used for debugging purpose - initializer is invoked the first time an entry is being cached - opts are options for the cache. If any lazyref option is passed then a lazy reference

is created for each of the cache entries to hold the actual value. This makes it possible
to have expiring values and values that proactively refresh.

func NewWithData Uses

func NewWithData(name string, initializer EntryInitializerWithData, opts ...options.Opt) *Cache

NewWithData creates a new lazy cache. The provided initializer accepts optional data that is passed in from Get(). - name is the name of the cache and is only used for debugging purpose - initializer is invoked the first time an entry is being cached - opts are options for the cache. If any lazyref option is passed then a lazy reference

is created for each of the cache entries to hold the actual value. This makes it possible
to have expiring values and values that proactively refresh.

func (*Cache) Close Uses

func (c *Cache) Close()

Close does the following: - calls Close on all values that implement a Close() function - deletes all entries from the cache - prevents further calls to the cache

func (*Cache) Delete Uses

func (c *Cache) Delete(key Key)

Delete does the following: - calls Close on all values that implement a Close() function - deletes key from the cache

func (*Cache) DeleteAll Uses

func (c *Cache) DeleteAll()

DeleteAll does the following: - calls Close on all values that implement a Close() function - deletes all entries from the cache

func (*Cache) Get Uses

func (c *Cache) Get(key Key, data ...interface{}) (interface{}, error)

Get returns the value for the given key. If the key doesn't exist then the initializer is invoked to create the value, and the key is inserted. If the initializer returns an error then the key is removed from the cache.

Code:

cache := New("Example_Cache", func(key Key) (interface{}, error) {
    if key.String() == "error" {
        return nil, fmt.Errorf("some error")
    }
    return fmt.Sprintf("Value_for_key_%s", key), nil
})
defer cache.Close()

value, err := cache.Get(NewStringKey("Key1"))
if err != nil {
    fmt.Printf("Error returned: %s\n", err)
}
fmt.Println(value)

value, err = cache.Get(NewStringKey("error"))
if err != nil {
    fmt.Printf("Error returned: %s\n", err)
}
fmt.Println(value)

Code:

cache := New("Example_Expiring_Cache",
    func(key Key) (interface{}, error) {
        if key.String() == "error" {
            return nil, fmt.Errorf("some error")
        }
        return fmt.Sprintf("Value_for_key_%s", key), nil
    },
    lazyref.WithAbsoluteExpiration(time.Second),
    lazyref.WithFinalizer(func(expiredValue interface{}) {
        fmt.Printf("Expired value: %s\n", expiredValue)
    }),
)
defer cache.Close()

value, err := cache.Get(NewStringKey("Key1"))
if err != nil {
    fmt.Printf("Error returned: %s\n", err)
} else {
    fmt.Print(value)
}

_, err = cache.Get(NewStringKey("error"))
if err != nil {
    fmt.Printf("Error returned: %s\n", err)
}

Code:

cache := NewWithData("Example_Expiring_Cache",
    func(key Key, data interface{}) (interface{}, error) {
        return fmt.Sprintf("Value_for_%s_%d", key, data.(int)), nil
    },
    lazyref.WithAbsoluteExpiration(20*time.Millisecond),
)
defer cache.Close()

for i := 0; i < 5; i++ {
    value, err := cache.Get(NewStringKey("Key"), i)
    if err != nil {
        fmt.Printf("Error returned: %s", err)
    } else {
        fmt.Print(value)
    }
    time.Sleep(15 * time.Millisecond)
}

func (*Cache) IsClosed Uses

func (c *Cache) IsClosed() bool

IsClosed reeturns true if the cache has been closed

func (*Cache) MustGet Uses

func (c *Cache) MustGet(key Key) interface{}

MustGet returns the value for the given key. If the key doesn't exist then the initializer is invoked to create the value and the key is inserted. If an error is returned during initialization of the value then this function will panic.

Code:

cache := New("Example_Cache", func(key Key) (interface{}, error) {
    return fmt.Sprintf("Value_for_key_%s", key), nil
})
defer cache.Close()

key := NewStringKey("Key1")

fmt.Println(cache.MustGet(key))

Output:

Value_for_key_Key1

func (*Cache) Name Uses

func (c *Cache) Name() string

Name returns the name of the cache (useful for debugging)

type EntryInitializer Uses

type EntryInitializer func(key Key) (interface{}, error)

EntryInitializer creates a cache value for the given key

type EntryInitializerWithData Uses

type EntryInitializerWithData func(key Key, data interface{}) (interface{}, error)

EntryInitializerWithData creates a cache value for the given key and the additional data passed in from Get(). With expiring cache entries, the initializer is called with the same key, but the latest data is passed from the Get() call that triggered the data to be cached/re-cached.

type Key Uses

type Key interface {
    String() string
}

Key holds the string key for the cache entry

type StringKey Uses

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

StringKey is a simple string cache key

func NewStringKey Uses

func NewStringKey(key string) *StringKey

NewStringKey returns a new StringKey

func (*StringKey) String Uses

func (k *StringKey) String() string

String returns the key as a string

Package lazycache imports 9 packages (graph) and is imported by 8 packages. Updated 2019-01-10. Refresh now. Tools for package owners.