Skip to content

Commit fd161e1

Browse files
authored
docs: php local evaluation caching (#2876)
1 parent a1753b1 commit fd161e1

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

docs/docs/clients/server-side.md

+51
Original file line numberDiff line numberDiff line change
@@ -793,6 +793,12 @@ flagsmith.close();
793793
flagsmith.close();
794794
```
795795

796+
</TabItem>
797+
<TabItem value="php" label="PHP">
798+
799+
Since PHP does not share state between requests, you **have** to implement caching to get the benefits of Local
800+
Evaluation mode. Please see [caching](#caching) below.
801+
796802
</TabItem>
797803
</Tabs>
798804

@@ -1572,6 +1578,51 @@ router.get('/', function (req, res, next) {
15721578
});
15731579
```
15741580

1581+
</TabItem>
1582+
<TabItem value="php" label="PHP">
1583+
1584+
```php
1585+
$flagsmith = (new Flagsmith(TOKEN));
1586+
// This will load the environment from cache (or API, if cache does not exist.)
1587+
$flagsmith->updateEnvironment();
1588+
```
1589+
1590+
It is recommended to use a psr simple-cache implementation to cache the environment document between multiple requests.
1591+
1592+
```sh
1593+
composer require symfony/cache
1594+
```
1595+
1596+
```php
1597+
$flagsmith = (new Flagsmith(TOKEN))
1598+
->withCache(new Psr16Cache(new FilesystemAdapter()));
1599+
// Cache the environment call to reduce network calls for each and every evaluation.
1600+
// This will load the environment from cache (or API, if cache does not exist.)
1601+
$flagsmith->updateEnvironment();
1602+
```
1603+
1604+
An optional cron job can be added to refresh this cache at a set time depending on your choice. Please set
1605+
EnvironmentTTL value for this purpose.
1606+
1607+
```php
1608+
// the environment will be cached for 100 seconds.
1609+
$flagsmith = $flagsmith->withEnvironmentTtl(100);
1610+
$flagsmith->updateEnvironment();
1611+
```
1612+
1613+
```sh
1614+
* * * 1 40 php index.php # using cli
1615+
* * * 1 40 curl http://localhost:8000/ # using http
1616+
```
1617+
1618+
Note:
1619+
1620+
- For the environment cache, please use the server key generated from the Flagsmith Settings menu. The key's prefix is
1621+
`ser.`.
1622+
- The cache is important for concurrent requests. Without the cache, each request in PHP is a different process with its
1623+
own memory objects. The cache (filesystem or other) would enforce that the network call is reduced to a file system
1624+
one.
1625+
15751626
</TabItem>
15761627
</Tabs>
15771628

0 commit comments

Comments
 (0)