Documentation
¶
Overview ¶
Package scache defines cache Cache interface and implementation
Index ¶
Examples ¶
Constants ¶
View Source
const ( //DefaultCacheSizeMb default cache size DefaultCacheSizeMb = 1 //MinShards min shards MinShards = 32 //DefaultShardMapSize default map shard allocation size. DefaultShardMapSize = 32 * 1024 )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Cache ¶
type Cache struct {
OnSegmentSwitch
// contains filtered or unexported fields
}
Cache represents cache service
func New ¶
New creates a Cache
Example ¶
package main
import (
"github.com/viant/scache"
"log"
)
const (
InMemoryExample = iota
MemoryMappedFileExample
InMemoryEntriesExample
)
func main() {
var cacheUsageType int
var cache *scache.Cache
var err error
switch cacheUsageType {
case InMemoryExample:
cache, err = scache.New(&scache.Config{SizeMb: 256})
//or
cache, err = scache.NewMemCache(256, 0, 0)
case MemoryMappedFileExample:
cache, err = scache.New(&scache.Config{SizeMb: 256, Location: "/tmp/data.sch"})
//or
cache, err = scache.NewMmapCache("/tmp/data.sch", 256, 0, 0)
case InMemoryEntriesExample:
cache, err = scache.New(&scache.Config{MaxEntries: 5000000, EntrySize: 128})
}
if err != nil {
log.Fatal(err)
}
err = cache.Set("keyX", []byte("some value"))
if err != nil {
log.Fatal(err)
}
value, err := cache.Get("keyX")
if err != nil {
log.Fatal(err)
}
log.Printf("value : %s\n", value)
err = cache.Delete("keyX")
if err != nil {
log.Fatal(err)
}
cache.Close()
}
func NewMemCache ¶
NewMemCache creates a memory backed cache
func NewMmapCache ¶
NewMmapCache creates a memory mapped filed backed cache
type Config ¶
type Config struct {
MaxEntries int //optional upper entries limit in the cache
EntrySize int //optional entry size to estimate SizeMb (MaxEntries * EntrySize) when specified
KeySize int
SizeMb int //optional max cache size, default 1
Shards uint64 //optional segment shards size, default MAX(32, MaxEntries / 1024*1024)
Location string //optional path to mapped memory file
// contains filtered or unexported fields
}
Config represents cache config
func (*Config) SegmentDataSize ¶
SegmentDataSize returns segments data size (cache always has 2 segments)
type OnSegmentSwitch ¶ added in v0.2.0
OnSegmentSwitch function to call when segment switches primary to secondary role
Click to show internal directories.
Click to hide internal directories.