POC GraphQL Cache

GitHub top language Github last commit

This project is a Proof of Concept of a custom cache of GraphQL.

I want an in-memory cache of GraphQL server with custom memory consume to prevent to consume all server's memory.

Actually Apollo Server has a server side caching, but without custom memory consume's configuration. So I've done this POC to create a GraphQL server with custom cache.

The custom cache gets the method, arguments and authorization from the request and creates a unique hash as a cache key. In the first call, the result is saved in cache.

Every call made between the interval of the first call and the cache max age with the same combination method, arguments and authorization receives the cached result.

After the cache max age expires, the cache key is invalidated. In the next call, the cache is renovated.

To control the memory consume, you can config the cache's maximum keys.

 

🚀 Technologies

Tech: JS Tech: GraphQL Tech: Express Tech: Apollo Server Express Tech: Lodash Tech: Node Cache

 

🏁 Getting started

# Clone this repository
$ git clone https://github.com/juliaramosguedes/poc-graphql-cache.git

# Go into the repository
$ cd poc-graphql-cache

# Install dependencies
$ yarn install

# Run server
$ yarn start

 

🔑 How to use

Englobe the callback in resolvers that you want to cache with the cache function, passing the time in seconds of cache's max age in the second argument.

Example:

listBooks: cache((parent, data, { dataSources }) => {
      console.log('listBooks called');
      return dataSources.book.list();
    }, 360),
    
// PS: 360 is the cache's max age

Set the max keys' cache at cache/index file.

const myCache = new NodeCache({ maxKeys: 10, checkperiod: 60, stdTTL: 0 });

 

✨ Give a star

If you like this project or if it helped you in any way, how about giving it a star?

 

👩‍💻 Author

Julia Ramos

www.juliaramos.dev

Email: juliaramos Github: /juliaramosguedes Linkedin: /juliaramosguedes Medium: @juliaramosguedes