datastore

package module
v1.3.0 Latest Latest
Warning

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

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

README

persist/datastore

Google Cloud Datastore persistence with native TTL support.

Features

  • Scales automatically, native TTL support
  • JSON encoding with base64 for binary safety
  • Streaming loads for warmup
  • Works across Cloud Run instances

Usage

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

// cacheID becomes the Datastore database name
p, _ := datastore.New[string, User](ctx, "myapp")

// Optional: clean up stale entries on startup
p.Cleanup(ctx, 24*time.Hour)

cache, _ := sfcache.New[string, User](ctx,
    sfcache.WithPersistence(p))
gcloud firestore fields ttls update expiry \
  --collection-group=CacheEntry \
  --enable-ttl \
  --database=myapp

One-time setup per database. Datastore deletes expired entries within 24 hours.

Fallback Pattern

p, err := datastore.New[string, User](ctx, "myapp")
if err != nil {
    p, _ = localfs.New[string, User]("myapp", "")
}

Key Constraints

  • Maximum key length: 1500 characters (Datastore limit)

Documentation

Overview

Package datastore provides Google Cloud Datastore persistence for sfcache.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Store added in v1.3.0

type Store[K comparable, V any] struct {
	// contains filtered or unexported fields
}

Store implements persistence using Google Cloud Datastore.

func New

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

New creates a new Datastore-based persistence layer. The cacheID is used as the Datastore database name. An empty projectID will be auto-detected from the environment.

func (*Store[K, V]) Cleanup added in v1.3.0

func (s *Store[K, V]) Cleanup(ctx context.Context, maxAge time.Duration) (int, error)

Cleanup removes expired entries from Datastore. maxAge specifies how old entries must be (based on expiry field) before deletion. If native Datastore TTL is properly configured, this will find no entries.

func (*Store[K, V]) Close added in v1.3.0

func (s *Store[K, V]) Close() error

Close releases Datastore client resources.

func (*Store[K, V]) Delete added in v1.3.0

func (s *Store[K, V]) Delete(ctx context.Context, key K) error

Delete removes a value from Datastore.

func (*Store[K, V]) Flush added in v1.3.0

func (s *Store[K, V]) Flush(ctx context.Context) (int, error)

Flush removes all entries from Datastore. Returns the number of entries removed and any error.

func (*Store[K, V]) Get added in v1.3.0

func (s *Store[K, V]) Get(ctx context.Context, key K) (value V, expiry time.Time, found bool, err error)

Get retrieves a value from Datastore.

func (*Store[K, V]) Len added in v1.3.0

func (s *Store[K, V]) Len(ctx context.Context) (int, error)

Len returns the number of entries in Datastore.

func (*Store[K, V]) Location added in v1.3.0

func (s *Store[K, V]) Location(key K) string

Location returns the Datastore key path for a given cache key. Implements the Store interface Location() method. Format: "kind/key" (e.g., "CacheEntry/mykey").

func (*Store[K, V]) Set added in v1.3.0

func (s *Store[K, V]) Set(ctx context.Context, key K, value V, expiry time.Time) error

Set saves a value to Datastore.

func (*Store[K, V]) ValidateKey added in v1.3.0

func (*Store[K, V]) ValidateKey(key K) error

ValidateKey checks if a key is valid for Datastore persistence. Datastore has stricter key length limits than files.

Jump to

Keyboard shortcuts

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