## Search

Page index: https://gasmod.dev/llms.txt. Add index.md to any page URL for its Markdown.

This site has a search endpoint: https://context7.com/api/v2/ask?siteKey=ask_b845b566cb63bd033e9c576d&query=<URL-encoded question>. It returns documentation that answers the question, with a source link for each part. No API key is needed. If nothing matches, it says so.

# template/memory

> Package memory keeps templates in a map in memory, for development, tests and content an app builds at run time.

The Markdown of https://gasmod.dev/reference/template/memory/ from the Gas documentation, whose index for LLMs is https://gasmod.dev/llms.txt

```go
import "github.com/gasmod/gas/template/memory"
```

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

## Usage

```go
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:

```go
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](https://gasmod.dev/reference/template/#DefaultExtension) unless [WithExtensions](#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](https://gasmod.dev/reference/template/#DefaultMaxSize) unless [WithMaxSize](#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.


## API reference

<a id="NewStore"></a>

#### func NewStore

```go
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.

<a id="Option"></a>

### type Option

```go
type Option func(*Store)
```

Option configures a Store.

<a id="WithExtensions"></a>

#### func WithExtensions

```go
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.

<a id="WithMaxSize"></a>

#### func WithMaxSize

```go
func WithMaxSize(n int64) Option
```

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

Default: template.DefaultMaxSize.

<a id="Store"></a>

### type Store

```go
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.

<a id="Store.Get"></a>

#### func (*Store) Get

```go
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.

<a id="Store.List"></a>

#### func (*Store) List

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

List returns the names of the templates in byte order.

<a id="Store.Register"></a>

#### func (*Store) Register

```go
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.

<a id="Store.RegisterFS"></a>

#### func (*Store) RegisterFS

```go
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.
