Skip to content

Commit

Permalink
getTtl()
Browse files Browse the repository at this point in the history
  • Loading branch information
gerold-penz committed Aug 12, 2024
1 parent 8ebe4a5 commit 7b039ba
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 7 deletions.
30 changes: 29 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -856,7 +856,7 @@ store.db.transaction(() => {
```


## Renew TTL
## Set TTL (renew)

```typescript
setTtl(key: string, ttlMs?: number): boolean
Expand Down Expand Up @@ -894,6 +894,34 @@ store.setTtl("my-key", 0) // --> true
```


## Get TTL

```typescript
getTtl(key: string): number | undefined
```

Returns how long the data record is still valid (in milliseconds).
Returns `undefined` if the `key` does not exist or no expiration date has been set.

Inspired by: https://docs.keydb.dev/docs/commands/#ttl

### key

The key must be a string.

### Example

```typescript
import { BunSqliteKeyValue } from "bun-sqlite-key-value"
const store = new BunSqliteKeyValue()
store.set("my-key", "my-value", 20000)
await Bun.sleep(1)
store.getTtl("my-key") // --> 19999
```


## Hash (Map Object) - Write Value
```typescript
hSet(key: string, field: string, value: any, ttlMs?: number)
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "bun-sqlite-key-value",
"version": "1.10.1",
"version": "1.10.2",
"author": {
"name": "Gerold Penz",
"email": "[email protected]",
Expand Down
24 changes: 19 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ export class BunSqliteKeyValue {
private getRandomKeyStatement: Statement<Omit<Record, "value" | "expires">>
private getRandomItemStatement: Statement<Omit<Record, "expires">>
private renameStatement: Statement
private setTtlStatement: Statement
private setExpiresStatement: Statement
private getExpiresStatement: Statement<{expires: number}>


// - `filename`: The full path of the SQLite database to open.
Expand Down Expand Up @@ -150,7 +151,8 @@ export class BunSqliteKeyValue {
LIMIT 1
)`)
this.renameStatement = this.db.query("UPDATE items SET key = $newKey WHERE key = $oldKey")
this.setTtlStatement = this.db.query("UPDATE items SET expires = $expires WHERE key = $key")
this.setExpiresStatement = this.db.query("UPDATE items SET expires = $expires WHERE key = $key")
this.getExpiresStatement = this.db.query("SELECT expires FROM items WHERE key = $key")

// Delete expired items
this.deleteExpired()
Expand Down Expand Up @@ -616,13 +618,25 @@ export class BunSqliteKeyValue {
if (ttlMs !== undefined && ttlMs > 0) {
expires = Date.now() + ttlMs
}
return this.setTtlStatement.run({key, expires}).changes === 1
return this.setExpiresStatement.run({key, expires}).changes === 1
}


// ToDo: ttl() milliseconds like pTtl()
// Returns how long the data record is still valid (in milliseconds).
// Returns `undefined` if the key does not exist.
// Inspired by: https://docs.keydb.dev/docs/commands/#ttl
// Inspired by: https://docs.keydb.dev/docs/commands/#pttl
getTtl(key: string): number | undefined {
const record = this.getExpiresStatement.get({key})
if (!record) return
const expires = record?.expires
if (!expires) return
const now = Date.now()
if (expires < now) {
this.delete(key)
return
}
return expires - now
}


// Do not use it with several large amounts of data or blobs.
Expand Down
14 changes: 14 additions & 0 deletions tests/memory.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -610,6 +610,20 @@ test("setTtl() with global defined TTL", async () => {
})


test("getTtl()", async () => {
const store = new BunSqliteKeyValue()

store.set(KEY_1, STRING_VALUE_1)

expect(store.getTtl(KEY_1)).toBeUndefined()
expect(store.getTtl(KEY_2)).toBeUndefined()

store.setTtl(KEY_1, 20000)
await Bun.sleep(1)
expect(store.getTtl(KEY_1)).toBeLessThanOrEqual(19999)
})


test("hSet(), hGet()", async () => {
const store = new BunSqliteKeyValue()

Expand Down

0 comments on commit 7b039ba

Please sign in to comment.