Skip to content

Commit 1e3beb2

Browse files
committed
optimize to batch deletion alarm
1 parent 04883bf commit 1e3beb2

File tree

1 file changed

+4
-2
lines changed
  • workers/befuddle-interactions/src

1 file changed

+4
-2
lines changed

workers/befuddle-interactions/src/index.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ export class ConfigStore {
2525
// ── PUT ──
2626
if (action === "put") {
2727
const { value, ttl } = await request.json();
28+
const bucketSize = 300 * 1000;
2829
const expiresAt = Date.now() + ttl * 1000;
30+
const batchedExpiry = Math.ceil(expiresAt / bucketSize) * bucketSize;
2931

3032
this.sql.exec(
3133
`INSERT OR REPLACE INTO configs (key, value, expires_at) VALUES (?, ?, ?)`,
@@ -34,8 +36,8 @@ export class ConfigStore {
3436

3537
// Schedule an alarm to clean up after TTL
3638
const currentAlarm = await this.ctx.storage.getAlarm();
37-
if (!currentAlarm || expiresAt < currentAlarm) {
38-
await this.ctx.storage.setAlarm(expiresAt);
39+
if (!currentAlarm || batchedExpiry < currentAlarm) {
40+
await this.ctx.storage.setAlarm(batchedExpiry);
3941
}
4042

4143
return new Response("OK");

0 commit comments

Comments
 (0)