first commit

This commit is contained in:
2026-02-17 17:15:39 -08:00
parent 1bf9c29f09
commit b70ea7e877
25 changed files with 4145 additions and 0 deletions

48
store/store.go Normal file
View File

@@ -0,0 +1,48 @@
package store
import (
"context"
"crypto/rand"
"encoding/hex"
"fmt"
"time"
"github.com/redis/go-redis/v9"
)
// TTL is how long any stored file lives in Redis before automatic deletion.
const TTL = 2 * time.Hour
type Store struct {
rdb *redis.Client
}
func New(addr string) (*Store, error) {
rdb := redis.NewClient(&redis.Options{Addr: addr})
if err := rdb.Ping(context.Background()).Err(); err != nil {
return nil, fmt.Errorf("redis connect %s: %w", addr, err)
}
return &Store{rdb: rdb}, nil
}
// Set stores binary data under key with the global TTL.
func (s *Store) Set(ctx context.Context, key string, data []byte) error {
return s.rdb.Set(ctx, key, data, TTL).Err()
}
// Get retrieves binary data by key.
func (s *Store) Get(ctx context.Context, key string) ([]byte, error) {
return s.rdb.Get(ctx, key).Bytes()
}
// Delete removes a key immediately (called after download).
func (s *Store) Delete(ctx context.Context, key string) {
s.rdb.Del(ctx, key)
}
// NewID generates a random hex ID for use as a Redis key suffix.
func NewID() string {
b := make([]byte, 16)
rand.Read(b)
return hex.EncodeToString(b)
}