Compare commits

..

5 Commits
v0.7.2 ... main

Author SHA1 Message Date
3ca245dadb
up 2025-08-09 19:13:59 +02:00
6f0e883bff
up 2025-08-09 19:10:16 +02:00
5fc1b55d52
update 2025-08-09 19:05:21 +02:00
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
2 changed files with 33 additions and 24 deletions

View File

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

View File

@ -65,6 +65,7 @@ func (c *Client) retryWorker() {
case job := <-c.retryQueue:
err := c.sendShortenJob(job)
if err != nil {
log.Panicln("got error sending shorten job to server", err)
// Re-enqueue with delay
go func(j shortenJob) {
time.Sleep(2 * time.Second)
@ -95,7 +96,8 @@ func (c *Client) enqueueJob(job shortenJob) {
select {
case c.retryQueue <- job:
default:
log.Println("Retry queue full, dropping job:", job.ID)
log.Println("Retry queue full, dropping job and removing from caches:", job.ID, job.URL)
go c.remFromCache(job.URL)
}
}