feat: deferred writes #154
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
This PR introduces deferred writes for both the
DatabaseHandlerandFileHandler. This feature is optional and can be enabled or disabled via the config setting.Closes #8
DatabaseHandler
Multiple calls to
set()orforget()within a single request are now deferred and executed in a single database transaction at the end of the request.set()calls are grouped intooneupsertBatchupdateBatchand/orinsertBatchquery.forget()calls are grouped into oneDELETEquery.This significantly reduces the number of database operations per request.
FileHandler
Multiple
set()orforget()calls are now batched into a single file write, performed once at the end of the request.Each file will be touched only once, substantially reducing I/O operations.
Implementation details
Deferred writes are currently executed during the
post_systemevent.This approach has some drawbacks - database queries (
upsertinsert/updateanddelete) will not be visible in the Debug Toolbar.I'm open to switching this to a Filter-based implementation, though this would require users to manually register the filter in the
$requiredarray - just before thetoolbarfilter.Version and migration notes
The minimum required CodeIgniter version has been increased to4.3, as this version introduces support for:Adding indexes to existing table schemasTheupsertBatch()methodSince this feature requires a database schema change (adding a unique index to support upserts), it would be appropriate to release this as v3 of the library once (if) merged.EDIT:
As it turned out, the
nullvalue (used for context) is not treated as unique by most database engines, so the entire idea of using upsert fell through.Some good and bad things came out of this.
Let's start with the good ones:
And the bad part:
I've also added more tests to ensure that deferred writes don’t create any duplicate entries.
Checklist: