MemoryStorage

Summary: Keep saves in memory for tests and temporary sessions.

Description

MemoryStorage keeps data in the current storage instance. It implements ISaveStorage and IBackupStorage, so loads can recover from its backup copies. Use a unique identity for each independent store. For an isolated test, assign it to a separate SaveService.

Namespace Gley.SaveSystem.

Declaration

C#
public class MemoryStorage : ISaveStorage, IBackupStorage

Members

MemberTypeDescription
MemoryStorageConstructorMemoryStorage(string identity)
Creates an empty store with the given identity.
IdentitystringIdentity used by Save System to coordinate operations on this store.
RequiresMainThreadboolFalse. The storage itself can be used by background work.
TryRead—bool TryRead(string storageKey, out byte[] data)
Reads a copy of the main data for a key.
Write—void Write(string storageKey, byte[] data)
Writes a copy of the data and keeps the previous main copy as backup.
Delete—void Delete(string storageKey)
Removes the main, temporary and backup data for a key.
Exists—bool Exists(string storageKey)
Checks the main, temporary and backup entries for a key.
List—IReadOnlyList<string> List(string prefix)
Lists loadable keys with the requested prefix.
TryReadCandidates—bool TryReadCandidates(string storageKey, List<StorageCandidate> candidates)
Adds available recovery candidates to the supplied list.
Quarantine—string Quarantine(string storageKey)
Moves the main entry to broken storage and returns a label when it exists.
Promote—void Promote(string storageKey, CandidateKind from)
Moves a temporary or backup entry to the main entry.
KeepBrokenCopy—string KeepBrokenCopy(string storageKey, byte[] data)
Stores a separate broken copy.
DeleteCompanions—void DeleteCompanions(string storageKey, bool includeBroken)
Deletes temporary and backup entries and optionally the broken copy.
SetRaw—void SetRaw(CandidateKind kind, string storageKey, byte[] data)
Sets raw bytes for a specified candidate kind. Useful for testing recovery.
TryGetRaw—bool TryGetRaw(CandidateKind kind, string storageKey, out byte[] data)
Reads a copy of raw bytes for a specified candidate kind.
HasBroken—bool HasBroken(string storageKey)
Checks whether a broken copy is present.
ListOwnedKeys—IReadOnlyList<string> ListOwnedKeys(string prefix)
Lists owned keys including broken copies.

Example

Create TryMemoryStorage.cs, attach it to a GameObject and enter Play mode.

C#
using Gley.SaveSystem;
using UnityEngine;

public class TryMemoryStorage : MonoBehaviour
{
    private void Start()
    {
        MemoryStorage storage = new MemoryStorage("type-reference-memory");
        SaveService service = new SaveService();
        service.SetStorage(storage);

        SaveResult result = service.Save(transform.position, "position");
        Debug.Log($"Memory save: {result.Status}", this);
    }
}
On This Page