Compare commits
No commits in common. "cache_improvements" and "main" have entirely different histories.
cache_impr
...
main
49
relay/cache/cache.go
vendored
49
relay/cache/cache.go
vendored
@ -8,13 +8,11 @@ import (
|
|||||||
type item struct {
|
type item struct {
|
||||||
value interface{}
|
value interface{}
|
||||||
expiration int64
|
expiration int64
|
||||||
staleTime int64
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type Cache struct {
|
type Cache struct {
|
||||||
items map[string]item
|
items map[string]item
|
||||||
mu sync.RWMutex
|
mu sync.RWMutex
|
||||||
refreshing sync.Map
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func New() *Cache {
|
func New() *Cache {
|
||||||
@ -29,68 +27,35 @@ func (c *Cache) Set(key string, value interface{}, duration time.Duration) {
|
|||||||
c.mu.Lock()
|
c.mu.Lock()
|
||||||
defer c.mu.Unlock()
|
defer c.mu.Unlock()
|
||||||
|
|
||||||
now := time.Now().UnixNano()
|
|
||||||
c.items[key] = item{
|
c.items[key] = item{
|
||||||
value: value,
|
value: value,
|
||||||
expiration: now + duration.Nanoseconds(),
|
expiration: time.Now().Add(duration).UnixNano(),
|
||||||
staleTime: now + (duration * 2).Nanoseconds(), // Stale time is double the normal duration
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Cache) Get(key string) (interface{}, bool) {
|
func (c *Cache) Get(key string) (interface{}, bool) {
|
||||||
c.mu.RLock()
|
c.mu.RLock()
|
||||||
item, exists := c.items[key]
|
defer c.mu.RUnlock()
|
||||||
c.mu.RUnlock()
|
|
||||||
|
|
||||||
|
item, exists := c.items[key]
|
||||||
if !exists {
|
if !exists {
|
||||||
return nil, false
|
return nil, false
|
||||||
}
|
}
|
||||||
|
|
||||||
now := time.Now().UnixNano()
|
if time.Now().UnixNano() > item.expiration {
|
||||||
if now > item.staleTime {
|
|
||||||
return nil, false
|
return nil, false
|
||||||
}
|
}
|
||||||
|
|
||||||
return item.value, true
|
return item.value, true
|
||||||
}
|
}
|
||||||
|
|
||||||
func (c *Cache) GetOrRefresh(key string, refreshFn func() (interface{}, error), duration time.Duration) (interface{}, bool) {
|
|
||||||
// Try to get from cache first
|
|
||||||
if value, exists := c.Get(key); exists {
|
|
||||||
now := time.Now().UnixNano()
|
|
||||||
item := c.items[key]
|
|
||||||
|
|
||||||
// If the value is expired but not stale, trigger refresh in background
|
|
||||||
if now > item.expiration && now <= item.staleTime {
|
|
||||||
if _, loading := c.refreshing.LoadOrStore(key, true); !loading {
|
|
||||||
go func() {
|
|
||||||
defer c.refreshing.Delete(key)
|
|
||||||
if newValue, err := refreshFn(); err == nil {
|
|
||||||
c.Set(key, newValue, duration)
|
|
||||||
}
|
|
||||||
}()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return value, true
|
|
||||||
}
|
|
||||||
|
|
||||||
// If no value exists, do a blocking refresh
|
|
||||||
value, err := refreshFn()
|
|
||||||
if err != nil {
|
|
||||||
return nil, false
|
|
||||||
}
|
|
||||||
|
|
||||||
c.Set(key, value, duration)
|
|
||||||
return value, true
|
|
||||||
}
|
|
||||||
|
|
||||||
func (c *Cache) startCleanup() {
|
func (c *Cache) startCleanup() {
|
||||||
ticker := time.NewTicker(time.Minute)
|
ticker := time.NewTicker(time.Minute)
|
||||||
for range ticker.C {
|
for range ticker.C {
|
||||||
c.mu.Lock()
|
c.mu.Lock()
|
||||||
now := time.Now().UnixNano()
|
now := time.Now().UnixNano()
|
||||||
for k, v := range c.items {
|
for k, v := range c.items {
|
||||||
if now > v.staleTime {
|
if now > v.expiration {
|
||||||
delete(c.items, k)
|
delete(c.items, k)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -23,7 +23,12 @@ var (
|
|||||||
cacheDuration = 5 * time.Minute
|
cacheDuration = 5 * time.Minute
|
||||||
)
|
)
|
||||||
|
|
||||||
func fetchTrendingKind20(db *sql.DB) ([]Post, error) {
|
// 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 := `
|
query := `
|
||||||
WITH reactions AS (
|
WITH reactions AS (
|
||||||
SELECT
|
SELECT
|
||||||
@ -71,17 +76,6 @@ func fetchTrendingKind20(db *sql.DB) ([]Post, error) {
|
|||||||
trendingPosts = append(trendingPosts, post)
|
trendingPosts = append(trendingPosts, post)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
trendingCache.Set("trending_kind_20", trendingPosts, cacheDuration)
|
||||||
return trendingPosts, nil
|
return trendingPosts, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetTrendingKind20(db *sql.DB) ([]Post, error) {
|
|
||||||
posts, exists := trendingCache.GetOrRefresh("trending_kind_20", func() (interface{}, error) {
|
|
||||||
return fetchTrendingKind20(db)
|
|
||||||
}, cacheDuration)
|
|
||||||
|
|
||||||
if !exists {
|
|
||||||
return nil, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
return posts.([]Post), nil
|
|
||||||
}
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user