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

Temp Directory

If you don't want to create yet another database table containing a single row with some data that needs to be persistent the cache server has the API to work with the local temp directory. The class snipe.lib.Temp has two methods: load() and save(). The save() method automatically serializes the given object into JSON-encoded string and, vice versa, the load() method reads the JSON-encoded string from the given file and returns a parsed object. You can give the save() method a time argument. In that case the object will be saved to the file only if that amount of seconds has passed since last save.

The save() and load() methods are thread-safe.

Let's take a look at the example:

  var _listTop: Array<{ id: Int, name: String }>;

  override function loadTables()
    {
      _listTop = server.temp.load('top_users');

      // ...
    }

  public function addTop(id: Int, name: String)
    {
      _acquire();

      _listTop.push({ id: id, name: name });
      server.temp.save('top_users', _listTop, 60);

      _release();
    }

In this example we use the loadTables() module hook to load the file "top_users" from the temp directory and store its contents as an array. When the addTop() method is called, the new user is pushed into the top users array and then Temp.save() is called. If 60 seconds have passed since the last save, the top users list will be saved into the file.

Clone this wiki locally