Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
1.3 kB
3
Indexable
Never
package main

import (
	"context"
	"log"
	"time"

	"gitlab.com/americastech/paytechinfra/core/pkg/cache"
	"gitlab.com/americastech/paytechinfra/core/pkg/cache/provider/redis"
	"gitlab.com/americastech/paytechinfra/core/pkg/logger"
)

type CacheModel struct {
	Value string
}

func main() {
	ctx := context.Background()

	redis, err := redis.NewProvider(ctx, redis.WithTelemetryDisabled())
	if err != nil {
		logger.Of(ctx).Warn(err.Error())
	}

	cache.SetGlobal(cache.NewCache(redis))

	cacheModel := CacheModel{
		Value: "some-value",
	}

	cache.Instance().Set(ctx, cache.Item{
		Key:       "some-value",
		Value:     cacheModel,
		ExpiresIn: time.Second * 10,
	})

	i, err := cache.Instance().Get(ctx, "some-value")
	if err != nil {
		log.Fatal(err)
	}

	log.Println(i.Value)

	res, err := cache.NewResolver[CacheModel](ctx, cache.NewResolverConfig("some-value"))
	if err != nil {
		log.Fatalln(err)
	}

	log.Println(res.Value)

	options := cache.NewResolverConfig("some-value")

	fn := func(ctx context.Context) (CacheModel, error) {
		// Chamada para o banco de dados
		return CacheModel{}, nil
	}

	res, err = cache.NewResolverFunc(ctx, options, fn)
	if err != nil {
		log.Fatalln(err)
	}
}