Spaces:
Sleeping
Sleeping
Download backend/src/utils/redis.js from edichat/edi.chat.test: direct link, hf CLI and curl.
- Browser
- Download file 3.34 kB
-
https://huggingface.co/spaces/edichat/edi.chat.test/resolve/main/backend/src/utils/redis.js
- Command line
-
hf download hf://spaces/edichat/edi.chat.test/backend/src/utils/redis.js
-
curl -L -o redis.js https://huggingface.co/spaces/edichat/edi.chat.test/resolve/main/backend/src/utils/redis.js
3.34 kB
| const { createClient } = require('redis'); | |
| const logger = require('./logger'); | |
| let redisClient; | |
| const connectRedis = async () => { | |
| // Skip Redis connection on Hugging Face Spaces or when REDIS_URL is not set | |
| if (process.env.NODE_ENV === 'production' && !process.env.REDIS_URL) { | |
| logger.info('Redis not configured for production, skipping connection'); | |
| return; | |
| } | |
| try { | |
| redisClient = createClient({ | |
| url: process.env.REDIS_URL || 'redis://localhost:6379' | |
| }); | |
| redisClient.on('error', (err) => { | |
| logger.error('Redis Client Error:', err); | |
| // Don't crash the app on Redis errors | |
| redisClient = null; | |
| }); | |
| redisClient.on('connect', () => { | |
| logger.info('Redis Client Connected'); | |
| }); | |
| redisClient.on('ready', () => { | |
| logger.info('Redis Client Ready'); | |
| }); | |
| redisClient.on('end', () => { | |
| logger.warn('Redis Client Disconnected'); | |
| }); | |
| await redisClient.connect(); | |
| // Test the connection | |
| await redisClient.ping(); | |
| logger.info('Redis connection successful'); | |
| } catch (error) { | |
| logger.error('Error connecting to Redis:', error.message); | |
| logger.info('Continuing without Redis - using in-memory storage'); | |
| redisClient = null; | |
| } | |
| }; | |
| // Redis utility functions | |
| const redisUtils = { | |
| // Set key with expiration | |
| async setex(key, seconds, value) { | |
| if (!redisClient || !redisClient.isReady) { | |
| logger.warn('Redis not available, skipping setex operation'); | |
| return null; | |
| } | |
| try { | |
| return await redisClient.setEx(key, seconds, JSON.stringify(value)); | |
| } catch (error) { | |
| logger.error('Redis setex error:', error); | |
| return null; | |
| } | |
| }, | |
| // Get key | |
| async get(key) { | |
| if (!redisClient || !redisClient.isReady) { | |
| logger.warn('Redis not available, skipping get operation'); | |
| return null; | |
| } | |
| try { | |
| const value = await redisClient.get(key); | |
| return value ? JSON.parse(value) : null; | |
| } catch (error) { | |
| logger.error('Redis get error:', error); | |
| return null; | |
| } | |
| }, | |
| // Delete key | |
| async del(key) { | |
| if (!redisClient || !redisClient.isReady) { | |
| logger.warn('Redis not available, skipping del operation'); | |
| return null; | |
| } | |
| try { | |
| return await redisClient.del(key); | |
| } catch (error) { | |
| logger.error('Redis del error:', error); | |
| return null; | |
| } | |
| }, | |
| // Set key without expiration | |
| async set(key, value) { | |
| if (!redisClient || !redisClient.isReady) { | |
| logger.warn('Redis not available, skipping set operation'); | |
| return null; | |
| } | |
| try { | |
| return await redisClient.set(key, JSON.stringify(value)); | |
| } catch (error) { | |
| logger.error('Redis set error:', error); | |
| return null; | |
| } | |
| }, | |
| // Check if key exists | |
| async exists(key) { | |
| if (!redisClient || !redisClient.isReady) { | |
| return false; | |
| } | |
| try { | |
| return await redisClient.exists(key); | |
| } catch (error) { | |
| logger.error('Redis exists error:', error); | |
| return false; | |
| } | |
| } | |
| }; | |
| // Graceful shutdown | |
| process.on('SIGINT', async () => { | |
| if (redisClient && redisClient.isOpen) { | |
| await redisClient.quit(); | |
| logger.info('Redis connection closed through app termination'); | |
| } | |
| }); | |
| module.exports = { | |
| connectRedis, | |
| redisClient: () => redisClient, | |
| redisUtils | |
| }; |