Skip to content

Commit

Permalink
Use strings as keys in cache, not objects
Browse files Browse the repository at this point in the history
  • Loading branch information
ericvolp12 committed Sep 23, 2023
1 parent da97bef commit fc845bf
Showing 1 changed file with 13 additions and 13 deletions.
26 changes: 13 additions & 13 deletions atproto/identity/cache_directory.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import (
type CacheDirectory struct {
Inner Directory
ErrTTL time.Duration
handleCache *expirable.LRU[syntax.Handle, HandleEntry]
identityCache *expirable.LRU[syntax.DID, IdentityEntry]
handleCache *expirable.LRU[string, HandleEntry]
identityCache *expirable.LRU[string, IdentityEntry]
}

type HandleEntry struct {
Expand All @@ -36,8 +36,8 @@ func NewCacheDirectory(inner Directory, capacity int, hitTTL, errTTL time.Durati
return CacheDirectory{
ErrTTL: errTTL,
Inner: inner,
handleCache: expirable.NewLRU[syntax.Handle, HandleEntry](capacity, nil, hitTTL),
identityCache: expirable.NewLRU[syntax.DID, IdentityEntry](capacity, nil, hitTTL),
handleCache: expirable.NewLRU[string, HandleEntry](capacity, nil, hitTTL),
identityCache: expirable.NewLRU[string, IdentityEntry](capacity, nil, hitTTL),
}
}

Expand All @@ -63,7 +63,7 @@ func (d *CacheDirectory) updateHandle(ctx context.Context, h syntax.Handle) (*Ha
DID: "",
Err: err,
}
d.handleCache.Add(h, he)
d.handleCache.Add(h.String(), he)
return &he, nil
}

Expand All @@ -78,15 +78,15 @@ func (d *CacheDirectory) updateHandle(ctx context.Context, h syntax.Handle) (*Ha
Err: nil,
}

d.identityCache.Add(ident.DID, entry)
d.handleCache.Add(ident.Handle, he)
d.identityCache.Add(ident.DID.String(), entry)
d.handleCache.Add(ident.Handle.String(), he)
return &he, nil
}

func (d *CacheDirectory) ResolveHandle(ctx context.Context, h syntax.Handle) (syntax.DID, error) {
var err error
var entry *HandleEntry
maybeEntry, ok := d.handleCache.Get(h)
maybeEntry, ok := d.handleCache.Get(h.String())

if !ok {
entry, err = d.updateHandle(ctx, h)
Expand Down Expand Up @@ -123,17 +123,17 @@ func (d *CacheDirectory) updateDID(ctx context.Context, did syntax.DID) (*Identi
}
}

d.identityCache.Add(did, entry)
d.identityCache.Add(did.String(), entry)
if he != nil {
d.handleCache.Add(ident.Handle, *he)
d.handleCache.Add(ident.Handle.String(), *he)
}
return &entry, nil
}

func (d *CacheDirectory) LookupDID(ctx context.Context, did syntax.DID) (*Identity, error) {
var err error
var entry *IdentityEntry
maybeEntry, ok := d.identityCache.Get(did)
maybeEntry, ok := d.identityCache.Get(did.String())

if !ok {
entry, err = d.updateDID(ctx, did)
Expand Down Expand Up @@ -187,12 +187,12 @@ func (d *CacheDirectory) Lookup(ctx context.Context, a syntax.AtIdentifier) (*Id
func (d *CacheDirectory) Purge(ctx context.Context, a syntax.AtIdentifier) error {
handle, err := a.AsHandle()
if nil == err { // if not an error, is a handle
d.handleCache.Remove(handle)
d.handleCache.Remove(handle.String())
return nil
}
did, err := a.AsDID()
if nil == err { // if not an error, is a DID
d.identityCache.Remove(did)
d.identityCache.Remove(did.String())
return nil
}
return fmt.Errorf("at-identifier neither a Handle nor a DID")
Expand Down

0 comments on commit fc845bf

Please sign in to comment.