DacheQL — Dashingly fast caching for your GraphQL queries

Andrew Moy
5 min readOct 12, 2022

--

DacheQL Version 1.0.0 is now released!

DacheQL is an open-source developer tool that leverages the pinpoint accuracy of GraphQL’s queries and implements caching to improve your website’s query efficiency

What is GraphQL?

GraphQL was released by Facebook in 2015

GraphQL is a query language for APIs and a runtime for fulfilling those queries with your existing data. GraphQL provides a complete and understandable description of the data in your API, gives clients the power to ask for exactly what they need and nothing more, makes it easier to evolve APIs over time, and enables powerful developer tools.

With GraphQL, you can specify what data you want to request from your API. You will get back exactly what you need, nothing more and nothing less. GraphQL queries always return predictable results.

What is a cache and why should you use it?

To put it simply, a cache can be defined as a high-speed data storage that stores a subset of data. This is so that future requests for that data are served up faster than by accessing the data’s primary storage location. But why should we use a cache? Does it really matter? Yes it does!

79% of customers dissatisfied with a site’s load speed are less likely to return

1 in 4 visitors will leave the site if it takes more than 4 seconds to load

Even just a one second difference can result in 7% decline in conversions

Performance matters.

This is where DacheQL comes in!

With DacheQL, you can automatically cache your GraphQL queries with some simple setup. If you are using DacheQL for the first time, please visit our website at http://dacheql.com/ to get a better understaning of DacheQL.

If you are ready to get started with DacheQL, please refer to our documentation!

Installation and Setup

If you have not installed dacheql run the following command to install DacheQL into your application:
npm install dacheql

Now that you’ve installed DacheQL, in your server file you will have to require our middleware to handle GraphQL requests using the CommonJS format:
const dacheql = require(‘dacheql’);

Using DacheQL with Redis

DacheQL can be used two ways. The first way to use DacheQL is to use it with Redis, an open-source in memory data store that can be use for client side caching. You will want to make sure that you have Redis already installed on your computer. If you have not done so and wish to use Redis, please refer to the Redis documentation here.

Redis is an in memory data structure store

Once your Redis client has been connected, simply pass it into our DacheQL function as follows:
app.use(‘/graphql’, dacheQL({ redis } = {<redis: client>}, capacity, endpoint, TTL), expressGraphQL({ schema: schema, graphiql: true, }));

Simply replace <redis: client> with redis: *name of your client*

Since we are using Redis, the second parameter capacity does not matter as this is used for our LRU cache. You can simply replace capacity with something arbitrary like 50.

The third parameter is the endpoint where you are actually using GraphQL. This endpoint may be something like http://localhost:3000/graphql

Our last parameter TTL is for the time to live in the Redis cache. Simply pass in the time you want the query to last in the cache for in seconds. (Example: 50)Now you are good to use cache your GraphQL responses using Redis!

Using DacheQL with our custom LRU eviction policy cache

If you are not using Redis caching, DacheQL provides a middleware for caching using the server’s memory with our custom cache that behaves with an LRU eviction policy. The arguments you should input for this middleware are as follows:

For the first parameter, since you are not using Redis, simply pass in an empty object {}like so.

Next is the capacity you would like your cache to hold. This capacity refers to when our cache will begin evicting items. For example, if you set the capacity to 50, it will evict an item upon the 51st unique query. It should be noted that if you pass in a non-whole number, it will be rounded down for you. Non integers, negative numbers, and capacities below two will default to simply creating a GraphQL fetch without storing values in the cache.

The third parameter is the endpoint at which you are actually using GraphQL. For example, this may be http://localhost:3000/graphql.

Our last parameter is the Time to Live, or how long you want this specific query to last in your cache for. Since we aren’t using Redis here, just pass in anything for your TTL as our cache is not reliant on this information. Now you are good to cache your GraphQL responses with our custom LRU eviction policy!

Improved performance with DacheQL

How to contribute

If you wish to contribute to DacheQL, please visit DacheQL on GitHub!

Closing Thoughts

In a fast paced technical environment, performance speed for websites matters more now than ever. DacheQL simplifies the caching process of your GraphQL queries in order for the devloper to increase their application performance. If you enjoyed our easy to use NPM package and this article, be sure to clap the article, follow the team on Linkedin and Github, and star our DacheQL GitHub!

DacheQL Team

Andrew Moy @ Linkedin | Github
ChunHao Zheng @ Linkedin | Github
Ethan Chuang @ Linkedin | Github
Sandy Liu @ Linkedin | Github

--

--