You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/configuration.md
+49-7Lines changed: 49 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -26,6 +26,12 @@ Example:
26
26
public $handlers = ['database'];
27
27
```
28
28
29
+
### Deferred writes
30
+
31
+
Handlers like `database` and `file` support deferred writes. When `deferWrites` is enabled, multiple `set()` and `forget()` calls
32
+
are batched and persisted efficiently at the end of the request during the `post_system` event. This minimizes the number of
33
+
database queries or file I/O operations, improving performance for write-heavy operations.
34
+
29
35
### Multiple handlers
30
36
31
37
Example:
@@ -44,6 +50,8 @@ This configuration will:
44
50
45
51
Only handlers marked as `writeable => true` will be used when calling `set()`, `forget()`, or `flush()` methods.
46
52
53
+
---
54
+
47
55
## DatabaseHandler
48
56
49
57
This handler stores settings in a database table and is production-ready for high-traffic applications.
@@ -54,21 +62,38 @@ This handler stores settings in a database table and is production-ready for hig
54
62
*`table` - The database table name for storing settings. Default: `'settings'`
55
63
*`group` - The database connection group to use. Default: `null` (uses default connection)
56
64
*`writeable` - Whether this handler supports write operations. Default: `true`
65
+
*`deferWrites` - Whether to defer writes until the end of request (`post_system` event). Default: `false`
57
66
58
67
Example:
59
68
60
69
```php
61
70
public $database = [
62
-
'class' => DatabaseHandler::class,
63
-
'table' => 'settings',
64
-
'group' => null,
65
-
'writeable' => true,
71
+
'class' => DatabaseHandler::class,
72
+
'table' => 'settings',
73
+
'group' => null,
74
+
'writeable' => true,
75
+
'deferWrites' => false,
66
76
];
67
77
```
68
78
69
79
!!! note
70
80
You need to run migrations to create the settings table: `php spark migrate -n CodeIgniter\\Settings`
71
81
82
+
**Deferred Writes**
83
+
84
+
When `deferWrites` is enabled, multiple `set()` or `forget()` calls are batched and persisted in a single transaction at the end of the request. This significantly reduces database queries:
85
+
86
+
```php
87
+
// With deferWrites = false: 1 SELECT (hydrates all settings for context) + 3 separate INSERT/UPDATE queries
88
+
$settings->set('Example.prop1', 'value1');
89
+
$settings->set('Example.prop2', 'value2');
90
+
$settings->set('Example.prop3', 'value3');
91
+
92
+
// With deferWrites = true: 1 SELECT + 1 INSERT batch + 1 UPDATE batch (all batched at end of request)
93
+
```
94
+
95
+
The deferred approach is especially beneficial when updating existing records or performing many operations in a single request.
96
+
72
97
---
73
98
74
99
## FileHandler
@@ -80,20 +105,37 @@ This handler stores settings as PHP files and is optimized for production use wi
80
105
*`class` - The handler class. Default: `FileHandler::class`
81
106
*`path` - The directory path where settings files are stored. Default: `WRITEPATH . 'settings'`
82
107
*`writeable` - Whether this handler supports write operations. Default: `true`
108
+
*`deferWrites` - Whether to defer writes until the end of request (`post_system` event). Default: `false`
83
109
84
110
Example:
85
111
86
112
```php
87
113
public $file = [
88
-
'class' => FileHandler::class,
89
-
'path' => WRITEPATH . 'settings',
90
-
'writeable' => true,
114
+
'class' => FileHandler::class,
115
+
'path' => WRITEPATH . 'settings',
116
+
'writeable' => true,
117
+
'deferWrites' => false,
91
118
];
92
119
```
93
120
94
121
!!! note
95
122
The `FileHandler` automatically creates the directory if it doesn't exist and checks write permissions on instantiation.
96
123
124
+
**Deferred Writes**
125
+
126
+
When `deferWrites` is enabled, multiple `set()` or `forget()` calls to the same class are batched into a single file write at the end of the request. This significantly reduces I/O operations:
127
+
128
+
```php
129
+
// With deferWrites = false: 1 file read (hydrates all settings for class) + 3 separate file writes
130
+
$settings->set('Example.prop1', 'value1');
131
+
$settings->set('Example.prop2', 'value2');
132
+
$settings->set('Example.prop3', 'value3');
133
+
134
+
// With deferWrites = true: 1 file read + 1 file write (all changes batched at end of request)
135
+
```
136
+
137
+
The deferred approach is especially beneficial when updating multiple properties in the same class.
0 commit comments