@mastra/redis 1.0.2 → 1.1.0-alpha.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -1,2 +1,3 @@
1
1
  export * from './storage/index.js';
2
+ export { RedisServerCache, type RedisClient, type RedisServerCacheOptions, upstashPreset, nodeRedisPreset, } from './cache.js';
2
3
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,cAAc,WAAW,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AACA,cAAc,WAAW,CAAC;AAG1B,OAAO,EACL,gBAAgB,EAChB,KAAK,WAAW,EAChB,KAAK,uBAAuB,EAC5B,aAAa,EACb,eAAe,GAChB,MAAM,SAAS,CAAC"}
package/dist/index.js CHANGED
@@ -4,6 +4,7 @@ import { MemoryStorage, TABLE_THREADS, TABLE_MESSAGES, TABLE_RESOURCES, ensureDa
4
4
  import crypto2 from 'crypto';
5
5
  import { saveScorePayloadSchema } from '@mastra/core/evals';
6
6
  import { createClient } from 'redis';
7
+ import { MastraServerCache } from '@mastra/core/cache';
7
8
 
8
9
  // src/storage/domains/memory/index.ts
9
10
  function getKey(tableName, keys) {
@@ -1789,7 +1790,105 @@ var RedisStore = class extends MastraStorage {
1789
1790
  return `redis://${config.host}:${config.port || 6379}/${config.db || 0}`;
1790
1791
  }
1791
1792
  };
1793
+ var defaultSetWithExpiry = (client, key, value, seconds) => {
1794
+ return client.set(key, value, "EX", seconds);
1795
+ };
1796
+ var defaultScanKeys = (client, cursor, pattern, count) => {
1797
+ return client.scan(cursor, "MATCH", pattern, "COUNT", count);
1798
+ };
1799
+ var RedisServerCache = class extends MastraServerCache {
1800
+ client;
1801
+ keyPrefix;
1802
+ ttlSeconds;
1803
+ setWithExpiry;
1804
+ scanKeys;
1805
+ constructor(config, options = {}) {
1806
+ super({ name: "RedisServerCache" });
1807
+ this.client = config.client;
1808
+ this.keyPrefix = options.keyPrefix ?? "mastra:cache:";
1809
+ this.ttlSeconds = options.ttlSeconds ?? 300;
1810
+ this.setWithExpiry = options.setWithExpiry ?? defaultSetWithExpiry;
1811
+ this.scanKeys = options.scanKeys ?? defaultScanKeys;
1812
+ }
1813
+ getKey(key) {
1814
+ return `${this.keyPrefix}${key}`;
1815
+ }
1816
+ serialize(value) {
1817
+ return JSON.stringify(value);
1818
+ }
1819
+ deserialize(value) {
1820
+ if (typeof value === "string") {
1821
+ try {
1822
+ return JSON.parse(value);
1823
+ } catch {
1824
+ return value;
1825
+ }
1826
+ }
1827
+ return value;
1828
+ }
1829
+ async get(key) {
1830
+ const fullKey = this.getKey(key);
1831
+ const value = await this.client.get(fullKey);
1832
+ if (value === null) {
1833
+ return null;
1834
+ }
1835
+ return this.deserialize(value);
1836
+ }
1837
+ async set(key, value) {
1838
+ const fullKey = this.getKey(key);
1839
+ const serialized = this.serialize(value);
1840
+ if (this.ttlSeconds > 0) {
1841
+ await this.setWithExpiry(this.client, fullKey, serialized, this.ttlSeconds);
1842
+ } else {
1843
+ await this.client.set(fullKey, serialized);
1844
+ }
1845
+ }
1846
+ async listLength(key) {
1847
+ const fullKey = this.getKey(key);
1848
+ return this.client.llen(fullKey);
1849
+ }
1850
+ async listPush(key, value) {
1851
+ const fullKey = this.getKey(key);
1852
+ const serialized = this.serialize(value);
1853
+ await this.client.rpush(fullKey, serialized);
1854
+ if (this.ttlSeconds > 0) {
1855
+ await this.client.expire(fullKey, this.ttlSeconds);
1856
+ }
1857
+ }
1858
+ async listFromTo(key, from, to = -1) {
1859
+ const fullKey = this.getKey(key);
1860
+ const values = await this.client.lrange(fullKey, from, to);
1861
+ return values.map((v) => this.deserialize(v));
1862
+ }
1863
+ async delete(key) {
1864
+ const fullKey = this.getKey(key);
1865
+ await this.client.del(fullKey);
1866
+ }
1867
+ async clear() {
1868
+ const pattern = `${this.keyPrefix}*`;
1869
+ let cursor = "0";
1870
+ do {
1871
+ const [nextCursor, keys] = await this.scanKeys(this.client, cursor, pattern, 100);
1872
+ if (keys.length > 0) {
1873
+ await this.client.del(...keys);
1874
+ }
1875
+ cursor = nextCursor;
1876
+ } while (cursor !== "0" && cursor !== 0);
1877
+ }
1878
+ async increment(key) {
1879
+ const fullKey = this.getKey(key);
1880
+ return this.client.incr(fullKey);
1881
+ }
1882
+ };
1883
+ var upstashPreset = {
1884
+ setWithExpiry: (client, key, value, seconds) => client.set(key, value, { ex: seconds }),
1885
+ scanKeys: (client, cursor, pattern, count) => client.scan(cursor, { match: pattern, count })
1886
+ };
1887
+ var nodeRedisPreset = {
1888
+ setWithExpiry: (client, key, value, seconds) => client.set(key, value, { EX: seconds }),
1889
+ scanKeys: (client, cursor, pattern, count) => client.scan(cursor, { MATCH: pattern, COUNT: count })
1890
+ };
1792
1891
 
1793
- export { RedisStore, ScoresRedis, StoreMemoryRedis, WorkflowsRedis };
1892
+ export { RedisServerCache, RedisStore, ScoresRedis, StoreMemoryRedis, WorkflowsRedis, nodeRedisPreset, upstashPreset };
1794
1893
  //# sourceMappingURL=index.js.map
1795
1894
  //# sourceMappingURL=index.js.map