Autosave

Note:

This feature requires the Altruist Engine!

Autosaving in Altruist

To use this feature first enable the engine!

Overview

The Autosaving system in Altruist is designed to automatically persist the state of your entities at regular intervals or in real-time. This ensures that your data is saved without needing to manually trigger saves or worry about data consistency across cache and database layers.

There are two main types of autosave portals provided by the framework:

  • RealtimeAutosavePortal<Keyspace>: Saves entities in real-time, using the Altruist engine.
  • PeriodicAutosavePortal<Keyspace>: Saves entities at defined intervals, using cron-based scheduling.

Key Concept: Keyspace

Both autosave portals require a keyspace, which defines where to store data in the database. This is the logical namespace for saving your models, ensuring that your data is categorized and saved into the correct location.

  • The realtime autosave leverages the engine to automatically save data in real-time.
  • The periodic autosave uses a cron job to trigger saves at defined intervals.

Getting Started

To enable autosaving in your system, you'll need to extend either RealtimeAutosavePortal<Keyspace> or PeriodicAutosavePortal<Keyspace>, depending on your requirements. The key part of configuring the autosave is defining what entities should be persisted.

Implementing Autosave

The only thing the user needs to define is the GetPersistedEntities method. This method returns a list of IModel types that need to be persisted. IModel is an interface that defines entities that can exist in both the cache and the database.

public abstract List<Type> GetPersistedEntities()
{
    return new List<Type>
    {
        typeof(Player),
        typeof(Spaceship),
        // Add more models as needed
    };
}

Cache-Based Saving

  • All entities are first stored in cache before being saved to the database
  • The autosave process saves the actual state of the cache into the database based on model definitions

This means that the data in the cache is continuously synchronized with the database to ensure consistency.

How It Works

RealtimeAutosavePortal:

  • Automatically saves the entities to the database in real-time, ensuring that changes are reflected as soon as they occur.
  • This is ideal for use cases where data should always be in sync with the database, such as for critical game entities.

PeriodicAutosavePortal:

  • Saves the data at fixed intervals (e.g., every minute or hour) using cron-based scheduling.
  • Ideal for less critical data or when you need to batch updates together.

Benefits of Autosaving

  • Automated Data Persistence: You no longer need to manually trigger save operations. The system handles everything for you, reducing the chance of data loss.

  • Improved Performance: By saving data either in real-time or periodically, autosaving ensures that data is kept in sync without needing to poll the database frequently.

  • Seamless Integration: Autosaving works seamlessly with the cache system, meaning that your cache state is always reflected in the database without additional complexity.

Example: Using Realtime Autosave

public class SpaceshipAutosavePortal : AltruistRealtimeAutosavePortal<Spaceship>
{
    protected SpaceshipAutosavePortal(IPortalContext context, VaultRepositoryFactory vaultRepository, RealtimeSaveStrategy saveStrategy, IAltruistEngine engine, ILoggerFactory loggerFactory)
        : base(context, MyDBToken.Instance, vaultRepository, loggerFactory) {

        }

    public override List<Type> GetPersistedEntities()
    {
        return new List<Type> { typeof(Spaceship) };
    }
}

Note:

The portal will save data into database indentified by MyDBToken.Instance

Autosave System in Action

With this setup, the system will continuously track changes to the Spaceship entity in the cache, and it will automatically save the state of the entity to the database whenever the system triggers the save event.

Conclusion

The Autosaving system in Altruist provides a simple yet powerful way to ensure that your entities are always persisted to the database without manual intervention. Whether you need real-time saving or periodic cron-based saves, the framework has you covered. By using the AltruistAutosavePortal, you can focus on your game logic and leave data persistence to the framework.

Choose RealtimeAutosavePortal for instant data synchronization or PeriodicAutosavePortal for scheduled saves based on your use case.

Stay tuned for more autosaving features and improvements coming soon!