func (r *FlagRepository) Update(ctx context.Context, id string, input flaggio.UpdateFlag) error {
	span, ctx := opentracing.StartSpanFromContext(ctx, "RedisFlagRepository.Update")
	defer span.Finish()
	if err := r.store.Update(ctx, id, input); err != nil {
		return err
	}
	var flagKey string
	if input.Key != nil {
		flagKey = *input.Key
	} else {
		f, err := r.FindByID(ctx, id)
		if err != nil {
			return err
		}
		flagKey = f.Key
	}
	return r.invalidateRelevantCacheKeys(ctx, id, flagKey)
}
Update updates a flag.
package redis
import (
	"context"
	"errors"
	"time"
	"github.com/go-redis/redis/v7"
	"github.com/opentracing/opentracing-go"
	"github.com/victorkt/flaggio/internal/flaggio"
	"github.com/victorkt/flaggio/internal/repository"
	"github.com/vmihailenco/msgpack/v4"
)
var _ repository.Flag = (*FlagRepository)(nil)
type FlagRepository struct {
	redis *redis.Client
	store repository.Flag
	ttl   time.Duration
}
func (r *FlagRepository) FindAll(ctx context.Context, search *string, offset, limit *int64) (*flaggio.FlagResults, error) {
	span, ctx := opentracing.StartSpanFromContext(ctx, "RedisFlagRepository.FindAll")
	defer span.Finish()
	shouldCache := shouldCacheFindAll(search, offset, limit)
	cacheKey := flaggio.FlagCacheKey("*")
	if shouldCache {
		cached, err := r.redis.WithContext(ctx).Get(cacheKey).Result()
		if err != nil && !errors.Is(err, redis.Nil) {
			return nil, err
		}
		if cached != "" {
			var fr flaggio.FlagResults
			if err := msgpack.Unmarshal([]byte(cached), &fr); err == nil {
				return &fr, nil
			}
		}
	}
	res, err := r.store.FindAll(ctx, search, offset, limit)
	if err != nil {
		return nil, err
	}
	if shouldCache {
		b, err := msgpack.Marshal(res)
		if err != nil {
			return nil, err
		}
		if err := r.redis.Set(cacheKey, b, r.ttl).Err(); err != nil {
			return nil, err
		}
	}
	return res, nil
}
func (r *FlagRepository) FindByID(ctx context.Context, id string) (*flaggio.Flag, error) {
	span, ctx := opentracing.StartSpanFromContext(ctx, "RedisFlagRepository.FindByID")
	defer span.Finish()
	cacheKey := flaggio.FlagCacheKey(id)
	cached, err := r.redis.WithContext(ctx).Get(cacheKey).Result()
	if err != nil && !errors.Is(err, redis.Nil) {
		return nil, err
	}
	if cached != "" {
		var f flaggio.Flag
		if err := msgpack.Unmarshal([]byte(cached), &f); err == nil {
			return &f, nil
		}
	}
	res, err := r.store.FindByID(ctx, id)
	if err != nil {
		return nil, err
	}
	b, err := msgpack.Marshal(res)
	if err != nil {
		return nil, err
	}
	if err := r.redis.WithContext(ctx).Set(cacheKey, b, r.ttl).Err(); err != nil {
		return nil, err
	}
	return res, nil
}
func (r *FlagRepository) FindByKey(ctx context.Context, key string) (*flaggio.Flag, error) {
	span, ctx := opentracing.StartSpanFromContext(ctx, "RedisFlagRepository.FindByKey")
	defer span.Finish()
	cacheKey := flaggio.FlagCacheKey("key", key)
	cached, err := r.redis.WithContext(ctx).Get(cacheKey).Result()
	if err != nil && !errors.Is(err, redis.Nil) {
		return nil, err
	}
	if cached != "" {
		var f flaggio.Flag
		if err := msgpack.Unmarshal([]byte(cached), &f); err == nil {
			return &f, nil
		}
	}
	res, err := r.store.FindByKey(ctx, key)
	if err != nil {
		return nil, err
	}
	b, err := msgpack.Marshal(res)
	if err != nil {
		return nil, err
	}
	if err := r.redis.WithContext(ctx).Set(cacheKey, b, r.ttl).Err(); err != nil {
		return nil, err
	}
	return res, nil
}
func (r *FlagRepository) Create(ctx context.Context, input flaggio.NewFlag) (string, error) {
	span, ctx := opentracing.StartSpanFromContext(ctx, "RedisFlagRepository.Create")
	defer span.Finish()
	id, err := r.store.Create(ctx, input)
	if err != nil {
		return "", err
	}
	return id, r.invalidateRelevantCacheKeys(ctx, id, input.Key)
}