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 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
Click to show internal directories.
Click to hide internal directories.