alepha
Version:
Easy-to-use modern TypeScript framework for building many kind of applications.
93 lines (92 loc) • 3.17 kB
JavaScript
import { $atom, $inject, $module, $state, Alepha, z } from "alepha";
import { AlephaCache, CacheProvider } from "alepha/cache";
import { $logger } from "alepha/logger";
import { RedisProvider } from "alepha/redis";
//#region ../../src/cache/redis/providers/RedisCacheProvider.ts
/**
* Redis cache configuration atom.
*/
const redisCacheOptions = $atom({
name: "alepha.cache.redis.options",
schema: z.object({ prefix: z.text({ description: "Prefix for all cache keys in Redis. Useful for testing or multi-tenant applications." }).optional() }),
default: {}
});
var RedisCacheProvider = class extends CacheProvider {
log = $logger();
redisProvider = $inject(RedisProvider);
options = $state(redisCacheOptions);
alepha = $inject(Alepha);
async get(name, key) {
if (!this.alepha.isStarted()) return;
const keyWithPrefix = this.prefix(name, key);
const buffer = await this.redisProvider.get(keyWithPrefix);
if (!buffer) return;
this.log.debug(`Cache hit`, {
size: buffer.byteLength,
key: keyWithPrefix
});
return new Uint8Array(buffer);
}
async set(name, key, value, ttl) {
if (!this.alepha.isReady()) return new Uint8Array(Buffer.from(value));
const buffer = Buffer.from(value);
const prefix = this.prefix(name, key);
if (ttl) return new Uint8Array(await this.redisProvider.set(prefix, buffer, { expiration: {
type: "PX",
value: ttl
} }));
return new Uint8Array(await this.redisProvider.set(prefix, buffer));
}
async del(name, ...keys) {
const nameKey = this.prefix(name);
if (keys.length === 0) {
const matched = await this.redisProvider.keys(`${nameKey}:*`);
if (matched.length > 0) await this.redisProvider.del(matched);
return;
}
const prefixed = keys.map((key) => key.startsWith(nameKey) ? key : this.prefix(name, key));
if (prefixed.length > 0) await this.redisProvider.del(prefixed);
}
async has(name, key) {
return this.redisProvider.has(this.prefix(name, key));
}
async keys(name, filter) {
if (filter) return await this.redisProvider.keys(`${this.prefix(name)}:${filter}*`);
return this.redisProvider.keys(`${this.prefix(name)}:*`);
}
async clear() {
this.log.debug("Clearing all cache");
const pattern = `${this.prefix()}:*`;
const keys = await this.redisProvider.keys(pattern);
if (keys.length > 0) await this.redisProvider.del(keys);
}
async incr(name, key, amount) {
const keyWithPrefix = this.prefix(name, key);
return this.redisProvider.incr(keyWithPrefix, amount);
}
prefix(...path) {
const parts = ["cache", ...path];
if (this.options.prefix) parts.unshift(this.options.prefix);
return parts.join(":");
}
};
//#endregion
//#region ../../src/cache/redis/index.ts
/**
* Plugin for Alepha Cache that provides Redis caching capabilities.
*
* @see {@link RedisCacheProvider}
* @module alepha.cache.redis
*/
const AlephaCacheRedis = $module({
name: "alepha.cache.redis",
services: [RedisCacheProvider],
register: (alepha) => alepha.with({
provide: CacheProvider,
use: RedisCacheProvider,
optional: true
}).with(AlephaCache)
});
//#endregion
export { AlephaCacheRedis, RedisCacheProvider, redisCacheOptions };
//# sourceMappingURL=index.js.map