Skip to content

Commit 014d80f

Browse files
committed
docs: add readme and example
1 parent 9735336 commit 014d80f

File tree

2 files changed

+84
-0
lines changed

2 files changed

+84
-0
lines changed

README.md

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
Cache data in local files.
2+
==========================
3+
4+
Short blurb.
5+
6+
***
7+
8+
<a href="https://github.com/PhpGt/FileCache/actions" target="_blank">
9+
<img src="https://badge.status.php.gt/filecache-build.svg" alt="Build status" />
10+
</a>
11+
<a href="https://scrutinizer-ci.com/g/PhpGt/FileCache" target="_blank">
12+
<img src="https://badge.status.php.gt/filecache-quality.svg" alt="Code quality" />
13+
</a>
14+
<a href="https://scrutinizer-ci.com/g/PhpGt/FileCache" target="_blank">
15+
<img src="https://badge.status.php.gt/filecache-coverage.svg" alt="Code coverage" />
16+
</a>
17+
<a href="https://packagist.org/packages/PhpGt/FileCache" target="_blank">
18+
<img src="https://badge.status.php.gt/filecache-version.svg" alt="Current version" />
19+
</a>
20+
<a href="http://www.php.gt/filecache" target="_blank">
21+
<img src="https://badge.status.php.gt/filecache-docs.svg" alt="PHP.Gt/FileCache documentation" />
22+
</a>
23+
24+
## Example usage: get the latitude/longitude of the user's IP address
25+
26+
It's an expensive operation to make an HTTP call for every page view, but in this example we want to use a remote service to provide us with the estimated latitude/longitude of the current IP address.
27+
28+
The first time we see the IP address will have to make an HTTP call, but subsequent calls will be able to take advantage of the cache.
29+
30+
```php
31+
$ipAddress = $_SERVER["REMOTE_ADDR"];
32+
$fileCache = new Gt\FileCache\Cache("/tmp/ip-address-geolocation");
33+
34+
// This function uses file_get_contents to contact the remote server
35+
// at ipinfo.io, a costly operation. We will pass the lookup function
36+
// into the cache, so it is only called when we don't have a fresh result.
37+
$lookup = function()use($ipAddress):string {
38+
$jsonString = file_get_contents("https://ipinfo.io/$ipAddress");
39+
$obj = json_decode($jsonString);
40+
return $obj->loc;
41+
}
42+
43+
$location = $fileCache->get("lat-lon", $lookup);
44+
echo "Your location is: $location";
45+
```

example/01-latlon.php

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
/**
3+
* Two HTTP calls are in this script. The first is to get the public IP
4+
* address, the second is to look up that IP address in a geolocation database.
5+
* Both calls are costly operations, so are cached. Running this script will
6+
* output the inferred location coordinates, and a timer showing how many
7+
* seconds the operation took to complete. Running for a second time will take
8+
* close-to-zero seconds, as the data will be loaded from the cache.
9+
*
10+
* @noinspection PhpComposerExtensionStubsInspection
11+
*/
12+
require __DIR__ . "/../vendor/autoload.php";
13+
14+
$startTime = microtime(true);
15+
$fileCache = new Gt\FileCache\Cache("/tmp/ip-address-geolocation");
16+
17+
function httpJson(string $uri):object {
18+
$ch = curl_init($uri);
19+
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
20+
return json_decode(curl_exec($ch));
21+
}
22+
23+
$ipAddress = $fileCache->get("ip", function():string {
24+
return httpJson("https://ipinfo.io")
25+
->ip;
26+
});
27+
28+
$location = $fileCache->get("lat-lon", function()use($ipAddress):string {
29+
return httpJson("https://ipinfo.io/$ipAddress")
30+
->loc;
31+
});
32+
33+
echo "Your IP is: $ipAddress", PHP_EOL;
34+
echo "Your location is: $location", PHP_EOL;
35+
echo "https://www.google.com/maps/?ll=$location&z=8&q=$location", PHP_EOL;
36+
37+
echo "Time taken: ",
38+
number_format(microtime(true) - $startTime, 3), " seconds.",
39+
PHP_EOL;

0 commit comments

Comments
 (0)