cloudrun

package module
v1.5.0 Latest Latest
Warning

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

Go to latest
Published: Dec 15, 2025 License: Apache-2.0 Imports: 5 Imported by: 0

README

persist/cloudrun

Automatic persistence backend selection for Cloud Run environments.

Overview

This package detects whether your application is running in Google Cloud Run and automatically selects the best persistence backend:

  • In Cloud Run (when K_SERVICE env var is set): Attempts to use Google Cloud Datastore
  • Datastore unavailable: Gracefully falls back to local file persistence
  • Outside Cloud Run: Uses local file persistence

Usage

import (
    "github.com/codeGROOVE-dev/sfcache"
    "github.com/codeGROOVE-dev/sfcache/persist/cloudrun"
)

// Automatic backend selection with fallback
p, _ := cloudrun.New[string, User](ctx, "myapp")
cache, _ := sfcache.New[string, User](ctx,
    sfcache.WithPersistence(p))

The function always succeeds by falling back to local files if Datastore is unavailable due to:

  • Missing credentials
  • Network issues
  • Configuration problems
  • Running outside Cloud Run

When to Use

Use this package when:

  • Deploying to Cloud Run but want the flexibility to run locally
  • You want automatic environment detection without manual configuration
  • You prefer graceful degradation over hard failures

For production deployments where you need explicit control, consider using the datastore or localfs packages directly.

Documentation

Overview

Package cloudrun provides automatic persistence backend selection for Cloud Run. Detects Cloud Run via K_SERVICE env var and tries Datastore first, falling back to local files if unavailable.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Store

type Store[K comparable, V any] interface {
	ValidateKey(key K) error
	Get(ctx context.Context, key K) (V, time.Time, bool, error)
	Set(ctx context.Context, key K, value V, expiry time.Time) error
	Delete(ctx context.Context, key K) error
	Cleanup(ctx context.Context, maxAge time.Duration) (int, error)
	Location(key K) string
	Flush(ctx context.Context) (int, error)
	Len(ctx context.Context) (int, error)
	Close() error
}

Store is the persistence interface returned by New. Matches sfcache.Store so callers can pass it to sfcache.NewTiered.

func New

func New[K comparable, V any](ctx context.Context, cacheID string) (Store[K, V], error)

New creates a persistence layer for Cloud Run environments. In Cloud Run: tries Datastore, falls back to local files on error. Outside Cloud Run: uses local files directly.

Jump to

Keyboard shortcuts

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