cache

package
v0.250.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 15, 2026 License: AGPL-3.0 Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrCacheNotFound = errors.New("cache not found")
View Source
var NewCache = sync.OnceValues(func() (*ImageCache, error) {
	appCfg, err := config.LoadAppConfig()
	if err != nil {
		return nil, fmt.Errorf("load app config: %w", err)
	}
	c := &ImageCache{
		caches: make(map[string]ObjectCache),
	}
	switch appCfg.GetAppEnvironment() {
	case config.EnvProduction:
		bucketName := os.Getenv("IMAGEPROXY_BUCKET")
		imgCache, err := gcs.Connect(context.Background(), bucketName, "")
		if err != nil {
			return nil, fmt.Errorf("connect to gcs:%s %w", bucketName, err)
		}
		c.caches["imgproxy"] = imgCache
		avatarCache, err := gcs.Connect(context.Background(), bucketName, "avatars")
		if err != nil {
			return nil, fmt.Errorf("connect to gcs:%s/%s %w", bucketName, "avatars", err)
		}
		c.caches["avatars"] = avatarCache
		thumbnailCache, err := gcs.Connect(context.Background(), bucketName, "subscription_images")
		if err != nil {
			return nil, fmt.Errorf("connect to gcs:%s/%s %w", bucketName, "subscription_images", err)
		}
		c.caches["subscription_thumbnails"] = thumbnailCache
		screenshotCache, err := gcs.Connect(context.Background(), bucketName, "screenshots")
		if err != nil {
			return nil, fmt.Errorf("connect to gcs:%s/%s %w", bucketName, "screenshots", err)
		}
		c.caches["screenshots"] = screenshotCache
	default:
		var err error
		imgCache, err := newDirCache("imgproxy")
		if err != nil {
			return nil, fmt.Errorf("create dir cache: %s:  %w", "imgproxy", err)
		}
		c.caches["imgproxy"] = imgCache
		avatarCache, err := newDirCache("avatars")
		if err != nil {
			return nil, fmt.Errorf("create dir cache: %s:  %w", "avatars", err)
		}
		c.caches["avatars"] = avatarCache
		thumbnailCache, err := newDirCache("subscription_images")
		if err != nil {
			return nil, fmt.Errorf("create dir cache: %s: %w", "subscription_images", err)
		}
		c.caches["subscription_thumbnails"] = thumbnailCache
		screenshotsCache, err := newDirCache("screenshots")
		if err != nil {
			return nil, fmt.Errorf("create dir cache: %s: %w", "screenshots", err)
		}
		c.caches["screenshots"] = screenshotsCache
	}

	return c, nil
})
View Source
var NewItemsCache = sync.OnceValues(func() (ObjectCache, error) {
	appCfg, err := config.LoadAppConfig()
	if err != nil {
		return nil, fmt.Errorf("load app config: %w", err)
	}

	switch appCfg.GetAppEnvironment() {
	case config.EnvProduction:
		bucketName := os.Getenv("FORAGD_SERVER_BUCKET")
		var err error
		itemsCache, err := gcs.Connect(context.Background(), bucketName, "articles")
		if err != nil {
			return nil, fmt.Errorf("connect to gcs:%s: %w", "articles", err)
		}
		return itemsCache, nil
	default:
		var err error
		itemsCache, err := newDirCache("articles")
		if err != nil {
			return nil, fmt.Errorf("create items dir cache: %w", err)
		}
		return itemsCache, nil
	}
})

Functions

func HandleImage

func HandleImage(cache *ImageCache) http.HandlerFunc

HandleImage handles fetching and displaying an image from one of the image caches.

Types

type ImageCache added in v0.235.0

type ImageCache struct {
	// contains filtered or unexported fields
}

func (*ImageCache) GetAvatar added in v0.235.0

func (c *ImageCache) GetAvatar(ctx context.Context, key string, buf *bytes.Buffer) error

func (*ImageCache) GetImage added in v0.235.0

func (c *ImageCache) GetImage(ctx context.Context, key string, buf *bytes.Buffer) error

func (*ImageCache) GetScreenshot added in v0.235.0

func (c *ImageCache) GetScreenshot(ctx context.Context, key string, buf *bytes.Buffer) error

func (*ImageCache) GetThumbnail added in v0.235.0

func (c *ImageCache) GetThumbnail(ctx context.Context, key string, buf *bytes.Buffer) error

func (*ImageCache) SaveAvatar added in v0.235.0

func (c *ImageCache) SaveAvatar(ctx context.Context, id string, data []byte) error

func (*ImageCache) SaveImage added in v0.235.0

func (c *ImageCache) SaveImage(ctx context.Context, id string, data []byte) error

func (*ImageCache) SaveScreenshot added in v0.235.0

func (c *ImageCache) SaveScreenshot(ctx context.Context, id string, data []byte) error

func (*ImageCache) SaveThumbnail added in v0.235.0

func (c *ImageCache) SaveThumbnail(ctx context.Context, id string, data []byte) error

type ObjectCache added in v0.235.0

type ObjectCache interface {
	Get(ctx context.Context, key string) ([]byte, bool)
	Set(ctx context.Context, key string, value []byte)
	Delete(ctx context.Context, key string)
	Copy(ctx context.Context, key string, buf io.Writer) error
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL