-
-
Notifications
You must be signed in to change notification settings - Fork 9
/
cache_zones.go
28 lines (22 loc) · 893 Bytes
/
cache_zones.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
package contexts
import (
"github.com/ironsmile/nedomi/types"
"golang.org/x/net/context"
)
// The key type is unexported to prevent collisions with context keys defined in
// other packages.
type cacheZoneContextKey int
const sKey cacheZoneContextKey = 0
// NewCacheZonesContext returns a new Context carrying the map with
// the supplied CacheZones.
func NewCacheZonesContext(ctx context.Context,
cacheZones map[string]*types.CacheZone) context.Context {
return context.WithValue(ctx, sKey, cacheZones)
}
// GetCacheZones extracts the map of types.CacheZone objects, if present.
func GetCacheZones(ctx context.Context) (map[string]*types.CacheZone, bool) {
// ctx.Value returns nil if ctx has no value for the key;
// the map[string]types.CacheZone type assertion returns ok=false for nil.
cacheZones, ok := ctx.Value(sKey).(map[string]*types.CacheZone)
return cacheZones, ok
}