Skip to content
maxless edited this page Dec 14, 2015 · 4 revisions

Autosave Manager

The cache server has a feature for autosaving some data periodically. The check to save data will be made when notification arrives, this class does not use any threads. In fact, you can use it to run tasks periodically instead of through the cache server timer if you feel you need it. The timer tasks are running periodically in different threads and count time more predictably. On the contrary, with the autosave manager, it does everything in the same slave server thread that triggers the execution.

The autosave manager itself resides in the AutosaveManager class. The autosave controls are instances of the Autosave class. When the Autosave.updated(), a notification is called, the manager checks if enough time has passed since last save and if there has been, calls the method that is given in the constructor. Let's take a look at a complete example:

  var _listTop: List<{ id: Int, name: String }>;
  var autosave: Autosave;

  public function new(s: CacheServerTest)
    {
      super(s);

      // ...

      autosave = server.autosaveManager.create('top.users', 60, saveTop);
    }

  function saveTop()
    {
      var tmp = Lambda.array(_listTop);
      server.temp.save('top_users', tmp);
    }


  public function topAdd(id: Int, name: String)
    {
      _acquire();
      _listTop.add({ id: id, name: name });

      autosave.updated();
      _release();
    }

The autosave is created in the module constructor with the AutosaveManager.create() method. It accepts the name, time and method arguments. Note that the topAdd() method contents are locked with a module-wide mutex because any slave server thread can make the notification.

Clone this wiki locally