memory

package
v0.10.0 Latest Latest
Warning

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

Go to latest
Published: Nov 15, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package memory keeps templates in a map in memory, for development, tests and content an app builds at run time. Store implements gas.TemplateProvider with the contract of the template package.

Usage

store, err := memory.NewStore(memory.WithExtensions(".html", ".txt"))()
if err != nil {
	return err
}
err = store.RegisterFS(ctx, emails) // an embed.FS

After this the store holds every `.html` and `.txt` file of emails, under its path in emails, and nothing else. In an app, register the constructor:

gas.WithSingletonService[gas.TemplateProvider](memory.NewStore())

Store

A Store starts empty and holds what is registered until the process ends; it has no lifecycle. Names and extensions are as the template package describes, template.DefaultExtension unless WithExtensions gives others.

  • Register keeps a copy of the content and Get returns a copy, so neither the caller's slice nor the returned one shares memory with the store.
  • RegisterFS reads every file first and stores them all under one hold of the lock, so a reader sees all of them or none.
  • Register and RegisterFS refuse content over the store's maximum size, template.DefaultMaxSize unless WithMaxSize gives another, with an error wrapping template.ErrTooLarge.

Concurrency

A Store is safe for concurrent use. A read-write lock guards the map:

  • Get and List hold it for reading.
  • Register and RegisterFS hold it for writing.
  • RegisterFS never holds it while it reads its files.

Errors

The constructor returns an error wrapping template.ErrInvalidExtension or template.ErrInvalidMaxSize for a bad option.

The methods return errors wrapping template.ErrInvalidName, template.ErrTemplateNotFound and template.ErrTooLarge. RegisterFS also returns template.ErrNilFS, template.ErrRead and the context's error. The texts are those the template package lists.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewStore

func NewStore(opts ...Option) func() (*Store, error)

NewStore returns the constructor of an empty Store with opts applied, in the shape the gas container expects. The constructor returns an error wrapping template.ErrInvalidExtension or template.ErrInvalidMaxSize for a bad option.

Types

type Option added in v0.6.0

type Option func(*Store)

Option configures a Store.

func WithExtensions added in v0.6.0

func WithExtensions(exts ...string) Option

WithExtensions sets the extensions a template name must end in. The constructor checks them, as described in the template package documentation.

Default: template.DefaultExtension.

func WithMaxSize added in v0.6.0

func WithMaxSize(n int64) Option

WithMaxSize sets the most bytes a template may hold. The constructor checks it with template.CheckMaxSize.

Default: template.DefaultMaxSize.

type Store

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

Store keeps templates in memory, as described in the package documentation. It is safe for concurrent use.

func (*Store) Get

func (s *Store) Get(_ context.Context, name string) ([]byte, error)

Get returns a copy of the content of the template name, an error wrapping template.ErrTemplateNotFound when there is none, or one wrapping template.ErrInvalidName for a name the store does not accept.

func (*Store) List

func (s *Store) List(context.Context) ([]string, error)

List returns the names of the templates in byte order.

func (*Store) Register

func (s *Store) Register(_ context.Context, name string, content []byte) error

Register stores a copy of content as the template name, replacing one of that name. Nil content is stored as empty. A name the store does not accept is an error wrapping template.ErrInvalidName, and content over the store's maximum size one wrapping template.ErrTooLarge.

func (*Store) RegisterFS

func (s *Store) RegisterFS(ctx context.Context, fsys fs.FS) error

RegisterFS reads the files of fsys with one of the store's extensions and stores them all, or none when reading fails, as described in the template package documentation.

Jump to

Keyboard shortcuts

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