Skip to content

Commit ef61f88

Browse files
committed
feat: implement RistrettoStorage for improved caching with HTTP middleware
1 parent 66b8e00 commit ef61f88

2 files changed

Lines changed: 71 additions & 0 deletions

File tree

main.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"media-proxy/metrics"
2020
fiberprometheus "media-proxy/middlewares/prometheus"
2121
"media-proxy/routes"
22+
"media-proxy/storage"
2223

2324
"github.com/dgraph-io/ristretto/v2"
2425
)
@@ -50,6 +51,13 @@ func main() {
5051
BufferItems: 64, // number of keys per Get buffer.
5152
}
5253

54+
// HTTP cache configuration for middleware
55+
httpCacheConfig := &ristretto.Config[string, []byte]{
56+
NumCounters: 1e6, // number of keys to track frequency of (1M).
57+
MaxCost: 1 << 28, // maximum cost of cache (256MB).
58+
BufferItems: 64, // number of keys per Get buffer.
59+
}
60+
5361
if config.HTTPCacheTTL == 0 {
5462
config.HTTPCacheTTL = 1800 // 30 minutes
5563
}
@@ -75,6 +83,12 @@ func main() {
7583
logger.Fatal(err.Error())
7684
}
7785

86+
// Create HTTP cache for middleware
87+
httpCacheStore, err := ristretto.NewCache(httpCacheConfig)
88+
if err != nil {
89+
logger.Fatal(err.Error())
90+
}
91+
7892
// Initialize optional S3 cache
7993
s3cache, s3err := routes.NewS3Cache(
8094
config.S3Enabled,
@@ -135,6 +149,14 @@ func main() {
135149
app.Use(etag.New())
136150
app.Use(cache.New(cache.Config{
137151
Expiration: time.Minute * 10,
152+
Storage: storage.NewRistrettoStorage(httpCacheStore),
153+
Next: func(c *fiber.Ctx) bool {
154+
if strings.HasPrefix(c.Path(), "/videos/") && !strings.HasPrefix(c.Path(), "/videos/preview/") {
155+
return true
156+
}
157+
158+
return false
159+
},
138160
KeyGenerator: func(c *fiber.Ctx) string {
139161
if strings.HasPrefix(c.Path(), "/videos/") && !strings.HasPrefix(c.Path(), "/videos/preview/") {
140162
return c.Path() + "?" + string(c.Request().Header.Peek("Range"))

storage/ristretto.go

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package storage
2+
3+
import (
4+
"time"
5+
6+
"github.com/dgraph-io/ristretto/v2"
7+
)
8+
9+
// RistrettoStorage implements fiber.Storage using Ristretto cache
10+
type RistrettoStorage struct {
11+
cache *ristretto.Cache[string, []byte]
12+
}
13+
14+
// NewRistrettoStorage creates a new RistrettoStorage
15+
func NewRistrettoStorage(cache *ristretto.Cache[string, []byte]) *RistrettoStorage {
16+
return &RistrettoStorage{cache: cache}
17+
}
18+
19+
// Get retrieves data from cache
20+
func (r *RistrettoStorage) Get(key string) ([]byte, error) {
21+
if value, found := r.cache.Get(key); found {
22+
return value, nil
23+
}
24+
return nil, nil
25+
}
26+
27+
// Set stores data in cache
28+
func (r *RistrettoStorage) Set(key string, val []byte, exp time.Duration) error {
29+
r.cache.SetWithTTL(key, val, 1, exp)
30+
return nil
31+
}
32+
33+
// Delete removes data from cache
34+
func (r *RistrettoStorage) Delete(key string) error {
35+
r.cache.Del(key)
36+
return nil
37+
}
38+
39+
// Reset clears all data from cache
40+
func (r *RistrettoStorage) Reset() error {
41+
r.cache.Clear()
42+
return nil
43+
}
44+
45+
// Close closes the storage (no-op for ristretto)
46+
func (r *RistrettoStorage) Close() error {
47+
r.cache.Close()
48+
return nil
49+
}

0 commit comments

Comments
 (0)