Cache All the Things! 🤑

hyper cache is a cache document store that provides a clean API to cache JSON documents.

Why use a cache?

Caches are good for all sorts of things, to take the stress off of your transactional database when performing reads, to provide sessions for authentication, to contain simple aggregate values for dashboards and metrics, and more. Cache systems are complex and a challenge to set up and maintain. hyper removes this burden, hyper makes it easy to cache.

Here is a quick peek into the hyper cache API:

Add document to the cache store

await hyper.cache.add(
  'game-1', // key
  { id: 'game-1', type: 'game', name: 'Donkey Kong'} // value
  // optional ttl - time to live
)

In this example, we add a document to the cache, by supplying a key and a value, the value must be a JSON document, the key must be a unique string. You do have the option to supply a ttl or time to live value on the cached item. This means the cached item will expire within a set amount of time.

Add document to cache store with TTL

await hyper.cache.add(
  'game-2',
  { id: 'game-2', type: 'game', name: 'Super Mario Bros'},
  '2h'
)

In this example, we add a record to the cache with a TTL of 2 hours, which means it will expire from the cache in 2 hours.

NOTE: It is important to note, that you should always treat a cache as temporary storage, not permanent storage, caches are there for you to handle system memoization so you do not have to tax your system on redundant queries.

Get a document from the cache

To get a document from the cache all you need is the key.

await hyper.cache.get('game-1')

Update document in cache

await hyper.cache.set(
  'game-1',  // key
  { id: 'game-1', type: 'game', name: 'Donkey Kong', published: '1981'} //value
)

Remove the document from the cache

await hyper.cache.remove('game-1')

Query documents in cache (almost)

Cache data is best read directly by key, but sometimes you want to get a collection of cache documents, using a pattern matching string, you can query the cache keys, for example, if I wanted to get all the keys that start with game.

await hyper.cache.query('game*')

Use Case: cache my game documents

Ok, let's provide a real use case, and one of the most powerful features of hyper is the ability to compose its services. This example will use promises to showcase how you can compose the cache API with the data API to always read from the cache when accessing a game document.

Composing Data and Cache

If you are interested in seeing how to compose the data and cache services in your application check out this video:

Summary

Hyper Cache is a simple easy to use service that can be composed with other services to create great utility and continue to keep business logic in your application tier and not in your service tier. By keeping your business logic in your application tier, you can build and maintain applications with higher velocity and reduce the amount of accidental complexity.

Try hyper today! (It's Free!)

https://hyper.io