running prettier for cleanup
This commit is contained in:
parent
fe7733aeb5
commit
d85cd58acd
151 changed files with 29158 additions and 27966 deletions
183
libs/cache/src/index.ts
vendored
183
libs/cache/src/index.ts
vendored
|
|
@ -1,92 +1,91 @@
|
|||
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;
|
||||
import { RedisConnectionManager } from './connection-manager';
|
||||
import { RedisCache } from './redis-cache';
|
||||
import type { CacheOptions, CacheProvider } 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;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue