-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added direct caching for cache_miss in Pusher
- Loading branch information
Showing
12 changed files
with
135 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import type * as FN from '@soketi/impl/types'; | ||
|
||
export abstract class Brain { | ||
abstract get(key: string): Promise<FN.Brain.BrainRecord['value']|null>; | ||
abstract getWithMetadata(key: string): Promise<FN.Brain.BrainRecord|null>; | ||
abstract set(key: string, value: FN.Brain.BrainRecord['value'], ttlSeconds?: number): Promise<void>; | ||
abstract has(key: string): Promise<boolean>; | ||
abstract delete(key: string): Promise<void>; | ||
|
||
async cleanup(): Promise<void> { | ||
// | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './brain'; | ||
export * from './local-brain'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import type * as FN from '@soketi/impl/types'; | ||
import { Brain } from './brain'; | ||
|
||
export class LocalBrain extends Brain { | ||
memory: Map<string, FN.Brain.BrainRecord> = new Map(); | ||
|
||
constructor() { | ||
super(); | ||
|
||
setInterval(() => { | ||
for (let [key, { ttlSeconds, setTime }] of [...this.memory]) { | ||
let currentTime = parseInt((new Date().getTime() / 1000) as unknown as string); | ||
|
||
if (ttlSeconds > 0 && (setTime + ttlSeconds) <= currentTime) { | ||
this.memory.delete(key); | ||
} | ||
} | ||
}, 1_000); | ||
} | ||
|
||
async get(key: string): Promise<FN.JSON.Value|null> { | ||
return (await this.getWithMetadata(key))?.value ?? null; | ||
} | ||
|
||
async getWithMetadata(key: string): Promise<FN.Brain.BrainRecord|null> { | ||
return this.memory.get(key) ?? null; | ||
} | ||
|
||
async set(key: string, value: FN.JSON.Value, ttlSeconds = -1): Promise<void> { | ||
this.memory.set(key, { | ||
value, | ||
ttlSeconds, | ||
setTime: parseInt((new Date().getTime() / 1000) as unknown as string), | ||
}); | ||
} | ||
|
||
async has(key: string): Promise<boolean> { | ||
return Boolean(this.memory.get(key)); | ||
} | ||
|
||
async delete(key: string): Promise<void> { | ||
this.memory.delete(key); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import { LocalBrain } from '../../src/brain'; | ||
import { describe, test, expect } from 'vitest'; | ||
|
||
describe('brain/local-brain', () => { | ||
test('basic storage', async () => { | ||
const brain = new LocalBrain(); | ||
|
||
await brain.set('test', { test: 'object' }); | ||
|
||
expect(await brain.has('test')).toBe(true); | ||
expect(await brain.get('test')).toEqual({ test: 'object' }); | ||
|
||
await brain.delete('test'); | ||
|
||
expect(await brain.has('test')).toBe(false); | ||
expect(await brain.get('test')).toBe(null); | ||
}); | ||
|
||
test('basic storage with ttl', async () => { | ||
const brain = new LocalBrain(); | ||
|
||
await brain.set('test', { test: 'object' }, 1); | ||
|
||
await new Promise((resolve) => setTimeout(resolve, 1_100)); | ||
|
||
expect(await brain.has('test')).toBe(false); | ||
expect(await brain.get('test')).toBe(null); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters