Exemple #1
0
func (c *Cache) Build(runtime *garnish.Runtime) error {
	runtime.Cache = garnish.NewCache()
	runtime.Cache.Saint = c.saint
	runtime.Cache.GraceTTL = c.grace
	runtime.Cache.Storage = cache.New(c.maxSize)

	if c.purgeHandler != nil {
		runtime.Cache.PurgeHandler = c.purgeHandler
		runtime.Router.AddNamed("_gc_purge_all", "PURGE", "/*", nil)
		runtime.Routes["_gc_purge_all"] = &garnish.Route{
			Stats: garnish.NewRouteStats(time.Millisecond * 500),
			Cache: garnish.NewRouteCache(-1, c.lookup),
		}
	}

	for _, route := range runtime.Routes {
		if route.Cache != nil && route.Cache.KeyLookup == nil {
			route.Cache.KeyLookup = c.lookup
		}
	}
	return nil
}
Exemple #2
0
func helper() *RuntimeHelper {
	garnish.Log = garnish.NewFakeLogger()
	r := router.New(router.Configure())
	r.AddNamed("cache", "GET", "/cache", nil)
	r.AddNamed("cache", "PURGE", "/cache", nil)
	r.AddNamed("hcache", "GET", "/hcache", nil)
	r.AddNamed("nocache", "GET", "/nocache", nil)
	r.AddNamed("control", "GET", "/control", nil)
	r.AddNamed("dispatch", "GET", "/dispatch", nil)

	hydr := &middlewares.Hydrate{Header: "X-Hydrate"}

	e := garnish.WrapMiddleware("upst", middlewares.Upstream, nil)
	e = garnish.WrapMiddleware("dspt", middlewares.Dispatch, e)
	e = garnish.WrapMiddleware("hydr", hydr.Handle, e)
	e = garnish.WrapMiddleware("cach", middlewares.Cache, e)
	e = garnish.WrapMiddleware("stat", middlewares.Stats, e)
	runtime := &garnish.Runtime{
		Router:           r,
		Executor:         e,
		FatalResponse:    garnish.Empty(500),
		NotFoundResponse: garnish.Empty(404),
		Routes: map[string]*garnish.Route{
			"cache": &garnish.Route{
				Stats: garnish.NewRouteStats(time.Millisecond * 100),
				Cache: garnish.NewRouteCache(time.Minute, garnish.DefaultCacheKeyLookup),
			},
			"hcache": &garnish.Route{
				Stats: garnish.NewRouteStats(time.Millisecond * 100),
				Cache: garnish.NewRouteCache(time.Minute, garnish.DefaultCacheKeyLookup),
			},
			"noauth": &garnish.Route{
				Stats: garnish.NewRouteStats(time.Millisecond * 100),
				Cache: garnish.NewRouteCache(time.Duration(-1), nil),
			},
			"control": &garnish.Route{
				Stats: garnish.NewRouteStats(time.Millisecond * 100),
				Cache: garnish.NewRouteCache(time.Duration(-1), nil),
			},
			"nocache": &garnish.Route{
				Stats: garnish.NewRouteStats(time.Millisecond * 100),
				Cache: garnish.NewRouteCache(time.Duration(-1), nil),
			},
			"dispatch": &garnish.Route{
				Stats: garnish.NewRouteStats(time.Millisecond * 100),
				Cache: garnish.NewRouteCache(time.Duration(-1), nil),
				FlowHandler: func(req *garnish.Request, next garnish.Handler) garnish.Response {
					res := next(req)
					res.Header().Set("Dispatch", "out")
					return res
				},
			},
		},
	}

	runtime.Cache = garnish.NewCache()
	runtime.Cache.Storage = cache.New(10)
	runtime.Cache.PurgeHandler = func(req *garnish.Request, lookup garnish.CacheKeyLookup, cache garnish.CacheStorage) garnish.Response {
		if cache.Delete(lookup(req)) == false {
			return garnish.PurgeMissResponse
		}
		return garnish.PurgeHitResponse
	}

	runtime.HydrateLoader = func(reference garnish.ReferenceFragment) []byte {
		return []byte(fmt.Sprintf(`{"i": %q, "p": %q}`, reference.String("id"), reference.String("type")))
	}

	return &RuntimeHelper{
		runtime: runtime,
	}
}