97 lines
3.4 KiB
TypeScript
97 lines
3.4 KiB
TypeScript
#!/usr/bin/env tsx
|
|
/**
|
|
* Populate Redis with test data so you can see it in RedisInsight
|
|
*/
|
|
|
|
import { Redis } from "ioredis";
|
|
import { setCache } from "@/lib/cache";
|
|
import { initRedisRateLimiter, checkMusicRateLimit } from "@/lib/rate-limit";
|
|
|
|
async function populateRedis() {
|
|
console.log("🔍 Populating Redis with test data...\n");
|
|
|
|
// Load environment variables
|
|
if (process.env.NODE_ENV !== "production") {
|
|
try {
|
|
const { config } = await import("dotenv");
|
|
config({ path: ".env.local" });
|
|
} catch (e) {
|
|
// dotenv not available
|
|
}
|
|
}
|
|
|
|
const redisUrl = process.env.REDIS_URL;
|
|
if (!redisUrl) {
|
|
console.error("❌ REDIS_URL not set");
|
|
process.exit(1);
|
|
}
|
|
|
|
const redis = new Redis(redisUrl);
|
|
|
|
try {
|
|
// Test 1: Add some cache entries
|
|
console.log("📦 Adding cache entries...");
|
|
await setCache("test:user:123", { name: "John Doe", email: "john@example.com" }, 3600);
|
|
await setCache("test:user:456", { name: "Jane Smith", email: "jane@example.com" }, 3600);
|
|
await setCache("test:stats", { totalUsers: 150, activeUsers: 45 }, 1800);
|
|
console.log("✅ Added 3 cache entries\n");
|
|
|
|
// Test 2: Add some rate limit entries (by using the rate limiter)
|
|
console.log("⏱️ Creating rate limit entries...");
|
|
await initRedisRateLimiter(redisUrl);
|
|
|
|
const testIds = ["user:123", "user:456", "ip:192.168.1.1"];
|
|
for (const id of testIds) {
|
|
await checkMusicRateLimit(id);
|
|
await checkMusicRateLimit(id); // Make a second request
|
|
}
|
|
console.log("✅ Created rate limit entries for 3 identifiers\n");
|
|
|
|
// Test 3: Add some direct Redis keys for visibility
|
|
console.log("🔑 Adding direct Redis keys...");
|
|
await redis.set("app:version", "1.0.0");
|
|
await redis.set("app:environment", process.env.NODE_ENV || "development");
|
|
await redis.set("app:last_deploy", new Date().toISOString());
|
|
await redis.hset("app:config", {
|
|
feature_flags: "enabled",
|
|
maintenance_mode: "false",
|
|
max_users: "1000"
|
|
});
|
|
await redis.lpush("app:recent_actions", "user_login", "api_call", "cache_hit");
|
|
await redis.sadd("app:active_sessions", "session:abc123", "session:def456", "session:ghi789");
|
|
console.log("✅ Added various Redis data structures\n");
|
|
|
|
// Test 4: Add some time-series like data
|
|
console.log("📊 Adding time-series data...");
|
|
const now = Date.now();
|
|
for (let i = 0; i < 10; i++) {
|
|
const timestamp = now - (i * 60000); // 1 minute intervals
|
|
await redis.zadd("app:metrics:api_calls", timestamp, `api_call_${i}`);
|
|
}
|
|
console.log("✅ Added time-series data\n");
|
|
|
|
// Show summary
|
|
const dbSize = await redis.dbsize();
|
|
console.log("=".repeat(50));
|
|
console.log(`📊 Total keys in Redis: ${dbSize}`);
|
|
console.log("=".repeat(50) + "\n");
|
|
|
|
console.log("🔍 Key patterns you should see in RedisInsight:\n");
|
|
console.log(" • test:user:* - Cache entries");
|
|
console.log(" • test:stats - Stats cache");
|
|
console.log(" • rate_limit:* - Rate limiting data");
|
|
console.log(" • app:* - Application data");
|
|
console.log(" • app:metrics:* - Time-series data\n");
|
|
|
|
console.log("✅ Redis populated! Check RedisInsight now.\n");
|
|
|
|
await redis.quit();
|
|
} catch (error) {
|
|
console.error("❌ Error:", error instanceof Error ? error.message : String(error));
|
|
await redis.quit();
|
|
process.exit(1);
|
|
}
|
|
}
|
|
|
|
populateRedis();
|