Skip to content

Latest commit

 

History

28 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

cache

Test

Usage

Single

package main

import (
  "fmt"
  "github.com/taybart/cache"
)

func main() {
  c := cache.New(cache.Default())
  defer c.Finish()

  // set item
  c.Set("test", "test")
  c.SetWithTTL("user", "me", cache.TTLNeverExpire)

  var item string
  err := c.Get("test", &item)
  fmt.Println(item) // output: test
}

Pub/Sub

package main

import (
  "fmt"
  "github.com/taybart/cache"
)

func main() {
    c := cache.New(cache.Default())
    defer c.Finish()

    var wg sync.WaitGroup
    wg.Add(1)

    ch := c.Subscribe("general") // subscribe to key updates
    go func(ch chan any) {
        defer wg.Done()
        item := <-ch
        fmt.Println(item.(string)) // hello
    }(ch)

    c.Set("general", "hello")

    wg.Wait()
    return nil
}

Shared

package main

import (
  "fmt"
  "github.com/taybart/cache"
)

func main() {
  // Will access the same cache across calls
  c := cache.NewShared(cache.Default())
  defer c.Finish()

  // set item
  c.Set("test", "test")

  var item string
  err := c.Get("test", &item)
  fmt.Println(item) // output: test
}

Config

Pruning

Defaults: TTL: 24 hours PruneRate: 5 Minutes SleepDuration: 100 Milliseconds

package main

import (
  "fmt"
  "github.com/taybart/cache"
)

func main() {
  c := cache.New(cache.Config{
    TTL: time.Nanosecond, // we really don't care about data here
    PruneRate: 3*time.Nanosecond, // extend the prune rate to some stuff might live
    SleepDuration: time.Hour, // wait an hour between checks
  })
  defer c.Finish()

  c.SetPruneRate(0) // actually, don't prune at all
}

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages