mirror of
https://github.com/aljazceru/njump.git
synced 2026-01-31 11:44:34 +01:00
disable cache at build time.
using build tag `nocache` to disable usage of the disk cache entirely.
This commit is contained in:
3
main.go
3
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/*
|
||||
|
||||
15
nostr.go
15
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
|
||||
}
|
||||
|
||||
|
||||
19
null_cache.go
Normal file
19
null_cache.go
Normal 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) {}
|
||||
@@ -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))
|
||||
|
||||
Reference in New Issue
Block a user