8. Persistence

Persistence describes the ability for the plugins to persist data even if Errbot is restarted.

8.1. How to use it

You plugin is the store, simply use self as a dictionary.

Here is a simple example storing are retreiving value from the store.

from errbot import BotPlugin, botcmd

class PluginExample(BotPlugin):
    @botcmd
    def remember(self, msg, args):
        self['TODO'] = args

    @botcmd
    def recall(self, msg, args):
        return self['TODO']

8.2. Caveats

The storing occurs when you assign the key. So for example:

# THIS WON'T WORK
d = {}
self['FOO'] = d
d['subkey'] = 'NONONONONONO'

You need to do that instead: (manual method)

# THIS WORKS
d = {}
self['FOO'] = d

# later ...
d['subkey'] = 'NONONONONONO'
self['FOO'] = d  # restore the full key if something changed in memory.

Or use the mutable contex manager:

# THIS WORKS AND IS CLEANER
d = {}
self['FOO'] = d

# later ...

with self.mutable('FOO') as d:
    d['subkey'] = 'NONONONONONO'
# it will save automatically the key