cloudrun

package module
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Dec 8, 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 environments. It detects Cloud Run via K_SERVICE environment variable and attempts to use Datastore, falling back to local files if Datastore is unavailable or if not running in Cloud Run.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func New

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

New creates a persistence layer optimized for Cloud Run environments. It automatically selects the best available backend:

  • In Cloud Run (K_SERVICE env var set): tries Datastore, falls back to local files on error
  • Outside Cloud Run: uses local files

The cacheID is used as the database name for Datastore or subdirectory for local files. This function always succeeds by falling back to local files if Datastore is unavailable.

Types

This section is empty.

Jump to

Keyboard shortcuts

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