Git Product home page Git Product logo

Comments (7)

lukeed avatar lukeed commented on September 23, 2024 3

With the changes in the next release, the above example can be consolidated into this:

import { Router } from 'worktop';
import * as Cache from 'worktop/cache';

const API = new Router;
// ...

// reusable function
const main = Cache.reply(API.run);

addEventListener('fetch', event => {
  let request = event.request;

  if (request.method !== 'POST') {
    return main(event);
  }

  let clone = request.clone();
  let key = toBodyHash(clone); // string

  // use new `key` for cache lookup
  return main(event, key);
});

from worktop.

lukeed avatar lukeed commented on September 23, 2024 1

There will be a req.cachekey (name tbd) that allows you to customize the Cache's keyname to be used.

Until then, all the Cache methods already support custom keys w/ their final request?: string|Request parameter. A string is treated as a GET request.

So you may want to do something like:

import { Router } from 'worktop';
import * as Cache from 'worktop/cache';

const API = new Router;
// ...

addEventListener('fetch', event => {
  let request = event.request;

  if (request.method !== 'POST') {
    return Cache.reply(API.run)(event);
  }

  let clone = request.clone();
  let key = toBodyHash(clone); // string

  // basically the same as Cache.reply, but w/ custom request
  event.respondWith(
    Cache.lookup(event, key).then(prev => {
      return prev || handler(event).then(res => {
        return save(event, res, key);
      });
    })
  );
});

Looking at this, Cache.reply should probably take an optional request? parameter.

from worktop.

lukeed avatar lukeed commented on September 23, 2024 1

I suppose it can be today 🙇 I was gunna get JWT stuff in, but I didn't have any time last week or over the weekend.
I'll merge in #54 and cut a new minor version later today

from worktop.

lukeed avatar lukeed commented on September 23, 2024 1

@dhrubabasu released 0.7.0 btw~!

from worktop.

lukeed avatar lukeed commented on September 23, 2024 1

With the latest changes in worktop@next, this snippet (#53 (comment)) changes a little bit.

Now Cache.reply returns a Module.Worker definition (object), and there's no method that does the lookup -> save sequence for you, but it's pretty straightforward & it's more configurable now too:

import { Router } from 'worktop';
import * as Cache from 'worktop/cache';

const API = new Router;
// ...

async function reply(req, ctx, custom) {
  let res = await Cache.lookup(custom || req);
  if (res) return res;
  
  res = await API.run(req, ctx);
  return Cache.save(custom || req, res);
} 

export default {
  fetch(req, env, ctx) {
    ctx.bindings = env;

    if (req.method === 'POST') {
      let hash = toBodyHash(clone); // string
      return reply(req, ctx, hash);
    }

    return reply(req, ctx);
  }
}

from worktop.

dhrubabasu avatar dhrubabasu commented on September 23, 2024

I've tried my hand at implementing this in worktop/cache but running into some issues hashing the request body since I consume it in a handler.

from worktop.

dhrubabasu avatar dhrubabasu commented on September 23, 2024

@lukeed When the next release will be on npm?

from worktop.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.