39 lines
1013 B
TypeScript
39 lines
1013 B
TypeScript
import Redis from 'ioredis';
|
|
|
|
// Initialize Redis client
|
|
const redis = new Redis(process.env.REDIS_URL);
|
|
|
|
/**
|
|
* Set a value in Redis with an optional expiration time.
|
|
* @param {string} key The key under which to store the value.
|
|
* @param {string} value The value to store.
|
|
* @param {number} [expire] Optional expiration time in seconds.
|
|
*/
|
|
const setValue = async (key: string, value: string, expire?: number) => {
|
|
if (expire) {
|
|
await redis.set(key, value, 'EX', expire);
|
|
} else {
|
|
await redis.set(key, value);
|
|
}
|
|
};
|
|
|
|
/**
|
|
* Get a value from Redis.
|
|
* @param {string} key The key of the value to retrieve.
|
|
* @returns {Promise<string|null>} The value, if found, otherwise null.
|
|
*/
|
|
const getValue = async (key: string): Promise<string | null> => {
|
|
const value = await redis.get(key);
|
|
return value;
|
|
};
|
|
|
|
/**
|
|
* Delete a key from Redis.
|
|
* @param {string} key The key to delete.
|
|
*/
|
|
const deleteKey = async (key: string) => {
|
|
await redis.del(key);
|
|
};
|
|
|
|
export { setValue, getValue, deleteKey };
|