-
Notifications
You must be signed in to change notification settings - Fork 1.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feat/upstash redis storage #2963
Merged
Merged
Changes from 9 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
316ad43
feat: upstash redis storage BaseStore
bracesproul 432e4be
nit: rename file
bracesproul 6a9de55
nit: rename class & input interface
bracesproul 3bcc3c2
nit: remove unused input
bracesproul 745c218
feat: tests
bracesproul a6c0e3b
fix: yield & tests yield
bracesproul feabb2f
Merge branch 'main' into feat/upstash-redis-storage
bracesproul 4d80366
chore: lint files
bracesproul 4dfd95f
nit
bracesproul 55a6b6a
Merge branch 'main' into feat/upstash-redis-storage
bracesproul 3fd366d
chore: added entrypoint
bracesproul a1252f1
nit: tests
bracesproul 3bec777
Merge branch 'main' into feat/upstash-redis-storage
bracesproul dabe4c1
fix: added to requiresOptionalDependency
bracesproul File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
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,102 @@ | ||
/* eslint-disable no-process-env */ | ||
|
||
import { test } from "@jest/globals"; | ||
import { Redis as UpstashRedis } from "@upstash/redis"; | ||
import { UpstashRedisStore } from "../upstash_redis.js"; | ||
|
||
const getClient = () => { | ||
if ( | ||
!process.env.UPSTASH_REDIS_REST_URL || | ||
!process.env.UPSTASH_REDIS_REST_TOKEN | ||
) { | ||
throw new Error("Missing Upstash Redis env variables."); | ||
} | ||
|
||
const config = { | ||
url: process.env.UPSTASH_REDIS_REST_URL, | ||
token: process.env.UPSTASH_REDIS_REST_TOKEN, | ||
}; | ||
|
||
return new UpstashRedis(config); | ||
}; | ||
|
||
describe("UpstashRedisStore", () => { | ||
const keys = ["key1", "key2"]; | ||
|
||
afterEach(async () => { | ||
const client = getClient(); | ||
await client.del(...keys); | ||
}); | ||
|
||
test("UpstashRedis can write & read values", async () => { | ||
const encoder = new TextEncoder(); | ||
const decoder = new TextDecoder(); | ||
|
||
const client = getClient(); | ||
const store = new UpstashRedisStore({ | ||
client, | ||
}); | ||
|
||
const value1 = new Date().toISOString(); | ||
const value2 = new Date().toISOString() + new Date().toISOString(); | ||
await store.mset([ | ||
[keys[0], encoder.encode(value1)], | ||
[keys[1], encoder.encode(value2)], | ||
]); | ||
const retrievedValues = await store.mget([keys[0], keys[1]]); | ||
|
||
const everyValueDefined = retrievedValues.every((v) => v !== undefined); | ||
expect(everyValueDefined).toBe(true); | ||
expect(retrievedValues.map((v) => decoder.decode(v))).toEqual([ | ||
value1, | ||
value2, | ||
]); | ||
}); | ||
|
||
test("UpstashRedis can delete values", async () => { | ||
const encoder = new TextEncoder(); | ||
|
||
const client = getClient(); | ||
const store = new UpstashRedisStore({ | ||
client, | ||
}); | ||
|
||
const value1 = new Date().toISOString(); | ||
const value2 = new Date().toISOString() + new Date().toISOString(); | ||
await store.mset([ | ||
[keys[0], encoder.encode(value1)], | ||
[keys[1], encoder.encode(value2)], | ||
]); | ||
|
||
await store.mdelete(keys); | ||
|
||
const retrievedValues = await store.mget([keys[0], keys[1]]); | ||
|
||
const everyValueUndefined = retrievedValues.every((v) => v === undefined); | ||
expect(everyValueUndefined).toBe(true); | ||
}); | ||
|
||
test("UpstashRedis can yield keys with prefix", async () => { | ||
const encoder = new TextEncoder(); | ||
|
||
const client = getClient(); | ||
const store = new UpstashRedisStore({ | ||
client, | ||
}); | ||
|
||
const value = new Date().toISOString(); | ||
const prefix = "prefix_"; | ||
const keysWithPrefix = keys.map((key) => `${prefix}${key}`); | ||
await store.mset(keysWithPrefix.map((key) => [key, encoder.encode(value)])); | ||
|
||
const yieldedKeys = []; | ||
for await (const key of store.yieldKeys(prefix)) { | ||
yieldedKeys.push(key); | ||
} | ||
console.log("Yielded keys:", yieldedKeys); | ||
expect(yieldedKeys.sort()).toEqual(keysWithPrefix.sort()); | ||
|
||
// afterEach won't automatically delete these since we're applying a prefix. | ||
await store.mdelete(keysWithPrefix); | ||
}); | ||
}); |
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,157 @@ | ||
import { Redis as UpstashRedis, type RedisConfigNodejs } from "@upstash/redis"; | ||
|
||
import { BaseStore } from "../schema/storage.js"; | ||
|
||
/** | ||
* Type definition for the input parameters required to initialize an | ||
* instance of the UpstashStoreInput class. | ||
*/ | ||
export interface UpstashRedisStoreInput { | ||
sessionTTL?: number; | ||
config?: RedisConfigNodejs; | ||
client?: UpstashRedis; | ||
/** | ||
* The amount of keys to retrieve per batch when yielding keys. | ||
* @default 1000 | ||
*/ | ||
yieldKeysScanBatchSize?: number; | ||
/** | ||
* The namespace to use for the keys in the database. | ||
*/ | ||
namespace?: string; | ||
} | ||
|
||
/** | ||
* Class that extends the BaseStore class to interact with an Upstash Redis | ||
* database. It provides methods for getting, setting, and deleting data, | ||
* as well as yielding keys from the database. | ||
*/ | ||
export class UpstashRedisStore extends BaseStore<string, Uint8Array> { | ||
lc_namespace = ["langchain", "storage"]; | ||
|
||
protected client: UpstashRedis; | ||
|
||
protected namespace?: string; | ||
|
||
protected yieldKeysScanBatchSize = 1000; | ||
|
||
private sessionTTL?: number; | ||
|
||
constructor(fields: UpstashRedisStoreInput) { | ||
super(fields); | ||
if (fields.client) { | ||
this.client = fields.client; | ||
} else if (fields.config) { | ||
this.client = new UpstashRedis(fields.config); | ||
} else { | ||
throw new Error( | ||
`Upstash Redis store requires either a config object or a pre-configured client.` | ||
); | ||
} | ||
this.sessionTTL = fields.sessionTTL; | ||
this.yieldKeysScanBatchSize = | ||
fields.yieldKeysScanBatchSize ?? this.yieldKeysScanBatchSize; | ||
this.namespace = fields.namespace; | ||
} | ||
|
||
_getPrefixedKey(key: string) { | ||
if (this.namespace) { | ||
const delimiter = "/"; | ||
return `${this.namespace}${delimiter}${key}`; | ||
} | ||
return key; | ||
} | ||
|
||
_getDeprefixedKey(key: string) { | ||
if (this.namespace) { | ||
const delimiter = "/"; | ||
return key.slice(this.namespace.length + delimiter.length); | ||
} | ||
return key; | ||
} | ||
|
||
/** | ||
* Gets multiple keys from the Upstash Redis database. | ||
* @param keys Array of keys to be retrieved. | ||
* @returns An array of retrieved values. | ||
*/ | ||
async mget(keys: string[]) { | ||
const encoder = new TextEncoder(); | ||
|
||
const prefixedKeys = keys.map(this._getPrefixedKey.bind(this)); | ||
const retrievedValues = await this.client.mget<Uint8Array[]>( | ||
...prefixedKeys | ||
); | ||
return retrievedValues.map((value) => { | ||
if (!value) { | ||
return undefined; | ||
} else if (typeof value === "object") { | ||
return encoder.encode(JSON.stringify(value)); | ||
} else { | ||
return encoder.encode(value); | ||
} | ||
}); | ||
} | ||
|
||
/** | ||
* Sets multiple keys in the Upstash Redis database. | ||
* @param keyValuePairs Array of key-value pairs to be set. | ||
* @returns Promise that resolves when all keys have been set. | ||
*/ | ||
async mset(keyValuePairs: [string, Uint8Array][]): Promise<void> { | ||
const decoder = new TextDecoder(); | ||
const encodedKeyValuePairs = keyValuePairs.map(([key, value]) => [ | ||
this._getPrefixedKey(key), | ||
decoder.decode(value), | ||
]); | ||
const pipeline = this.client.pipeline(); | ||
for (const [key, value] of encodedKeyValuePairs) { | ||
if (this.sessionTTL) { | ||
pipeline.setex(key, this.sessionTTL, value); | ||
} else { | ||
pipeline.set(key, value); | ||
} | ||
} | ||
await pipeline.exec(); | ||
} | ||
|
||
/** | ||
* Deletes multiple keys from the Upstash Redis database. | ||
* @param keys Array of keys to be deleted. | ||
* @returns Promise that resolves when all keys have been deleted. | ||
*/ | ||
async mdelete(keys: string[]): Promise<void> { | ||
await this.client.del(...keys.map(this._getPrefixedKey.bind(this))); | ||
} | ||
|
||
/** | ||
* Yields keys from the Upstash Redis database. | ||
* @param prefix Optional prefix to filter the keys. A wildcard (*) is always appended to the end. | ||
* @returns An AsyncGenerator that yields keys from the Upstash Redis database. | ||
*/ | ||
async *yieldKeys(prefix?: string): AsyncGenerator<string> { | ||
let pattern; | ||
if (prefix) { | ||
const wildcardPrefix = prefix.endsWith("*") ? prefix : `${prefix}*`; | ||
pattern = `${this._getPrefixedKey(wildcardPrefix)}*`; | ||
} else { | ||
pattern = this._getPrefixedKey("*"); | ||
} | ||
let [cursor, batch] = await this.client.scan(0, { | ||
match: pattern, | ||
count: this.yieldKeysScanBatchSize, | ||
}); | ||
for (const key of batch) { | ||
yield this._getDeprefixedKey(key); | ||
} | ||
while (cursor !== 0) { | ||
[cursor, batch] = await this.client.scan(cursor, { | ||
match: pattern, | ||
count: this.yieldKeysScanBatchSize, | ||
}); | ||
for (const key of batch) { | ||
yield this._getDeprefixedKey(key); | ||
} | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I actually don't think this should be in the codebase at all?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This stack overflow answer is saying it's good practice to include a couple, for sharing certain settings across devs.