Compare commits

...

5 Commits
cache ... main

Author SHA1 Message Date
highperfocused
cabc153ee8 add relay software and version info 2025-02-24 10:59:42 +01:00
highperfocused
557a6f4ba8 add LICENSE 2025-02-23 23:38:42 +01:00
highperfocused
813ced8bb0 rename example data file 2025-02-23 23:08:14 +01:00
highperfocused
4402ecb674 bump go version to 1.24.0 for docker relay 2025-02-23 22:44:23 +01:00
44f4c2df6a implement caching (#1)
Co-authored-by: mr0x50 <24775431+mroxso@users.noreply.github.com>
Reviewed-on: highperfocused/lumina-relay#1
2025-02-23 22:41:21 +01:00
6 changed files with 103 additions and 1 deletions

24
LICENSE Normal file
View File

@ -0,0 +1,24 @@
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or
distribute this software, either in source code form or as a compiled
binary, for any purpose, commercial or non-commercial, and by any
means.
In jurisdictions that recognize copyright laws, the author or authors
of this software dedicate any and all copyright interest in the
software to the public domain. We make this dedication for the benefit
of the public at large and to the detriment of our heirs and
successors. We intend this dedication to be an overt act of
relinquishment in perpetuity of all present and future rights to this
software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
For more information, please refer to <https://unlicense.org>

View File

@ -8,7 +8,7 @@
################################################################################
# Create a stage for building the application.
ARG GO_VERSION=1.23.4
ARG GO_VERSION=1.24.0
FROM --platform=$BUILDPLATFORM golang:${GO_VERSION} AS build
WORKDIR /src

64
relay/cache/cache.go vendored Normal file
View File

@ -0,0 +1,64 @@
package cache
import (
"sync"
"time"
)
type item struct {
value interface{}
expiration int64
}
type Cache struct {
items map[string]item
mu sync.RWMutex
}
func New() *Cache {
cache := &Cache{
items: make(map[string]item),
}
go cache.startCleanup()
return cache
}
func (c *Cache) Set(key string, value interface{}, duration time.Duration) {
c.mu.Lock()
defer c.mu.Unlock()
c.items[key] = item{
value: value,
expiration: time.Now().Add(duration).UnixNano(),
}
}
func (c *Cache) Get(key string) (interface{}, bool) {
c.mu.RLock()
defer c.mu.RUnlock()
item, exists := c.items[key]
if !exists {
return nil, false
}
if time.Now().UnixNano() > item.expiration {
return nil, false
}
return item.value, true
}
func (c *Cache) startCleanup() {
ticker := time.NewTicker(time.Minute)
for range ticker.C {
c.mu.Lock()
now := time.Now().UnixNano()
for k, v := range c.items {
if now > v.expiration {
delete(c.items, k)
}
}
c.mu.Unlock()
}
}

View File

@ -33,6 +33,8 @@ func main() {
relay.Info.PubKey = getEnv("RELAY_PUBKEY", "79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798")
relay.Info.Description = getEnv("RELAY_DESCRIPTION", "LUMINA Relay")
relay.Info.Icon = getEnv("RELAY_ICON", "https://external-content.duckduckgo.com/iu/?u=https%3A%2F%2Fliquipedia.net%2Fcommons%2Fimages%2F3%2F35%2FSCProbe.jpg&f=1&nofb=1&ipt=0cbbfef25bce41da63d910e86c3c343e6c3b9d63194ca9755351bb7c2efa3359&ipo=images")
relay.Info.Software = "lumina-relay"
relay.Info.Version = "0.1.0"
// Print relay information
fmt.Printf("Name: %s\n", relay.Info.Name)

View File

@ -4,6 +4,8 @@ import (
"database/sql"
"encoding/json"
"time"
"git.highperfocused.tech/highperfocused/lumina-relay/relay/cache"
)
type Post struct {
@ -16,8 +18,17 @@ type Post struct {
ReactionCount int `json:"reaction_count"`
}
var (
trendingCache = cache.New()
cacheDuration = 5 * time.Minute
)
// GetTrendingKind20 returns the top 20 trending posts of kind 20 from the last 24 hours
func GetTrendingKind20(db *sql.DB) ([]Post, error) {
if cached, ok := trendingCache.Get("trending_kind_20"); ok {
return cached.([]Post), nil
}
query := `
WITH reactions AS (
SELECT
@ -65,5 +76,6 @@ func GetTrendingKind20(db *sql.DB) ([]Post, error) {
trendingPosts = append(trendingPosts, post)
}
trendingCache.Set("trending_kind_20", trendingPosts, cacheDuration)
return trendingPosts, nil
}