EdgeAIG's picture
download
raw
3.11 kB
/**
* Redis support shared by persistence modules.
*
* This module defines a `Redis` service that can send Redis commands and run
* Lua scripts. It does not create a Redis client itself; callers provide a
* `send` function from their client or connection pool. The module also
* provides helpers for describing Lua scripts, loading them once, and running
* them later by their cached Redis id.
*
* @since 4.0.0
*/
import * as Cache from "../../Cache.js";
import * as Context from "../../Context.js";
import * as Effect from "../../Effect.js";
import * as Equal from "../../Equal.js";
import { constant, identity } from "../../Function.js";
import * as Hash from "../../Hash.js";
import * as Schema from "../../Schema.js";
/**
* Service for sending Redis commands and evaluating cached Lua scripts.
*
* @category services
* @since 4.0.0
*/
export class Redis extends /*#__PURE__*/Context.Service()("effect/persistence/Redis") {}
/**
* Creates a `Redis` service from a raw command sender.
*
* **Details**
*
* Lua scripts are loaded through `SCRIPT LOAD`, cached, and then invoked with
* `EVALSHA`.
*
* @category constructors
* @since 4.0.0
*/
export const make = /*#__PURE__*/Effect.fnUntraced(function* (options) {
const scriptCache = yield* Cache.make({
lookup: script => options.send("SCRIPT", "LOAD", script.lua),
capacity: Number.POSITIVE_INFINITY
});
const eval_ = script => (...params) => {
const evalSha = sha => options.send("EVALSHA", sha, script.numberOfKeys(...params).toString(), ...script.params(...params).map(param => String(param)));
return Cache.get(scriptCache, script).pipe(Effect.flatMap(evalSha), Effect.catchIf(error => String(error.cause).includes("NOSCRIPT"), () => Cache.refresh(scriptCache, script).pipe(Effect.flatMap(evalSha))));
};
return identity({
send: options.send,
eval: eval_
});
});
const ErrorTypeId = "~effect/persistence/Redis/RedisError";
/**
* Error raised by Redis command or script execution.
*
* @category errors
* @since 4.0.0
*/
export class RedisError extends /*#__PURE__*/Schema.ErrorClass(ErrorTypeId)({
_tag: /*#__PURE__*/Schema.tag("RedisError"),
cause: /*#__PURE__*/Schema.Defect()
}) {
/**
* Marks this value as a Redis persistence error for runtime guards.
*
* @since 4.0.0
*/
[ErrorTypeId] = ErrorTypeId;
}
const ScriptTypeId = "~effect/persistence/Redis/Script";
const ScriptProto = {
[ScriptTypeId]: {
params: identity,
result: identity
},
withReturnType() {
return this;
},
[Equal.symbol](that) {
return this === that;
},
[Hash.symbol]() {
return Hash.random(this);
}
};
/**
* Constructs a typed Redis Lua script descriptor.
*
* **Details**
*
* The result type defaults to `void` and can be refined with
* `withReturnType`.
*
* @category Scripting
* @since 4.0.0
*/
export const script = (f, options) => Object.assign(Object.create(ScriptProto), {
...options,
params: f,
numberOfKeys: typeof options.numberOfKeys === "number" ? constant(options.numberOfKeys) : options.numberOfKeys
});
//# sourceMappingURL=Redis.js.map

Xet Storage Details

Size:
3.11 kB
·
Xet hash:
c46b9fe3642d305260c22ab96e0e10b46e1400d55f265a1b0840336e290b8fdb

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.