I'm running a worker locally (`wrangler dev`) and trying to access some KV keys I've set using `wran
I'm running a worker locally (
wrangler dev
) and trying to access some KV keys I've set using wrangler kv key put ... --local <key> <value>
but my <kvnamespace>.get in my worker always returns null. I tried listing them from the worker too, and there's no keys at all. Listing them with "kv key list --local" tells me they are there. I can also read them with kv key get
. Any suggestions on where I should start to dig to figure it out?13 Replies
Any example to use KV with Typescript? All examples I see, have bugs =/
find: https://blog.cloudflare.com/improving-workers-types/
What I did wrong?
Try to use Cloudflare KV, in
npm run preview
and in deploy
, get some error:
Error adding key-value pair: Error: Error adding key-value pair: ReferenceError: env is not defined
export const runtime = 'edge';
export const putKV = async (key, value) => {
try {
await env.KV_PREVIEW.put(key, value);
return;
} catch (error) {
throw new Error(Error adding key-value pair: ${error}
);
}
};
How are you invoking putKV
And where is env coming from there
(May be better to ask in #next-on-pages )
@Walshy | Deploying I dont know how to make this with TypeScript, try a lot of examples and tutorials.
But, I change to Node.JS standard code, and work.
In loca env: pass, not generate error but, not save in KVS.
Only work 100% in CF Worker.
Hey folks, in reference to the above inaccurate metrics and free tier quota exhaustion, this fix has now been rolled out!
Hi, running into some weird eventual consistency behavior with KV - when updating a KV key value pair (via the API), accessing it via worker seemingly randomly returns the old version and switches back and forth to the new updated one. Any ideas? cacheTtl used to be set to 5 minutes, but this issue has been apparently occurring for the past 24 hours
when looking at the KV dashboard, the correct version is present there, but workers still return the old version, even after several hours
Hello.
Did u solve this issue?
@Jax Please DM me the details of your KV namespace
I'm using KV to cache browser screenshots as described in this doc: https://developers.cloudflare.com/browser-rendering/get-started/screenshots/
In that doc, the
expirationTtl
for the screenshots in KV is being set to 24 hours. My ideal would be to make the expiry much longer (e.g. 1 month), but with an implementation like an LRU cache where the TTL would reset whenever the value is read, so that only keys that haven’t been read for 1 month are evicted. Is there a way to accomplish something like that with KV, or is there another product that would be better suited for that kind of implementation? I did a search in discord and found this message, which suggests that KV’s expiration TTL isn’t the way to achieve this, but then because reading a value with a cacheTtl
parameter resets any previous cacheTtl from a previous read, I thought there could be something there.Is there any plan in cloudflare to implement "kv but without cache"? I know I can use D1 / Durable Objects in some scenarios, but they both have some trade-offs, and to bypass the trade-off requires too much effort.
Why not just use R2?
A very interesting alt method. I may consider this, though I doubt whether it can handle frequently CRUD.
It can handle it a lot better then KV does. KV is eventually consistent and should only be one write per key per second
https://developers.cloudflare.com/r2/reference/consistency/ R2 is read after write/strongly consistent.
Although it may be worth mentioning: Why did you say Durable Objects has some trade offs you don't like? R2 is backed by DOs for metadata and then stores the actual objects elsewhere
For my next purpose, I want to use it for de-duplicate. Each of my ID is 128-byte, and the durable objects' size limit is 128KB, so only 1k records will exceed the limit, which will happen in less than one second. Even the durable objects' with SQLite backend can be filled with only 16k records. Since I need to store at least 7 days data, it's far from my need.