Skip to content

Commit

Permalink
improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
steebchen committed Mar 17, 2024
1 parent 14927e5 commit 6946884
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 10 deletions.
11 changes: 9 additions & 2 deletions internal/config/loader/loader.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/hatchet-dev/hatchet/internal/integrations/vcs/github"
"github.com/hatchet-dev/hatchet/internal/logger"
"github.com/hatchet-dev/hatchet/internal/msgqueue/rabbitmq"
"github.com/hatchet-dev/hatchet/internal/repository/cache"
"github.com/hatchet-dev/hatchet/internal/repository/prisma"
"github.com/hatchet-dev/hatchet/internal/repository/prisma/db"
"github.com/hatchet-dev/hatchet/internal/services/ingestor"
Expand Down Expand Up @@ -149,9 +150,15 @@ func GetDatabaseConfigFromConfigFile(cf *database.ConfigFile) (res *database.Con
return nil, fmt.Errorf("could not connect to database: %w", err)
}

ch := cache.New(cf.CacheDuration)

return &database.Config{
Disconnect: c.Prisma.Disconnect,
Repository: prisma.NewPrismaRepository(c, pool, prisma.WithLogger(&l), prisma.WithCacheDuration(cf.CacheDuration)),
Disconnect: func() error {
ch.Purge()

return c.Prisma.Disconnect()
},
Repository: prisma.NewPrismaRepository(c, pool, prisma.WithLogger(&l), prisma.WithCache(ch)),
Seed: cf.Seed,
}, nil
}
Expand Down
6 changes: 6 additions & 0 deletions internal/repository/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ type Cacheable interface {

// Get gets a value from the cache with the given key
Get(key string) (interface{}, bool)

Purge()
}

type Cache struct {
Expand All @@ -26,6 +28,10 @@ func (c *Cache) Get(key string) (interface{}, bool) {
return c.cache.Get(key)
}

func (c *Cache) Purge() {
c.cache.Purge()
}

func New(duration time.Duration) *Cache {
if duration == 0 {
// consider a duration of 0 a very short expiry instead of no expiry
Expand Down
18 changes: 10 additions & 8 deletions internal/repository/prisma/repository.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ type prismaRepository struct {
type PrismaRepositoryOpt func(*PrismaRepositoryOpts)

type PrismaRepositoryOpts struct {
v validator.Validator
l *zerolog.Logger
cacheDuration time.Duration
v validator.Validator
l *zerolog.Logger
cache cache.Cacheable
}

func defaultPrismaRepositoryOpts() *PrismaRepositoryOpts {
Expand All @@ -60,9 +60,9 @@ func WithLogger(l *zerolog.Logger) PrismaRepositoryOpt {
}
}

func WithCacheDuration(duration time.Duration) PrismaRepositoryOpt {
func WithCache(cache cache.Cacheable) PrismaRepositoryOpt {
return func(opts *PrismaRepositoryOpts) {
opts.cacheDuration = duration
opts.cache = cache
}
}

Expand All @@ -76,13 +76,15 @@ func NewPrismaRepository(client *db.PrismaClient, pool *pgxpool.Pool, fs ...Pris
newLogger := opts.l.With().Str("service", "database").Logger()
opts.l = &newLogger

c := cache.New(opts.cacheDuration)
if opts.cache == nil {
opts.cache = cache.New(1 * time.Millisecond)
}

return &prismaRepository{
apiToken: NewAPITokenRepository(client, opts.v, c),
apiToken: NewAPITokenRepository(client, opts.v, opts.cache),
event: NewEventRepository(client, pool, opts.v, opts.l),
log: NewLogRepository(client, pool, opts.v, opts.l),
tenant: NewTenantRepository(client, opts.v, c),
tenant: NewTenantRepository(client, opts.v, opts.cache),
tenantInvite: NewTenantInviteRepository(client, opts.v),
workflow: NewWorkflowRepository(client, pool, opts.v, opts.l),
workflowRun: NewWorkflowRunRepository(client, pool, opts.v, opts.l),
Expand Down

0 comments on commit 6946884

Please sign in to comment.