<commit_before>package main

import (
	"github.com/karlseguin/garnish"
	"github.com/karlseguin/garnish/gc"
	"time"
)

func main() {
	config := garnish.Configure().Address("127.0.0.1:8080").Debug()
	config.Hydrate(HydrateLoader)
	config.Stats().FileName("stats.json").Slow(time.Millisecond * 100)
	config.Cache().Grace(time.Minute).PurgeHandler(PurgeHandler)

	config.Upstream("test").Address("http://localhost:3000").KeepAlive(8)
	config.Route("users").Get("/v1/users").Upstream("test").CacheTTL(time.Minute)
	config.Route("ping").Get("/v1/ping").Handler(func(reg *gc.Request, next gc.Middleware) gc.Response {
		return gc.Respond(200, "ok")
	})
	runtime, err := config.Build()
	if err != nil {
		panic(err)
	}
	garnish.Start(runtime)
}

func HydrateLoader(fragment gc.ReferenceFragment) []byte {
	return []byte(`{"id": "hyd-` + fragment.String("id") + `"}`)
}

func PurgeHandler(req *gc.Request, lookup gc.CacheKeyLookup, cache gc.CacheStorage) gc.Response {<commit_msg>expanded example to show saving and restoring cache<commit_after>package main

import (
	"fmt"
	"github.com/karlseguin/garnish"
	"github.com/karlseguin/garnish/gc"
	"os"
	"os/signal"
	"syscall"
	"time"
)

func main() {
	config := garnish.Configure().Address("127.0.0.1:8080").Debug()
	config.Hydrate(HydrateLoader)
	config.Stats().FileName("stats.json").Slow(time.Millisecond * 100)
	config.Cache().Grace(time.Minute).PurgeHandler(PurgeHandler)

	config.Upstream("test").Address("http://localhost:3000").KeepAlive(8)
	config.Route("users").Get("/v1/users").Upstream("test").CacheTTL(time.Minute)
	config.Route("ping").Get("/v1/ping").Handler(func(reg *gc.Request, next gc.Middleware) gc.Response {
		return gc.Respond(200, "ok")
	})
	runtime, err := config.Build()
	if err != nil {
		panic(err)
	}
	if err := runtime.Cache.Load("cache.save"); err != nil {
		fmt.Println("failed to restore cache", err)
	}
	go garnish.Start(runtime)
	s := make(chan os.Signal, 1)
	signal.Notify(s, syscall.SIGQUIT)
	<-s
	if err := runtime.Cache.Save("cache.save", 5000, time.Second*10); err != nil {
		fmt.Println("failed to save cache", err)
	}
}

func HydrateLoader(fragment gc.ReferenceFragment) []byte {
	return []byte(`{"id": "hyd-` + fragment.String("id") + `"}`)
}

func PurgeHandler(req *gc.Request, lookup gc.CacheKeyLookup, cache gc.CacheStorage) gc.Response {
{'fraction_non_alphanumeric': 0.19113215669393027, 'fraction_numerical': 0.022815325010761944, 'mean_word_length': 5.3497267759562845, 'pattern_counts': {'":': 2, '<': 4, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 2, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1823086', 'n_tokens_mistral': 927, 'n_tokens_neox': 863, 'n_words': 137}