momentohq / laravel-example
The Laravel weather app to showcase Momento cache driver.
Installs: 65
Dependents: 0
Suggesters: 0
Security: 0
Stars: 1
Watchers: 3
Forks: 0
Open Issues: 0
Type:project
Requires
- php: ^8.0.2
- guzzlehttp/guzzle: ^7.5
- momentohq/laravel-cache: v1.0.5
- dev-main
- v0.3.2
- v0.3.1
- v0.3.0
- v0.2.3
- v0.2.2
- v0.2.1
- v0.2.0
- v0.1.0
- dev-chore/update-example-version
- dev-release
- dev-chore/update-api-key
- dev-fix/dockerfile
- dev-feat/update-php-sdk
- dev-atomic-increment
- dev-grpc-fork-test
- dev-laravel-cache-version-bump-1-0-1
- dev-update-docker-0-2-2
- dev-chore/add-dockerfile
- dev-fix/github-wrokflows
- dev-feat/move-files-from-personal-repo
This package is auto-updated.
Last update: 2024-10-27 18:55:23 UTC
README
Momento Laravel Example
In this repo, you will see an example of how to integrate a Momento cache into your Laravel app, instead of a Redis or Memcached. What's the benefits, you ask? You don't have to worry about Redis/Memcached nodes! Intrigued? Keep on reading!
Run the app via Docker
Before building a Docker image, create .env
file in docker
directory with the following env variables:
MOMENTO_API_KEY
=<YOUR_API_KEY>MOMENTO_CACHE_NAME
=<YOUR_CACHE_NAME>WEATHER_API_KEY
this is for weather API. Check out OpenWeather to get an API key.
If you don't have a Momento API key, you can generate one using the Momento Console.
Build a Docker image for the app:
cd docker docker build --tag laravel-example .
Note: Building the laravel-example
image involves compiling the PHP gRPC extension, which will take several minutes to complete.
If you run into the API limits from GitHub, add a GitHub personal access token to your composer configuration:
export COMPOSER_AUTH=<YOUR_GITHUB_ACCESS_TOKEN> docker build --tag laravel-example --build-arg COMPOSER_AUTH=$COMPOSER_AUTH .
And run a Docker container with the image:
docker run -d --env-file .env -p 8000:8000 laravel-example
Manual PHP Setup
You will need to install the following:
Add the repository and dependency to your project's composer.json
:
{ "require": { "momentohq/laravel-example": "0.2.3" } }
Add the Momento configuration to the 'stores' section of config/cache.php
, adjusting the cache_name
and
default_ttl
parameters as needed:
'stores' => [ ... 'momento' => [ 'driver' => 'momento', 'cache_name' => 'my-momento-cache', 'default_ttl' => 60, ], ],
Update the cache driver entry in your .env
file:
CACHE_DRIVER=momento
And add the following environment variables into your .env
file:
WEATHER_API_KEY
this is for weather API. Check out OpenWeather to get an API key.MOMENTO_API_KEY
If you don't have a Momento API key, you can generate one using the Momento Console.
Run composer update
to install the necessary prerequisites.
To run this application:
php artisan serve
cURL commands
curl http://127.0.0.1:8000/api/weather/<your-favorite-city> Example: curl http://127.0.0.1:8000/api/weather/denver
or
curl http://127.0.0.1:8000/api/weather/<your-favorite-zipcode>/<country-code-such-as-us> Example: curl http://127.0.0.1:8000/api/weather/98101/us
or
curl http://127.0.0.1:8000/api/weather/id/<your-favorite-city-id> City id can be found here: http://bulk.openweathermap.org/sample/ Example: curl http://127.0.0.1:8000/api/weather/id/833
Exploring the Momento Cache Integration
Examples of different ways to use Momento's Laravel cache client and cache driver can be found in the WeatherController.php.
For examples of using the Momento client directly, check out our PHP SDK examples!
A simple example usage of Momento's Laravel cache driver is extracted below:
$apiKey = env("WEATHER_API_KEY"); $url = "https://api.openweathermap.org/data/2.5/weather?q={$city}&appid={$apiKey}"; $result = Cache::get($city); if (!is_null($result)) { return $result; } else { $res = $this->httpClient->get($url); if ($res->getStatusCode() == 200) { $json = $res->getBody(); // 10 minutes TTLc Cache::put($city, $json, 600); return $json; } }