Storage

gox.Storage conserves values shared throughout the current process of gox-server.

gox
gox.Storage.Set("maintenance", true)

var maintenance bool
exists, err := gox.Storage.Get("maintenance", &maintenance)

Storage is safe for concurrent access, but it is local, volatile and not distributed through the cluster.


Structure

text
Storage
{
    Set(key string, value any)
    Get(key string, out any) (bool, error)
    Has(key string) bool
    Delete(key string)
}

Methods

MethodDescription
Set()Save or replace a value.
Get()It recovers a value in a tipid destination.
Has()It indicates if there is a key.
Delete()Eliminate a key.

Set()

Firma:Set(key string, value any)

Save a value under the indicated key. If the key already exists, replace the above value.

gox
gox.Storage.Set("maxAttempts", 5)
gox.Storage.Set("administrator", "George")

Set() accepts any type, including nil. The value is kept in memory without serialization or deep copy. If you save a map, slice, pointer or other reference value, the application shares that same reference.


Get()

Firma:Get(key string, out any) (bool, error)

Recovers the value and assigns it to the destination received by out.

gox
var maxAttempts int

exists, err := gox.Storage.Get("maxAttempts", &maxAttempts)
if err != nil {
    return err
}

if !exists {
    maxAttempts = 3
}

out must be a non-zero pointer. The stored type must be able to be assigned directly to the destination type; Get() does not make automatic conversions.

gox
gox.Storage.Set("attempts", 5)

var attempts string
_, err := gox.Storage.Get("attempts", &attempts)
// err != nil porque int no puede asignarse a string

When the key does not exist, return false, nil and does not change the destination. When the stored value is nil, fate must be a guy who can receive nilsuch as a pointer, map, slide, channel, function or interface.


Has()

Firma:Has(key string) bool

It indicates if the key exists, even when its value is nil.

gox
if gox.Storage.Has("maintenance") {
    log.Println("Maintenance value configured")
}

When you also need the value, use it directly Get() to avoid two searches.


Delete()

Firma:Delete(key string)

Eliminates the key and its value. If the key does not exist, the operation does not produce any effect.

gox
gox.Storage.Delete("maintenance")

Concurrency and references

The collection protects operations Set(), Get(), Has() and Delete() This protection does not extend to the inside of the stored values.

If several goroutins recover the same map or pointer and modify it, the application should synchronize these modifications separately.


Scope and persistence

The values exist only as long as the current process remains active. gox-server it keeps its own storage.

To share data between instances or to keep them after a reboot, use a database or external storage. For data associated with a client, use Sessions.


Integration with Go

gox.Storage it maintains the original types of Go and requires accurate allocation when recovering them. This avoids implicit deerializations and allows to store internal structures without defining an additional format.

The synchronization protects the global record, but the recovered mutable objects retain the normal rules of Go's concurrence.


Best practices

  • It uses stable and domain-specific keys.
  • Always check the error and the boolean returned by Get().
  • Do not use Has() before Get() when you also need the courage.
  • It separately synchronizes the shared mutable values.
  • It does not store data that must persist or be shared between instances.

See also

Next step

Continue with gox.Sessions to recover existing sessions outside a specific request.

Sessions