3.1.0 • Published 10 months ago

@envelop/response-cache-redis v3.1.0

Weekly downloads
-
License
MIT
Repository
github
Last release
10 months ago

@envelop/response-cache-redis

  • Supports redis cache for @envelop/response-cache plugin
  • Suitable for serveless deployments where the LRU In-Memory Cache is not possible

Check out the GraphQL Response Cache Guide for more information

Getting Started

yarn add @envelop/response-cache
yarn add @envelop/response-cache-redis

Usage Example

In order to use the Redis cache, you need to:

  • Create a Redis database
  • Collect the connection settings (or its connection string), e.g., host, port, username, password, tls, etc.
  • Create and configure a Redis client with your connection settings and any additional options
  • Create an instance of the Redis Cache and set to the useResponseCache plugin options
import { envelop } from '@envelop/core';
import { useResponseCache } from '@envelop/response-cache';
import { createRedisCache } from '@envelop/response-cache-redis';

import Redis from 'ioredis';

/**
 * For additional Redis options to create the ioredis client
 * @see: https://github.com/luin/ioredis/blob/master/API.md#new_Redis_new
 *
 **/
const redis = new Redis({
  host: 'my-redis-db.example.com',
  port: '30652',
  password: '1234567890',
});

// or, you can also specify connection options as a redis:// URL or rediss:// URL when using TLS encryption
const redis = new Redis(("rediss://:1234567890@my-redis-db.example.com':30652");

const cache = createRedisCache({ redis });

const getEnveloped = envelop({
  plugins: [
    // ... other plugins ...
    useResponseCache({ cache }),
  ],
});

Invalidate Cache based on custom logic

import { envelop } from '@envelop/core';
import { useResponseCache } from '@envelop/response-cache';
import { createRedisCache } from '@envelop/response-cache-redis';

import { emitter } from './eventEmitter';

// we create our cache instance, which allows calling all methods on it
const redis = new Redis(("rediss://:1234567890@my-redis-db.example.com':30652");

const cache = createRedisCache({ redis });

const getEnveloped = envelop({
  plugins: [
    // ... other plugins ...
    useResponseCache({
      ttl: 2000,
      // we pass the cache instance to the request.
      cache,
    }),
  ],
});

emitter.on('invalidate', resource => {
  cache.invalidate([
    {
      typename: resource.type,
      id: resource.id,
    },
  ]);
});
3.1.0

10 months ago

3.0.1

11 months ago

3.0.0

12 months ago

2.0.7

1 year ago

2.0.6

1 year ago

2.0.8

1 year ago

2.0.5

1 year ago

2.0.3

2 years ago

2.0.2

2 years ago

2.0.4

1 year ago

2.0.1

2 years ago

2.0.0

2 years ago

1.2.0

2 years ago

1.1.0

2 years ago

1.0.2

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago

0.5.0

2 years ago

0.4.3

2 years ago

0.4.2

2 years ago

0.4.1

2 years ago

0.4.0

2 years ago

0.3.0

2 years ago

0.2.1

2 years ago

0.2.0

2 years ago

0.1.7

2 years ago

0.1.6

2 years ago

0.1.4

2 years ago

0.1.5

2 years ago

0.1.3

2 years ago

0.1.2

3 years ago

0.1.1

3 years ago

0.1.0

3 years ago