wille.memorystore

memorystore.py - Simple thread-safe in-memory key-value datastore

Usage examples

Start by importing memorystore module:

>>> import memorystore

Initiate a new memorystore and get a handler to it:

>>> store = memorystore.get_handler('store01')

Insert/update variable:

>>> store.update('message', 'Hello World!')
True

Read variable

>>> print(store.get('message'))
Hello World!

Switching to a new store is just a matter of getting a new handler

>>> store = memorystore.get_handler('store02')

A store with a different name is of course now empty:

>>> print(store.get('message'))
None

There is also support for store-level transactions. By starting a store-level transaction, you can be make sure that no other thread or process is using the same store during the transaction.

Start a transaction by calling:

>>> store.begin()
True

Do updates etc. as required:

>>> print (store.update('message', 'Doing a transaction'))
True

Finalise transaction by calling commit:

>>> store.commit()
True

No support for rollback is available at the moment.

You can also store Python primitives, such as objects, but they may cause concurrency problems if care is not taken.

class wille.memorystore.MemoryDatastore(name)

Memory Datastore Class

begin()

Start a transaction with store that requires a store-level lock

commit()

Ends (commits) a transaction – counterpart of begin

get(key)

Get data from a key

update(var, content)

Update/insert key with content

wille.memorystore.get_handler(store_name)

Acquire read/write access to a MemoryDatastore. Initiates datastore if it didn’t exist before

Arguments: store_name - Name of store (will be converted to string)

Returns: An instance of MemoryDatastore

Previous topic

wille.commandline

Next topic

wille.utils

This Page