Giter Site home page Giter Site logo

Comments (5)

sejori avatar sejori commented on May 20, 2024 1

https://github.com/sebringrose/peko/releases/tag/1.7.3 🎉

from peko.

navtoj avatar navtoj commented on May 20, 2024

Or.. maybe it'd be better to let the user create a suffix for the cache key by exposing the ctx on the cacher()?

Current

https://github.com/sebringrose/peko/blob/836e830e1c2daa738b8d71a12bec999bd4c5ea6b/middleware/cacher.ts#L9-L11

Additions

function parameter key suffix
id: (ctx: RequestContext) => string -${id(ctx)}

Future

export const cacher = (cache: ResponseCache, id: (ctx: RequestContext) => string): Middleware => async (ctx, next) => { 
   const reqURL = new URL(ctx.request.url) 
   const key = `${reqURL.pathname}${reqURL.search}-${JSON.stringify(ctx.state)}-${id(ctx)}`

from peko.

sejori avatar sejori commented on May 20, 2024

Excellent catch and thanks for the detailed issue.

The request method should definitely be in the CacheItem key, I think it used to be but has maybe been removed. Either that or this has been a long-standing oversight.

I also like the idea of the optional key generator argument.

Suggested fix:

export const cacher = (cache: ResponseCache, keyGen?: (ctx: RequestContext) => string): Middleware => async (ctx, next) => { 
   const key = keyGen 
       ? keyGen(ctx) 
       : `${ctx.request.method}-${ctx.request.url.pathname}${ctx.request.url.search}-${JSON.stringify(ctx.state)}`

Would you be interested in submitting a PR for this? Would be great to have you as a contributer :)

from peko.

navtoj avatar navtoj commented on May 20, 2024

Would you be interested in submitting a PR for this?

Yea, sure.

@sebringrose Are there any specific formatting guidelines for this repo?

from peko.

sejori avatar sejori commented on May 20, 2024

Basic formatting guidelines: (not enforced by linter, just habits I picked up from Deno docs)

  • only include ; semicolon where necessary
  • always use " double quotes instead of ' single quotes

You'll see requested changes on the PR. I realised we can define the default keygen function outside of the cacher for a minor performance boost.

from peko.

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.