Skip to content

Commit

Permalink
add caching and transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanfaerman committed Feb 16, 2024
1 parent d41edda commit 993ff09
Show file tree
Hide file tree
Showing 12 changed files with 368 additions and 229 deletions.
10 changes: 9 additions & 1 deletion internal/models/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ type Account struct {
Slug string `form:"slug" json:"slug"`
StreamID string `json:"stream_id"`

Name string `form:"name" validate:"required"`
Name string `form:"name" json:"name" validate:"required"`
About string `form:"about" json:"about"`
Kind AccountKind

Expand Down Expand Up @@ -158,6 +158,14 @@ func (m *Account) PrimaryEmail() (Email, error) {
return Email{}, errors.New("no primary email")
}

func (m *Account) Members(ctx context.Context) []*Membership {
members, err := Find[Membership](ctx, ByMemberOf(m.ID))
if err != nil {
return []*Membership{}
}
return members
}

func (m *Account) Delegated(ctx context.Context) ([]*Membership, error) {
return Find[Membership](ctx, ByAccount(m.ID))
}
Expand Down
8 changes: 5 additions & 3 deletions internal/models/account_finder.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package models
import (
"context"
"encoding/json"
"fmt"
"time"

"github.com/ryanfaerman/netctl/internal/dao"
"github.com/ryanfaerman/netctl/internal/models/finders"
Expand All @@ -13,6 +13,10 @@ func (m Account) FindCacheKey() string {
return "accounts"
}

func (m *Account) FindCacheDuration() time.Duration {
return 10 * time.Minute
}

func (m Account) Find(ctx context.Context, queries finders.QuerySet) (any, error) {
var (
raw dao.Account
Expand Down Expand Up @@ -102,7 +106,6 @@ func (m Account) Find(ctx context.Context, queries finders.QuerySet) (any, error
if qerr == nil {
defer func() {
for i := range found {
fmt.Println("setting distance on", i, distances[i])
found[i].Distance = distances[i]
}
}()
Expand Down Expand Up @@ -147,7 +150,6 @@ func (m Account) Find(ctx context.Context, queries finders.QuerySet) (any, error
}
}

fmt.Println(a.Settings.LocationSettings)
switch {
case queries.HasField("callsigns"):
if _, err := (&a).Callsigns(); err != nil {
Expand Down
58 changes: 51 additions & 7 deletions internal/models/finders/finders.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package finders

import (
"context"
"fmt"
"time"

ttlcache "github.com/jellydator/ttlcache/v3"
Expand All @@ -21,6 +22,21 @@ type FinderCacher interface {
FindCacheKey() string
}

type FinderCacheDurationer interface {
FinderCacher
FindCacheDuration() time.Duration
}

type FinderCacheCapacitor interface {
FinderCacher
FindCacheCapacity() uint64
}

type FinderCacherDurationerCapacitor interface {
FinderCacheDurationer
FinderCacheCapacitor
}

// Find the all instances of a Finder type, given a set of queries.
func Find[K Finder](ctx context.Context, queries ...QueryFunc) ([]*K, error) {
k := *new(K)
Expand Down Expand Up @@ -61,15 +77,43 @@ func FindOne[K Finder](ctx context.Context, queries ...QueryFunc) (*K, error) {

var caches = make(map[string]*ttlcache.Cache[string, any])

func FindCached[K FinderCacher](ctx context.Context, ttl time.Duration, queries ...QueryFunc) ([]*K, error) {
const (
DefaultCacheTTL = 5 * time.Minute
DefaultCacheCapacity = 1000
)

func FindCached[K FinderCacher](ctx context.Context, queries ...QueryFunc) ([]*K, error) {
k := *new(K)
cacheKey := k.FindCacheKey()

cache, ok := caches[cacheKey]
if !ok {
cache = ttlcache.New[string, any](
ttlcache.WithTTL[string, any](ttl),
)
var maybe any
maybe = k
switch T := maybe.(type) {
case FinderCacherDurationerCapacitor:
fmt.Println("CacheDurationerCapacitor")
cache = ttlcache.New[string, any](
ttlcache.WithTTL[string, any](T.FindCacheDuration()),
ttlcache.WithCapacity[string, any](T.FindCacheCapacity()),
)
case FinderCacheCapacitor:
fmt.Println("CacheCapacitor")
cache = ttlcache.New[string, any](
ttlcache.WithTTL[string, any](DefaultCacheTTL),
ttlcache.WithCapacity[string, any](T.FindCacheCapacity()),
)
case FinderCacheDurationer:
fmt.Println("CacheDurationer")
cache = ttlcache.New[string, any](
ttlcache.WithTTL[string, any](T.FindCacheDuration()),
)
default:
fmt.Println("Default")
cache = ttlcache.New[string, any](
ttlcache.WithTTL[string, any](DefaultCacheTTL),
)
}
go cache.Start()
caches[cacheKey] = cache
}
Expand All @@ -94,11 +138,11 @@ func FindCached[K FinderCacher](ctx context.Context, ttl time.Duration, queries

cache.Set(qsKey, results, ttlcache.DefaultTTL)

return nil, nil
return results, nil
}

func FindOneCached[K FinderCacher](ctx context.Context, ttl time.Duration, queries ...QueryFunc) (*K, error) {
results, err := FindCached[K](ctx, ttl, queries...)
func FindOneCached[K FinderCacher](ctx context.Context, queries ...QueryFunc) (*K, error) {
results, err := FindCached[K](ctx, queries...)
if err != nil {
return nil, err
}
Expand Down
9 changes: 7 additions & 2 deletions internal/models/membership.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,13 @@ type Membership struct {
ID int64
}

func (m *Membership) Account(ctx context.Context) (*Account, error) {
return finders.FindOne[Account](ctx, finders.ByID(m.AccountID))
func (m *Membership) Account(ctx context.Context) *Account {
account, err := finders.FindOneCached[Account](ctx, finders.ByID(m.AccountID))
if err != nil {
fmt.Println("error finding account", "error", err)
return nil
}
return account
}

func (m *Membership) Target(ctx context.Context) *Account {
Expand Down
11 changes: 11 additions & 0 deletions internal/models/membership_finder.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ import (
"github.com/ryanfaerman/netctl/internal/models/finders"
)

func (m Membership) FindCacheKey() string {
return "memberships"
}

// Find[Membership](ctx, ByAccount(1), ByMember(of), WithPermission(PermissionEdit))
func (m Membership) Find(ctx context.Context, queries finders.QuerySet) (any, error) {
var (
Expand Down Expand Up @@ -60,6 +64,13 @@ func (m Membership) Find(ctx context.Context, queries finders.QuerySet) (any, er
}
raws, err = global.dao.GetMembershipsForAccount(ctx, accountID)

case queries.HasWhere("member_of"):
memberOf, ok := finders.EnforceValue[int64](queries, "member_of")
if ok != nil {
return nil, ok
}
raws, err = global.dao.GetMembersOfAccount(ctx, memberOf)

}

if err != nil {
Expand Down
4 changes: 4 additions & 0 deletions internal/models/role_finder.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ import (
"github.com/ryanfaerman/netctl/internal/models/finders"
)

func (m Role) FindCacheKey() string {
return "roles"
}

func (m Role) Find(ctx context.Context, queries finders.QuerySet) (any, error) {
var (
raw dao.Role
Expand Down
3 changes: 2 additions & 1 deletion internal/models/settings_profile.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package models

type ProfileSettings struct {
Name string `form:"name" validate:"required"`
Name string `form:"name" json:"name" validate:"required"`
About string `form:"about" json:"about"`
Slug string `form:"slug" json:"slug"`
}
Loading

0 comments on commit 993ff09

Please sign in to comment.