6 Commits

Author SHA1 Message Date
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
6f7c7c5ea7 client side cache 2025-08-07 11:04:44 +02:00
474de0734f up 2025-08-07 10:27:43 +02:00
181e33db92 up 2025-08-06 20:59:33 +02:00
4 changed files with 89 additions and 5 deletions

View File

@@ -7,6 +7,7 @@ import (
"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"
) )
@@ -19,6 +20,11 @@ type Client struct {
db *bolt.DB db *bolt.DB
retryQueue chan shortenJob retryQueue chan shortenJob
stopRetry chan struct{} stopRetry chan struct{}
// In-memory cache
cacheMap *lru.Cache
maxCacheSize int
maxCacheInitialLoad int
} }
// NewClient with persistence and retry queue // NewClient with persistence and retry queue
@@ -41,6 +47,13 @@ func NewClient(serverURL string, folder string) (*Client, error) {
stopRetry: make(chan struct{}), stopRetry: make(chan struct{}),
} }
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 // Create buckets if not exist
err = db.Update(func(tx *bolt.Tx) error { err = db.Update(func(tx *bolt.Tx) error {
_, err := tx.CreateBucketIfNotExists([]byte(bucketPrefix)) _, err := tx.CreateBucketIfNotExists([]byte(bucketPrefix))
@@ -48,7 +61,16 @@ func NewClient(serverURL string, folder string) (*Client, error) {
return err return err
} }
_, err = tx.CreateBucketIfNotExists([]byte(bucketRetryJobs)) _, err = tx.CreateBucketIfNotExists([]byte(bucketRetryJobs))
if err != nil {
return err return err
}
_, err = tx.CreateBucketIfNotExists([]byte(bucketURLCache))
if err != nil {
return err
}
return nil
}) })
if err != nil { if err != nil {
return nil, err return nil, err
@@ -91,6 +113,21 @@ func NewClient(serverURL string, folder string) (*Client, error) {
cli.prefix = prefix cli.prefix = prefix
cli.gen = generation.NewGenerator(prefix) cli.gen = generation.NewGenerator(prefix)
// load cache
_ = cli.db.View(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte("url_cache"))
if b == nil {
return nil
}
c := b.Cursor()
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
})
// Load retry jobs from DB into channel // Load retry jobs from DB into channel
go cli.loadRetryJobs() go cli.loadRetryJobs()
@@ -100,14 +137,60 @@ func NewClient(serverURL string, folder string) (*Client, error) {
return cli, nil return cli, nil
} }
// Shorten creates a short URL and sends it async to the central server /*
func (c *Client) Shorten(longURL string) string { Shorten
*/
type ShortenOpt func(*shortenOptions)
type shortenOptions struct {
useCache bool
}
func UseCache() ShortenOpt {
return func(opts *shortenOptions) {
opts.useCache = true
}
}
func (c *Client) Shorten(longURL string, opts ...ShortenOpt) string {
options := shortenOptions{}
for _, opt := range opts {
opt(&options)
}
// Check memory cache
if options.useCache {
if shortID, ok := c.cacheMap.Get(longURL); ok {
return shortID.(string)
}
}
// Generate new ID
shortID := c.gen.NextID() shortID := c.gen.NextID()
// Queue job
go c.enqueueJob(shortenJob{ go c.enqueueJob(shortenJob{
ID: shortID, ID: shortID,
URL: longURL, URL: longURL,
}) })
// Async store in cache
if options.useCache {
go c.addToCache(longURL, shortID)
}
return shortID return shortID
} }
func (c *Client) addToCache(longURL, shortID string) {
c.cacheMap.Add(longURL, shortID)
// Async write to BoltDB
go func() {
_ = c.db.Update(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte("url_cache"))
return b.Put([]byte(longURL), []byte(shortID))
})
}()
}

View File

@@ -16,6 +16,7 @@ import (
const ( const (
bucketPrefix = "prefix" bucketPrefix = "prefix"
bucketRetryJobs = "retry_queue" bucketRetryJobs = "retry_queue"
bucketURLCache = "url_cache"
dbFileName = "shorty_client.db" dbFileName = "shorty_client.db"
) )

View File

@@ -16,7 +16,7 @@ func (s *Server) Admin() *fiber.App {
if err != nil { if err != nil {
return err return err
} }
var response []byte response := make([]byte, 2)
binary.LittleEndian.PutUint16(response, prefix) binary.LittleEndian.PutUint16(response, prefix)
return c.Send(response) return c.Send(response)
}) })

View File

@@ -38,7 +38,7 @@ func (s *Server) HandleGetURL() func(*fiber.Ctx) error {
url, err := s.GetURL(shortID) url, err := s.GetURL(shortID)
if err != nil { if err != nil {
return err return c.Next()
} }
s.LogVisit(VisitLog{ s.LogVisit(VisitLog{