2 Commits

Author SHA1 Message Date
9718a027e0 use lru for client side cache 2025-08-07 13:58:39 +02:00
2377fb191a use lru for client side cache 2025-08-07 13:57:10 +02:00

View File

@@ -4,10 +4,10 @@ import (
"encoding/binary" "encoding/binary"
"fmt" "fmt"
"net/http" "net/http"
"sync"
"time" "time"
"git.tijl.dev/tijl/shortify/pkg/generation" "git.tijl.dev/tijl/shortify/pkg/generation"
lru "github.com/hashicorp/golang-lru"
bolt "go.etcd.io/bbolt" bolt "go.etcd.io/bbolt"
) )
@@ -22,10 +22,9 @@ type Client struct {
stopRetry chan struct{} stopRetry chan struct{}
// In-memory cache // In-memory cache
cacheMap map[string]string // longURL -> shortID cacheMap *lru.Cache
cacheLock sync.RWMutex maxCacheSize int
maxCacheSize int maxCacheInitialLoad int
cacheCount int
} }
// NewClient with persistence and retry queue // NewClient with persistence and retry queue
@@ -48,9 +47,12 @@ func NewClient(serverURL string, folder string) (*Client, error) {
stopRetry: make(chan struct{}), stopRetry: make(chan struct{}),
} }
cli.cacheMap = make(map[string]string) cli.cacheMap, err = lru.New(cli.maxCacheSize)
cli.maxCacheSize = 10000 // or make this configurable if err != nil {
cli.cacheCount = 0 return nil, err
}
cli.maxCacheSize = 100000 // or make this configurable
cli.maxCacheInitialLoad = 10000
// Create buckets if not exist // Create buckets if not exist
err = db.Update(func(tx *bolt.Tx) error { err = db.Update(func(tx *bolt.Tx) error {
@@ -118,9 +120,10 @@ func NewClient(serverURL string, folder string) (*Client, error) {
return nil return nil
} }
c := b.Cursor() c := b.Cursor()
for k, v := c.First(); k != nil && cli.cacheCount < cli.maxCacheSize; k, v = c.Next() { initalCounter := 0
cli.cacheMap[string(k)] = string(v) for k, v := c.First(); k != nil && initalCounter < cli.maxCacheInitialLoad; k, v = c.Next() {
cli.cacheCount++ cli.cacheMap.Add(string(k), string(v))
initalCounter++
} }
return nil return nil
}) })
@@ -158,12 +161,9 @@ func (c *Client) Shorten(longURL string, opts ...ShortenOpt) string {
// Check memory cache // Check memory cache
if options.useCache { if options.useCache {
c.cacheLock.RLock() if shortID, ok := c.cacheMap.Get(longURL); ok {
if shortID, ok := c.cacheMap[longURL]; ok { return shortID.(string)
c.cacheLock.RUnlock()
return shortID
} }
c.cacheLock.RUnlock()
} }
// Generate new ID // Generate new ID
@@ -184,12 +184,7 @@ func (c *Client) Shorten(longURL string, opts ...ShortenOpt) string {
} }
func (c *Client) addToCache(longURL, shortID string) { func (c *Client) addToCache(longURL, shortID string) {
c.cacheLock.Lock() c.cacheMap.Add(longURL, shortID)
if _, exists := c.cacheMap[longURL]; !exists && c.cacheCount < c.maxCacheSize {
c.cacheMap[longURL] = shortID
c.cacheCount++
}
c.cacheLock.Unlock()
// Async write to BoltDB // Async write to BoltDB
go func() { go func() {