Skip to content

feat!: change TTL unit from seconds to milliseconds#30

Merged
flyingsquirrel0419 merged 7 commits intomainfrom
feat/ms-ttl-25
May 1, 2026
Merged

feat!: change TTL unit from seconds to milliseconds#30
flyingsquirrel0419 merged 7 commits intomainfrom
feat/ms-ttl-25

Conversation

@flyingsquirrel0419
Copy link
Copy Markdown
Owner

Summary

Convert all TTL values from seconds to milliseconds, aligning with Node.js conventions (setTimeout, Date.now(), etc.).

Breaking Changes

All public TTL options now expect milliseconds instead of seconds:

// Before (v1.x)
new MemoryLayer({ ttl: 60 })              // 60 seconds
new RedisLayer({ client: redis, ttl: 300 }) // 300 seconds

// After (v2.0)
new MemoryLayer({ ttl: 60_000 })              // 60 seconds
new RedisLayer({ client: redis, ttl: 300_000 }) // 300 seconds

Affected options: ttl, negativeTtl, staleWhileRevalidate, staleIfError, ttlJitter, refreshAhead, adaptiveTtl.step, adaptiveTtl.maxTtl, and all LayerTtlMap values.

Changes

Area Change
StoredValue freshTtlSecondsfreshTtlMs (wire format), removed * 1_000 conversion
TtlResolver Internal TTL now in milliseconds, DEFAULT_NEGATIVE_TTL_MS = 60_000
MemoryLayer Removed * 1_000 — uses ms directly for Date.now() + ttl
DiskLayer Same as MemoryLayer
RedisLayer Added Math.ceil(ttl / 1_000) for Redis EX command
MemcachedLayer Added ms → seconds conversion
Tests All 529 tests updated
Docs README, api.md, tutorial, examples, i18n translations updated

Refs #25

BREAKING CHANGE: All TTL values (ttl, negativeTtl, staleWhileRevalidate,
staleIfError, ttlJitter, refreshAhead, adaptiveTtl step/maxTtl) now expect
milliseconds instead of seconds, aligning with Node.js conventions.

- StoredValue envelope: freshTtlSeconds -> freshTtlMs (wire format change)
- TtlResolver: internal TTL now in milliseconds
- MemoryLayer/DiskLayer: removed * 1000 conversion (TTL now ms directly)
- RedisLayer: added / 1000 conversion for Redis EX command
- All tests and documentation updated

Refs #25
Comment thread src/layers/RedisLayer.ts Outdated
const normalizedKey = this.withPrefix(entry.key)
if (entry.ttl && entry.ttl > 0) {
pipeline.set(normalizedKey, payload as never, 'EX', entry.ttl)
pipeline.set(normalizedKey, payload as never, 'EX', Math.ceil(entry.ttl / 1_000))
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replace 'EX' + Math.ceil(ttl / 1000) with 'PX' + ttl for millisecond
precision. The rewrite path converts Redis TTL seconds to ms.

Suggested-by: @niksy
Comment thread src/layers/RedisLayer.ts
@flyingsquirrel0419 flyingsquirrel0419 requested review from niksy and turin-dev and removed request for niksy May 1, 2026 05:12
Comment thread src/layers/RedisLayer.ts Outdated
@@ -366,7 +366,7 @@ export class RedisLayer implements CacheLayer {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does it make sense to use PTTL here to keep it consistent with milliseconds?

https://redis.io/docs/latest/commands/pttl/

@flyingsquirrel0419 flyingsquirrel0419 merged commit 5c33598 into main May 1, 2026
2 checks passed
@niksy
Copy link
Copy Markdown

niksy commented May 1, 2026

Wanted to point out that this should have bumped package version to major (2.0.0) instead of minor (1.4.0) since it is a breaking change. Although there aren’t that many users of the package right now, you should probably take more care in the future when releasing breaking changes.

@flyingsquirrel0419
Copy link
Copy Markdown
Owner Author

flyingsquirrel0419 commented May 1, 2026

Oh my mistake . I will update my package to 2.0.0 right now! Thanks 🙏

@flyingsquirrel0419
Copy link
Copy Markdown
Owner Author

Updated !

@flyingsquirrel0419 flyingsquirrel0419 deleted the feat/ms-ttl-25 branch May 2, 2026 07:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants