Lasa is a stateless proxy that generates a RSS or an Atom feed from a Standard.site publication. lasa.anhgelus.world
rss atom atprotocol standard-site atproto
2
fork

Configure Feed

Select the types of activity you want to include in your feed.

feat(cache): set expire time

+29 -18
+29 -18
directory.go
··· 3 3 import ( 4 4 "context" 5 5 "encoding/json" 6 + "fmt" 6 7 "log/slog" 7 8 "time" 8 9 ··· 58 59 slog.Warn("cannot marshal DIDDocument", "document", doc, "error", err) 59 60 return 60 61 } 61 - err = d.cache.Do(ctx, d.cache.B().Set().Key(key).Value(string(b)).Build()).Error() 62 + cache := d.cache 63 + err = cache.Do(ctx, cache.B().Set().Key(key).Value(string(b)).Build()).Error() 62 64 if err != nil { 63 65 slog.Warn("cannot set DIDDocument in cache", "document", doc, "error", err) 66 + return 67 + } 68 + slog.Debug("DIDDocument set in cache") 69 + err = cache.Do( 70 + ctx, 71 + cache.B(). 72 + Expireat(). 73 + Key(key). 74 + Timestamp(time.Now().Add(d.duration).Unix()). 75 + Build(), 76 + ).Error() 77 + if err != nil { 78 + slog.Warn("cannot set DIDDocument expire at", "document", doc, "error", err) 64 79 } 65 80 } 66 81 67 82 func (d *Directory) ResolveHandle(ctx context.Context, h atproto.Handle) (*atproto.DIDDocument, error) { 68 - key := h.String() 69 - doc := d.fromCache(ctx, key) 70 - if doc != nil { 71 - return doc, nil 72 - } 73 - slog.Debug("cannot get DIDDocument from cache, requesting") 74 - 75 - return d.limiter.Do(key, func() (*atproto.DIDDocument, error) { 76 - doc, err := d.inner.ResolveHandle(ctx, h) 77 - if err != nil { 78 - return nil, err 79 - } 80 - d.toCache(ctx, key, doc) 81 - return doc, nil 82 - }) 83 + return resolve(ctx, d, h, d.inner.ResolveHandle) 83 84 } 84 85 85 86 func (d *Directory) ResolveDID(ctx context.Context, did *atproto.DID) (*atproto.DIDDocument, error) { 86 - key := did.String() 87 + return resolve(ctx, d, did, d.inner.ResolveDID) 88 + } 89 + 90 + func resolve[T fmt.Stringer]( 91 + ctx context.Context, 92 + d *Directory, 93 + authority T, 94 + res func(context.Context, T) (*atproto.DIDDocument, error), 95 + ) (*atproto.DIDDocument, error) { 96 + key := authority.String() 87 97 doc := d.fromCache(ctx, key) 88 98 if doc != nil { 99 + slog.Debug("DIDDocument got from cache") 89 100 return doc, nil 90 101 } 91 102 slog.Debug("cannot get DIDDocument from cache, requesting") 92 103 93 104 return d.limiter.Do(key, func() (*atproto.DIDDocument, error) { 94 - doc, err := d.inner.ResolveDID(ctx, did) 105 + doc, err := res(ctx, authority) 95 106 if err != nil { 96 107 return nil, err 97 108 }