DEV Community

Cover image for I built my first NPM package - it's a cache ⭐
tq-bit
tq-bit

Posted on • Updated on

I built my first NPM package - it's a cache ⭐

Note: Please don't use this module for productive projects just yet

With that out of the way:

The idea to this module came to me when developing my solution for the AppWrite hackathon. I had to handle a lot of refresh-calls just to keep my local and remote state in sync. So I placed the question:

Can I securely keep my local app state in sync with a remote server without executing quite a lot of GET - requests?

Which loosely translates to the project order:

Build a cache that's able to keep several of its items in sync if necessary AND stays out the way of your HTTP client

Here it is:

GitHub logo tq-bit / qache

Zero-dependency, lightweight caching module for Node.js and the browser

Qache

Zero-dependency, lightweight caching module for Node.js and the browser - built with Typescript

License Size npm lgtm code quality GitHub last commit nycrc config on GitHub

Table of contents

About The Project

Qache at its core is a simple key-value store. Its USP lies in the ability to keep collections of items in sync with single entries. The feature was heavily inspired by Apollo's caching system. I also added a validation system to prevent faulty values from sneaking in.

The module's structured like this:

I built Qache as a secure and lightweight HTTP cache for my Vue apps. It can also be used on the…

To elaborate a bit further:

  • You create an in-memory cache for each API / DB resource
  • If there are related entries (= Array items) in the cache, Qache will try and update them by an identifier property

As a small extra, Qache comes with a built-in validator + type checking:

  • You can enable validation for a cache instance and pass a Typescript generic to the class constructor
  • Each cache is assigned a schema the first time an entry is added OR when the instance is created
  • When a new entry is added to the cache, it's validated against this schema
  • If validation fails, the entry will not be added

The validation approach is not very sophisticated (yet). In the coming weeks, I'll spend some time to research and implement a more efficient algorithm.

Surely, it's not a module that world's been waiting for, but I've learned quite a lot along the way:

  • You can increment your project's version with npm version patch|minor|major
  • Configuring linting and testing in the beginning of a module project goes a long way
  • Implementing caching isn't as hard as it seemed at first (or I did something terribly wrong here)

I'd be happy if the one or another person found this module useful. I also appreciate suggestions, feedback and ideas on how I can make Qache better :-) Check out the repos or give the module a shot. I'll add further descriptions in the upcoming days.

Top comments (0)