cleaned up cache lib
This commit is contained in:
parent
ee57b66391
commit
e98b1d8ae2
9 changed files with 637 additions and 1209 deletions
176
docs/simplified-cache-usage.md
Normal file
176
docs/simplified-cache-usage.md
Normal file
|
|
@ -0,0 +1,176 @@
|
|||
# Simplified Cache Library Usage
|
||||
|
||||
The cache library has been simplified to only use Redis/Dragonfly with a connection manager for better performance and easier management.
|
||||
|
||||
## Quick Start
|
||||
|
||||
```typescript
|
||||
import { createCache, createTradingCache, createMarketDataCache, RedisConnectionManager } from '@stock-bot/cache';
|
||||
|
||||
// Create different cache instances
|
||||
const generalCache = createCache({ keyPrefix: 'app:' });
|
||||
const tradingCache = createTradingCache(); // Uses 'trading:' prefix
|
||||
const marketCache = createMarketDataCache(); // Uses 'market:' prefix with 5min TTL
|
||||
|
||||
// All cache instances share connections by default for better performance
|
||||
```
|
||||
|
||||
## Connection Management
|
||||
|
||||
The library now uses a connection manager that allows you to control whether services share connections or get unique ones:
|
||||
|
||||
```typescript
|
||||
import { RedisConnectionManager } from '@stock-bot/cache';
|
||||
|
||||
const connectionManager = RedisConnectionManager.getInstance();
|
||||
|
||||
// For shared connections (recommended for most cases)
|
||||
const sharedRedis = connectionManager.getConnection({
|
||||
name: 'BATCH-PROCESSOR',
|
||||
singleton: true // All instances share this connection
|
||||
});
|
||||
|
||||
// For unique connections (when you need isolation)
|
||||
const uniqueRedis = connectionManager.getConnection({
|
||||
name: 'DATA-FETCHER',
|
||||
singleton: false // Each instance gets its own connection
|
||||
});
|
||||
```
|
||||
|
||||
## Cache Usage Examples
|
||||
|
||||
### Basic Operations
|
||||
|
||||
```typescript
|
||||
import { createCache } from '@stock-bot/cache';
|
||||
|
||||
const cache = createCache({ keyPrefix: 'myapp:' });
|
||||
|
||||
// Set data with default TTL (1 hour)
|
||||
await cache.set('user:123', { name: 'John', email: 'john@example.com' });
|
||||
|
||||
// Set data with custom TTL (5 minutes)
|
||||
await cache.set('session:abc', sessionData, 300);
|
||||
|
||||
// Get data
|
||||
const user = await cache.get('user:123');
|
||||
|
||||
// Check if key exists
|
||||
const exists = await cache.exists('user:123');
|
||||
|
||||
// Delete data
|
||||
await cache.del('user:123');
|
||||
|
||||
// Clear all data with this prefix
|
||||
await cache.clear();
|
||||
```
|
||||
|
||||
### Trading-Specific Caches
|
||||
|
||||
```typescript
|
||||
import { createTradingCache, createMarketDataCache, createIndicatorCache } from '@stock-bot/cache';
|
||||
|
||||
// Trading cache (1 hour TTL)
|
||||
const tradingCache = createTradingCache();
|
||||
await tradingCache.set('position:AAPL', { shares: 100, price: 150.00 });
|
||||
|
||||
// Market data cache (5 minute TTL)
|
||||
const marketCache = createMarketDataCache();
|
||||
await marketCache.set('quote:AAPL', { price: 151.25, volume: 1000000 });
|
||||
|
||||
// Indicator cache (30 minute TTL)
|
||||
const indicatorCache = createIndicatorCache();
|
||||
await indicatorCache.set('sma:AAPL:20', [150.1, 150.3, 150.8, 151.2]);
|
||||
```
|
||||
|
||||
## Connection Names in Redis
|
||||
|
||||
When you create cache instances, they will appear in Redis with clean, identifiable names:
|
||||
|
||||
- `TRADING-SERVICE` - For trading cache
|
||||
- `MARKET-SERVICE` - For market data cache
|
||||
- `INDICATORS-SERVICE` - For indicator cache
|
||||
- `CACHE-SERVICE` - For general cache
|
||||
|
||||
You can monitor all connections using:
|
||||
|
||||
```bash
|
||||
# TypeScript version (more detailed)
|
||||
bun run scripts/get-redis-connections.ts
|
||||
|
||||
# Bash version (quick check)
|
||||
./scripts/get-redis-connections.sh
|
||||
```
|
||||
|
||||
## Health Monitoring
|
||||
|
||||
```typescript
|
||||
// Check if cache is ready
|
||||
if (cache.isReady()) {
|
||||
console.log('Cache is ready for operations');
|
||||
}
|
||||
|
||||
// Wait for cache to be ready
|
||||
await cache.waitForReady(5000); // Wait up to 5 seconds
|
||||
|
||||
// Health check
|
||||
const isHealthy = await cache.health();
|
||||
|
||||
// Get performance statistics
|
||||
const stats = cache.getStats();
|
||||
console.log(`Hit rate: ${stats.hitRate}%, Total operations: ${stats.total}`);
|
||||
```
|
||||
|
||||
## Batch Processor Example
|
||||
|
||||
Here's how to set up a batch processor with a shared connection:
|
||||
|
||||
```typescript
|
||||
import { RedisConnectionManager } from '@stock-bot/cache';
|
||||
import Redis from 'ioredis';
|
||||
|
||||
export class BatchProcessor {
|
||||
private redis: Redis;
|
||||
|
||||
constructor() {
|
||||
const connectionManager = RedisConnectionManager.getInstance();
|
||||
|
||||
// All batch processor instances share this connection
|
||||
this.redis = connectionManager.getConnection({
|
||||
name: 'BATCH-PROCESSOR',
|
||||
singleton: true
|
||||
});
|
||||
}
|
||||
|
||||
async processItems(items: any[]): Promise<void> {
|
||||
await this.redis.set('batch:status', 'processing');
|
||||
|
||||
for (const item of items) {
|
||||
await this.redis.lpush('batch:queue', JSON.stringify(item));
|
||||
}
|
||||
|
||||
await this.redis.set('batch:status', 'completed');
|
||||
}
|
||||
|
||||
async getBatchStatus(): Promise<string> {
|
||||
return await this.redis.get('batch:status') || 'idle';
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
## Key Benefits
|
||||
|
||||
✅ **Simplified**: Only Redis-based caching, no complex hybrid logic
|
||||
✅ **Connection Management**: Shared connections for better performance
|
||||
✅ **Clean Monitoring**: Easy to identify connections in Redis
|
||||
✅ **Trading Optimized**: Pre-configured caches for different data types
|
||||
✅ **Type Safe**: Full TypeScript support
|
||||
✅ **Error Handling**: Graceful fallbacks and comprehensive logging
|
||||
|
||||
## Removed Features
|
||||
|
||||
❌ **Memory Cache**: Removed to avoid consistency issues
|
||||
❌ **Hybrid Cache**: Removed unnecessary complexity
|
||||
❌ **Auto-detection**: Always uses Redis/Dragonfly now
|
||||
|
||||
This simplified approach provides better performance, easier debugging, and more predictable behavior for your trading application.
|
||||
Loading…
Add table
Add a link
Reference in a new issue