https://discord.cloudflare.com logo
Join Discord
Powered by
# durable-objects
  • u

    Unsmart | Tech debt

    03/29/2023, 3:29 PM
    DO reads are only faster if you are close to the DO. Otherwise KV is faster.
  • h

    HardAtWork

    03/29/2023, 3:29 PM
    Oh, like that
  • h

    HardAtWork

    03/29/2023, 3:29 PM
    I thought they meant reads within the DO
  • c

    Crazy Rabbit

    03/29/2023, 3:29 PM
    So caching to KV isn't good idea?
  • u

    Unsmart | Tech debt

    03/29/2023, 3:30 PM
    I wouldnt use KV to cache a DO value though tbh I would just use the cache api
  • d

    Dani Foldi

    03/29/2023, 3:30 PM
    Depends on request rate
  • d

    Dani Foldi

    03/29/2023, 3:30 PM
    That, too
  • h

    HardAtWork

    03/29/2023, 3:30 PM
    Do you mean caching responses from a DO, or DO Storage?
  • c

    Crazy Rabbit

    03/29/2023, 3:30 PM
    cache response from DO.
  • h

    HardAtWork

    03/29/2023, 3:30 PM
    Then maybe
  • h

    HardAtWork

    03/29/2023, 3:31 PM
    It depends on how fast your DO responds, how expensive you need, and how often it needs to update
  • c

    Crazy Rabbit

    03/29/2023, 3:31 PM
    For example if user has already voted today based on username or IP.
  • c

    Crazy Rabbit

    03/29/2023, 3:32 PM
    so I can store that he already voted in KV + Cache API and respond to them, so it doesn't need to read from DO again
  • c

    Crazy Rabbit

    03/29/2023, 3:45 PM
    Now everything works 🎉
  • c

    Crazy Rabbit

    03/29/2023, 3:45 PM
    The biggest problem that I face today is that get function for DOs actually returns undefined and not null if key does not exists
  • c

    Crazy Rabbit

    03/29/2023, 3:47 PM
    so:
    Copy code
    js
    let votes = await this.state.storage.get('votes');
    if(votes === null) return MinecraftVoteDO.jsonResponse({ 'error': 0, 'info': 'success', 'data': { 'monthly': 0, 'total': 0 } });
    return MinecraftVoteDO.jsonResponse({ 'error': 0, 'info': 'success', 'data': votes });
  • c

    Crazy Rabbit

    03/29/2023, 3:47 PM
    was returning me 500 error for so long
  • c

    Crazy Rabbit

    03/29/2023, 3:48 PM
    Basically
    votes == null
    or
    typeof(votes) === 'undefined'
    instead of
    votes === null
    would fix the issue.
  • c

    Crazy Rabbit

    03/29/2023, 3:49 PM
    KVs returns null while DOs returns undefined
  • c

    ckoeninger

    03/29/2023, 3:49 PM
    https://developers.cloudflare.com/workers/runtime-apis/durable-objects/#methods
  • c

    ckoeninger

    03/29/2023, 3:49 PM
    "The type of the returned value will be whatever was previously written for the key, or undefined if the key does not exist."
  • c

    Crazy Rabbit

    03/29/2023, 3:49 PM
    Yeah took me probably an hour to realize this issue XD
  • c

    Crazy Rabbit

    03/29/2023, 3:53 PM
    Also it is possible to put class for Durable Object on different file instead of the main one?
  • s

    Skye

    03/29/2023, 3:54 PM
    In your main file just
    export { MyClass } from './my-file'
  • c

    Crazy Rabbit

    03/29/2023, 3:54 PM
    I was doing import, so probably that's why it didn't worked thanks.
  • c

    ckoeninger

    03/29/2023, 3:58 PM
    the other starter templates linked from https://developers.cloudflare.com/workers/learning/using-durable-objects/#uploading-a-durable-object-worker have DO class in different files from the main one
  • a

    avi

    03/30/2023, 2:36 AM
    is there any 'hook' to know just before a DO goes away? like to quickly write to durable object storage API before the DO goes away
  • k

    kian

    03/30/2023, 2:39 AM
    Not that I’m aware of
  • u

    Unsmart | Tech debt

    03/30/2023, 2:41 AM
    Pretty sure they have said they won't add an API for that given it can't be reliable for example on system crashes
  • f

    four33

    03/30/2023, 2:41 AM
    Just realizing Durable Object storage has a limit of 128kb...
1...529530531...567Latest