92 lines
No EOL
2.5 KiB
TypeScript
92 lines
No EOL
2.5 KiB
TypeScript
import { RedisCache } from './redis-cache';
|
|
import { RedisConnectionManager } from './connection-manager';
|
|
import type { CacheProvider, CacheOptions } from './types';
|
|
|
|
// Cache instances registry to prevent multiple instances with same prefix
|
|
const cacheInstances = new Map<string, CacheProvider>();
|
|
|
|
/**
|
|
* Create a Redis cache instance with trading-optimized defaults
|
|
*/
|
|
export function createCache(options: Partial<CacheOptions> = {}): CacheProvider {
|
|
const defaultOptions: CacheOptions = {
|
|
keyPrefix: 'cache:',
|
|
ttl: 3600, // 1 hour default
|
|
enableMetrics: true,
|
|
shared: true, // Default to shared connections
|
|
...options
|
|
};
|
|
|
|
// For shared connections, reuse cache instances with the same key prefix
|
|
if (defaultOptions.shared) {
|
|
const cacheKey = `${defaultOptions.keyPrefix}-${defaultOptions.ttl}`;
|
|
|
|
if (cacheInstances.has(cacheKey)) {
|
|
return cacheInstances.get(cacheKey)!;
|
|
}
|
|
|
|
const cache = new RedisCache(defaultOptions);
|
|
cacheInstances.set(cacheKey, cache);
|
|
return cache;
|
|
}
|
|
|
|
// For non-shared connections, always create new instances
|
|
return new RedisCache(defaultOptions);
|
|
}
|
|
|
|
/**
|
|
* Create a cache instance for trading data
|
|
*/
|
|
export function createTradingCache(options: Partial<CacheOptions> = {}): CacheProvider {
|
|
return createCache({
|
|
keyPrefix: 'trading:',
|
|
ttl: 3600, // 1 hour default
|
|
enableMetrics: true,
|
|
shared: true,
|
|
...options
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Create a cache for market data with shorter TTL
|
|
*/
|
|
export function createMarketDataCache(options: Partial<CacheOptions> = {}): CacheProvider {
|
|
return createCache({
|
|
keyPrefix: 'market:',
|
|
ttl: 300, // 5 minutes for market data
|
|
enableMetrics: true,
|
|
shared: true,
|
|
...options
|
|
});
|
|
}
|
|
|
|
/**
|
|
* Create a cache for indicators with longer TTL
|
|
*/
|
|
export function createIndicatorCache(options: Partial<CacheOptions> = {}): CacheProvider {
|
|
return createCache({
|
|
keyPrefix: 'indicators:',
|
|
ttl: 1800, // 30 minutes for indicators
|
|
enableMetrics: true,
|
|
shared: true,
|
|
...options
|
|
});
|
|
}
|
|
|
|
// Export types and classes
|
|
export type {
|
|
CacheProvider,
|
|
CacheOptions,
|
|
CacheConfig,
|
|
CacheStats,
|
|
CacheKey,
|
|
SerializationOptions
|
|
} from './types';
|
|
|
|
export { RedisCache } from './redis-cache';
|
|
export { RedisConnectionManager } from './connection-manager';
|
|
export { CacheKeyGenerator } from './key-generator';
|
|
|
|
|
|
// Default export for convenience
|
|
export default createCache; |