disable cache at build time.

using build tag `nocache` to disable usage of the disk cache entirely.
This commit is contained in:
fiatjaf
2023-07-12 14:21:55 -03:00
parent 9dbd4d6d40
commit c0cbb3c27c
5 changed files with 31 additions and 12 deletions

View File

@@ -1,3 +1,5 @@
//go:build !nocache
package main
import (

View File

@@ -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/*

View File

@@ -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
}

19
null_cache.go Normal file
View File

@@ -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) {}

View File

@@ -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))