Skip to content

Repository files navigation

GitHub go.mod Go version Go Reference Go Report Card CI Pipeline

Cache

Cache is a simple dependency free cache package with the ability to add new storage methods by implementing a new store.

This cache will run as many stores as needed and can run more than one instance at a time. You can access and maintain the stores individually per cache instance. Each cache will run a trimming function which calls the trim method implemented by each store to remove old items. The cache will also shut down gracefully. You can shut down all cache instances or a single instance.

This package supports the last 2 stable go versions.

Install

$ go get github.com/tmstorm/cache

Stores

  • Mem (In-memory)
  • disk (On disk file store)

Implementing

Select and initiate each store you want to run in the cache.

Note

The Options for each store will vary depending on the store.

import "github.com/tmstorm/stores/mem"

func main() {
  ...
  store := mem.New(&mem.Store{
    // Every store should implement a MaxAge to be used for trimming old cache items.
    MaxAge: 1800,
  })
  ...
}

Note

MaxAge is measured in seconds so a value of 1800 would be 30 minutes

Initialize a new cache.

import (
  "github.com/tmstorm/cache"
)
func main() {
  ...
  c := cache.New(&cache.Options{
    TrimTime: 900,
    Stores: cache.MakeStores(store),
  })
  ...
}

Start the cache.

func main() {
  ...
  err := c.Start()
  if err != nil {
    log.Panicf("Cache instance could not be started: %v", err)
  }
  ...
}

Cache Options

Option Default Description
TrimTime 1800s (30m) Used by the internal trim method to decide when the trim method for each store should be ran.
Stores nil Used be the current cache to access the store or stores used to save items

Accessing Stores

Once you have the cache started you can access the store. You will need to call the store to get the store's writer.

func main() {
  ...
  m := mem.Get(store)
  ...
}

Now you can access the store's writer methods.

func main() {
  ...
  key := "foo"
  value := "bar"
  overwrite := false

  // Get in-memory store
  m := mem.Get(store)

  // Write key-value pair
  err := m.Write(key, []byte(value), overwrite)
  if err != nil {
    fmt.Println(err)
  }

  // Read value with given key
  v, err := m.Read(key)
  if err != nil {
    fmt.Println(err)
  }

  // Delete key-value pair
  m.Remove(key)
  ...
}

Direct Store Maintenance

If you would like to trim or purge a store directly you can do so by calling their methods directly.

func main() {
  ...
  // Runs the stores trim method for removing old items
  m.Trim()

  // Runs the stores purge method to remove all items
  m.Purge()
  ...
}

Stopping

When you want to stop the cache there are two ways to do this. You can either stop all instances or a single cache instance.

To stop a single cache instance.

func main() {
  ...
  err := cache.StopCacheInstance(c.CacheNum)
  if err != nil {
    log.Println(err)
  }
  ...
}

To stop all running cache instances.

func main() {
  ...
  err := cache.StopAll()
  if err != nil {
    log.Println(err)
  }
  ...
}

About

Zero dependency cache with the ability to add additional stores for alternate caching methods.

Resources

Contributing

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages