reference

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 11, 2020 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package reference holds a sample rendered typed cache which is used to facilitate updating the template.

Whenever changes are made to the typed cache implementation, this file makes it easy to do a quick sanity check. The contents of this file are not meant to be used by anyone!

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CacheOptions

type CacheOptions struct {
	Clock            clock.Clock
	ExpireAfterWrite time.Duration
	ExpireAfterRead  time.Duration
	Load             LoadFunc
	MaxSize          int32
	RemovalListeners []RemovalListener
	ShardCount       int
	HashCodeFunc     func(key string) int
}

type LoadFunc

type LoadFunc func(string) (int64, error)

type RemovalListener

type RemovalListener func(RemovalNotification)

type RemovalNotification

type RemovalNotification struct {
	Key    string
	Value  int64
	Reason loadingcache.RemovalReason
}

type TypedCache

type TypedCache interface {
	Get(key string) (int64, error)
	Put(key string, value int64)
	Invalidate(key string, keys ...string)
	InvalidateAll()
}
Example
package main

import (
	"fmt"
	"time"

	"github.com/Hartimer/loadingcache/cmd/typedcache/internal/reference"
)

func main() {
	cache := reference.NewTypedCache(reference.CacheOptions{
		MaxSize:          2,
		ExpireAfterRead:  2 * time.Minute,
		ExpireAfterWrite: time.Minute,
		RemovalListeners: []reference.RemovalListener{
			func(notification reference.RemovalNotification) {
				fmt.Printf("Entry removed due to %s\n", notification.Reason)
			},
		},
		Load: func(key string) (int64, error) {
			fmt.Printf("Loading key %s\n", key)
			return int64(len(key)), nil
		},
	})

	cache.Put("a", 1)
	var val1 int64
	val1, _ = cache.Get("a")
	fmt.Printf("%v\n", val1)

	val2, _ := cache.Get("aa")
	fmt.Printf("%v\n", val2)

	val3, _ := cache.Get("aaa")
	fmt.Printf("%v\n", val3)

}
Output:

1
Loading key aa
2
Loading key aaa
Entry removed due to SIZE
3

func NewTypedCache

func NewTypedCache(options CacheOptions) TypedCache

Jump to

Keyboard shortcuts

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