cache
cache ♻️
A caching service using crystal-lang + Redis with docker-compose
This service is used to cache all GraphQL responses from the main Tarkov API in order to provide maximum performance ⚡
About ⭐
This service exists to cache all response from the Tarkov API for performance and to reduce load on our cloudflare workers. It is written in crystal and is as simple as it needs to be.
This service caches requests only for a short period of time in order to keep data fresh and response times low
How it Works 📚
This service works by doing the following:
- Recieving requests to save a graphql query in its in-memory cache (redis)
- Serving requests for cached graphql queries from its in-memory cache (redis)
- Expiring cached items at a fixed interval so they can be refreshed
Traffic flow:
- Request hits the reverse proxy (caddy) - hosted on a VPS
- The request is routed to the backend caching service (this caching service here!)
- The request can either be a GET (retrieves from the cache) or a POST (saves to the cache)
- GET requests fetch data from the redis cache and POST requests save data to the redis cache
Usage 🔨
To use this repo do the following:
-
Clone the repo
-
Run the following command:
docker-compose up --build
-
Create a request to the cache endpoint to set an item in the cache:
curl --location --request POST 'http://localhost/api/cache' \ --header 'Content-Type: application/json' \ --data-raw '{ "key": "mycoolquery", "value": "fake response" }'
-
Create a request to retrieve the item you just placed in the cache:
curl --location --request GET 'http://localhost/api/cache?key=mycoolquery' \ --header 'Content-Type: application/json' \ --data-raw '{}'
-
As an added bonus, inspect your response headers to see how much longer the item will live in the cached before it expires and the request returns a 404 (
X-CACHE-TTL
)
That's it!
Extra Info 📚
Here is some extra info about the setup!
Volumes 🛢️
The docker-compose file creates one volume:
./data/redis:/data
The config volume is used to mount Redis data so it can be persisted between container restarts in production.
Contributing 🤝
To get started quickly with this project, you will need the following installed:
- crystal (crenv is suggested)
- docker compose
- bash
To get your repo setup for development do the following:
- Clone the repo
- Ensure your version of crystal matches the version in
.crystal-version
- Run the following command:
script/bootstrap
- Congrats you're ready to start developing!
- Write some code
- Run
script/accepance
to run acceptance test and ensure your changes will work - Open a pull request 🎉
cache
- 1
- 1
- 1
- 0
- 3
- 17 days ago
- June 10, 2022
MIT License
Fri, 17 May 2024 15:06:20 GMT