Download the PHP package meyersm/simple-datastore without Composer
On this page you can find all versions of the php package meyersm/simple-datastore. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download meyersm/simple-datastore
More information about meyersm/simple-datastore
Files in meyersm/simple-datastore
Package simple-datastore
Short Description A PHP library to store objects between sessions using only the local file system
License
Homepage https://github.com/meyersm/simpleDatastore
Informations about the package simple-datastore
simpleDatastore
A PHP library to store objects between sessions using only the local file system.
Objects can be stored in json or serialized formats and be opened in read only mode or a read/write mode that locks the datastore to prevent overwriting or race conditions.
Good uses for simpleDatastore
- Need to preserve data between scripts/sessions and a database is overkill or not feasable
- Need to share data with another non-php script/code and json is the most convienient
- Fallback to preserve data incase a database connection failed.
This library should NOT be used as a replacement for a database in any application that has high traffic and many concurrent writes, while the files are locked while writing, every other thread/script that wants to write will have to wait for the lock causing slowdowns.
Installation
To install with composer, add this to your composer.json
Then run composer update
and add this to your code,
Alternativly you can just download the simpleDatastore.php file and add this to your code,
Usage
Creating a new Datastore:
Then you can start adding data, and save to the new datastore
The datastore still has a write lock on the file until the file is closed, at that point another script can open and read the data
If you want to delete a datastore file, use the destroy function
When you try to open a datastore that already has a lock on it, the library will re-try for a configurable amount of time then error out. If you want to access a datastore while its locked, you can open it in read only mode
Working with php classes that would be better stored as serialized objects instead of json? Set the serialize flag to true when opening a datastore
By default the library will store all datastores and thier lock files in a sub directory called "datastore". It also throws exceptions in case of an error, if you would like to change either of those defaults you can.
To change the way the library handles a locked datastore file, use the setLockConfig function. Again if doing this, delay opening the datastore file like in the example above
For more examples on how to use this library, check out the unit tests
Unit tests