package mvcc

import (
	"context"
)

type cacheContextKey struct{}

// ContextWithCache returns a new context with the Cache stored in it. A nil Cache
// is not added into the context, and the passed context is returned as is.
func ContextWithCache(ctx context.Context, c Cache) context.Context {
	return context.WithValue(ctx, cacheContextKey{}, c)
}

// CacheFromContext returns the Cache in the context, or nil if not found.
func CacheFromContext(ctx context.Context) Cache {
	if c, ok := ctx.Value(cacheContextKey{}).(Cache); ok {
		return c
	}
	return nil
}
