Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Paginate returns the same results #63

Open
Sicria opened this issue Jan 14, 2019 · 1 comment
Open

Paginate returns the same results #63

Sicria opened this issue Jan 14, 2019 · 1 comment

Comments

@Sicria
Copy link

Sicria commented Jan 14, 2019

The following two finds returns different results but when cached, will return the first cached result.

const roles = await app.service('role').find({
    paginate: false,
    query: {
      enabled: true,
    },
  });
const roles = await app.service('role').find({
    paginate: true,
    query: {
      enabled: true,
    },
  });

I think that certain params should be taken into account when generating the cache key.

@Sicria
Copy link
Author

Sicria commented Jan 15, 2019

For now I'm using this hook which appears to do the job.

/**
 * Caches a service via Redis
 * Also ensures that certain params are included in the query to be parsed into the cache key
 *
 * @returns {function(): *[]}
 */
export default function cacheBefore() {
  return iff(
    context => context.type === 'before' && includes(['get', 'find'], context.method),
    // Store params in the query to parse a cache key inside redisBeforeHook
    context => ({
      ...(context || {}),
      params: {
        ...get(context, 'params', {}),
        query: {
          ...get(context, 'params.query', {}),
          // Prevent paginated & non-paginated results from using the same key
          $paginate: get(context, 'params.paginate', true),
        },
      },
    }),
    // Handle the caching via redis
    redisBeforeHook({ immediateCacheKey: true }),
    // Remove the params which were added to the query
    discardQuery('$paginate'),
  );
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant