diff --git a/cache.go b/cache.go index e114aaa..e308468 100644 --- a/cache.go +++ b/cache.go @@ -1,3 +1,5 @@ +//go:build !nocache + package main import ( diff --git a/main.go b/main.go index 7a69a0d..c6cedd5 100644 --- a/main.go +++ b/main.go @@ -12,8 +12,7 @@ import ( ) type Settings struct { - Port string `envconfig:"PORT" default:"2999"` - DisableCache bool `envconfig:"DISABLE_CACHE" default:"false"` + Port string `envconfig:"PORT" default:"2999"` } //go:embed static/* diff --git a/nostr.go b/nostr.go index 01dd40b..efa960b 100644 --- a/nostr.go +++ b/nostr.go @@ -115,14 +115,12 @@ func getEvent(ctx context.Context, code string) (*nostr.Event, error) { ctx, cancel := context.WithTimeout(ctx, time.Second*8) defer cancel() for event := range pool.SubManyEose(ctx, relays, nostr.Filters{filter}) { - if !s.DisableCache { - b, err := nson.Marshal(event) - if err != nil { - log.Error().Err(err).Stringer("event", event).Msg("error marshaling nson") - return event, nil - } - cache.SetWithTTL(code, []byte(b), time.Hour*24*7) + b, err := nson.Marshal(event) + if err != nil { + log.Error().Err(err).Stringer("event", event).Msg("error marshaling nson") + return event, nil } + cache.SetWithTTL(code, []byte(b), time.Hour*24*7) return event, nil } @@ -153,8 +151,11 @@ func getLastNotes(ctx context.Context, code string) []*nostr.Event { }) lastNotes := make([]*nostr.Event, 0, 20) for event := range events { + fmt.Println("last note", event) lastNotes = nostr.InsertEventIntoDescendingList(lastNotes, event) } + + fmt.Println("returning", len(lastNotes)) return lastNotes } diff --git a/null_cache.go b/null_cache.go new file mode 100644 index 0000000..d53f27f --- /dev/null +++ b/null_cache.go @@ -0,0 +1,19 @@ +//go:build nocache + +package main + +import ( + "time" +) + +var cache = Cache{} + +type Cache struct{} + +func (c *Cache) initialize() func() { return func() {} } +func (c *Cache) Get(key string) ([]byte, bool) { return nil, false } +func (c *Cache) GetJSON(key string, recv any) bool { return false } +func (c *Cache) Set(key string, value []byte) {} +func (c *Cache) SetJSON(key string, value any) {} +func (c *Cache) SetWithTTL(key string, value []byte, ttl time.Duration) {} +func (c *Cache) SetJSONWithTTL(key string, value any, ttl time.Duration) {} diff --git a/render.go b/render.go index a28cfa2..a391341 100644 --- a/render.go +++ b/render.go @@ -75,9 +75,7 @@ func render(w http.ResponseWriter, r *http.Request) { ctx, cancel := context.WithTimeout(r.Context(), time.Second*4) lastNotes = getLastNotes(ctx, code) cancel() - if !s.DisableCache { - cache.SetJSONWithTTL(key, lastNotes, time.Hour*24) - } + cache.SetJSONWithTTL(key, lastNotes, time.Hour*24) } renderableLastNotes = make([]*Event, len(lastNotes))