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.
Memory Datastore Class
Start a transaction with store that requires a store-level lock
Ends (commits) a transaction – counterpart of begin
Get data from a key
Update/insert key with content
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