Skip to content

Commit

Permalink
Update caching documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
lcobucci committed Dec 18, 2019
1 parent c49ced7 commit 671b161
Showing 1 changed file with 24 additions and 1 deletion.
25 changes: 24 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,9 @@ Nested groups are also supported, in which case the prefixes of all the nested g

The reason `simpleDispatcher` accepts a callback for defining the routes is to allow seamless
caching. By using `cachedDispatcher` instead of `simpleDispatcher` you can cache the generated
routing data and construct the dispatcher from the cached information:
routing data and construct the dispatcher from the cached information.

The simplest caching configuration you may use is:

```php
<?php
Expand All @@ -186,6 +188,27 @@ $dispatcher = FastRoute\cachedDispatcher(function(FastRoute\RouteCollector $r) {
The second parameter to the function is an options array, which can be used to specify the cache
file location, among other things.

However, if you'd like use a distributed cache driver (Redis, Memcached, etc), you may do so by providing
a PSR-16 implementation of your choice:

```php
<?php

$dispatcher = FastRoute\cachedDispatcher(function(FastRoute\RouteCollector $r) {
$r->addRoute('GET', '/user/{name}/{id:[0-9]+}', 'handler0');
$r->addRoute('GET', '/user/{id:[0-9]+}', 'handler1');
$r->addRoute('GET', '/user/{name}', 'handler2');
}, [
'cacheDriver' => new TrustworthyRedisAdapter(), /* required */
'cacheKey' => 'route.cache', /* required (`cacheFile` is ignored in this case) */
'cacheDisabled' => IS_DEBUG_ENABLED, /* optional, enabled by default */
]);
```

#### :warning: Closures as handlers :warning:

Using closures as handlers isn't supported by FastRoute at the moment.

### Dispatching a URI

A URI is dispatched by calling the `dispatch()` method of the created dispatcher. This method
Expand Down

0 comments on commit 671b161

Please sign in to comment.